Created
April 22, 2022 00:58
-
-
Save matthughes/5eb68d1934029090b8865dc3fc12d827 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> using scala "3.1.2" | |
//> using lib "co.fs2::fs2-core:3.2.7" | |
//> using lib "co.fs2::fs2-io:3.2.7" | |
//> using lib "org.http4s::http4s-core:0.23.11" | |
//> using lib "org.http4s::http4s-dsl:0.23.11" | |
//> using lib "org.http4s::http4s-ember-client:0.23.11" | |
import fs2.Stream | |
import cats.effect._ | |
import cats.syntax.all._ | |
import org.http4s._ | |
import cats.effect.IOApp | |
import cats.effect.ExitCode | |
import org.http4s.ember.client.EmberClientBuilder | |
import org.http4s.client.dsl.Http4sClientDsl | |
import org.http4s.dsl.io.GET | |
import org.http4s.syntax.all.* | |
import org.http4s.client.middleware.FollowRedirect | |
import fs2.compression.Compression | |
import fs2.compression.GunzipResult | |
object Main extends IOApp with Http4sClientDsl[IO] { | |
override def run(args: List[String]): IO[ExitCode] = { | |
EmberClientBuilder.default[IO].build.use { client => | |
val uri = uri"https://github.com/typelevel/cats-effect/archive/refs/heads/series/3.x.tar.gz" | |
val redirects = FollowRedirect(3)(client) | |
val gzipResponse: Stream[IO, GunzipResult[IO]] = | |
redirects | |
.stream(GET(uri)) | |
.flatMap { res => res.body } | |
.through(Compression[IO].gunzip()) | |
.flatTap { res => | |
Stream.eval(IO.println(s"File Entry: ${res.fileName}")) | |
} | |
val toStdOutResponse: Stream[IO, Nothing] = | |
redirects | |
.stream(GET(uri)) | |
.flatMap { res => res.body } | |
.through(fs2.io.stdout) | |
gzipResponse | |
.compile | |
.drain >> ExitCode.Success.pure[IO] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment