Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active September 16, 2020 16:59
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 adamw/b0f39c0eb9daec30f1413e7abae4563f to your computer and use it in GitHub Desktop.
Save adamw/b0f39c0eb9daec30f1413e7abae4563f to your computer and use it in GitHub Desktop.
package sttp.client3.examples
import sttp.capabilities.zio.ZioStreams
import sttp.client3._
import sttp.client3.asynchttpclient.zio.{AsyncHttpClientZioBackend, SttpClient}
import zio._
import zio.console._
import zio.stream._
object StreamZio extends App {
def streamRequestBody: RIO[Console with SttpClient, Unit] = {
val stream: Stream[Throwable, Byte] = Stream("Hello, world".getBytes: _*)
SttpClient
.send(
basicRequest
.streamBody(ZioStreams)(stream)
.post(uri"https://httpbin.org/post")
)
.flatMap { response => putStrLn(s"RECEIVED:\n${response.body}") }
}
def streamResponseBody: RIO[Console with SttpClient, Unit] = {
SttpClient
.send(
basicRequest
.body("I want a stream!")
.post(uri"https://httpbin.org/post")
.response(asStreamAlways(ZioStreams)(_.transduce(Transducer.utf8Decode).fold("")(_ + _)))
)
.flatMap { response => putStrLn(s"RECEIVED:\n${response.body}") }
}
override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] = {
(streamRequestBody *> streamResponseBody)
.provideCustomLayer(AsyncHttpClientZioBackend.layer())
.exitCode
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment