Last active
September 16, 2020 16:59
-
-
Save adamw/b0f39c0eb9daec30f1413e7abae4563f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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