Skip to content

Instantly share code, notes, and snippets.

@matthughes
Created April 22, 2022 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthughes/5eb68d1934029090b8865dc3fc12d827 to your computer and use it in GitHub Desktop.
Save matthughes/5eb68d1934029090b8865dc3fc12d827 to your computer and use it in GitHub Desktop.
//> 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