Skip to content

Instantly share code, notes, and snippets.

@Koroeskohr
Last active February 25, 2023 10:51
Show Gist options
  • Save Koroeskohr/c62025c48fc629658e92aad82786cd39 to your computer and use it in GitHub Desktop.
Save Koroeskohr/c62025c48fc629658e92aad82786cd39 to your computer and use it in GitHub Desktop.
//> using lib "org.typelevel::toolkit::0.0.2"
import cats.effect.*
import io.circe.Decoder
import fs2.Stream
import fs2.io.file.*
import org.http4s.ember.client.*
import org.http4s.*
import org.http4s.implicits.*
import org.http4s.circe.*
object Hello extends IOApp.Simple:
case class Data(value: String)
implicit val echoDecoder: Decoder[Data] = Decoder.forProduct1("data")(Data.apply)
def run = {
EmberClientBuilder.default[IO].build.use { client =>
val request: Request[IO] = Request(Method.POST, uri"https://httpbin.org/anything")
.withEntity("file.txt bunchofdata")
client.expect[Data](request)(jsonOf[IO, Data])
.map(_.value.split(" "))
.flatMap { result =>
val Array(fileName, content) = result
val path = Path(fileName)
IO.println(s"Writing data to $path") *>
Stream.emit(content)
.through(fs2.text.utf8.encode)
.through(
Files[IO].writeAll(path)
)
.compile
.drain
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment