Last active
May 25, 2024 10:20
-
-
Save dacr/a8b68569f7fd52598299934f25159a24 to your computer and use it in GitHub Desktop.
ZIO learning - playing with sttp async http client and circe json / published by https://github.com/dacr/code-examples-manager #84823bec-9149-41ab-bd45-cfef3570cd03/75dfb371899b031fa216d9885d96b49cc3b40279
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
// summary : ZIO learning - playing with sttp async http client and circe json | |
// keywords : scala, zio, learning, json, pure-functional, async, sttp, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 84823bec-9149-41ab-bd45-cfef3570cd03 | |
// created-on : 2021-04-18T20:55:55Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
//> using dep "com.softwaremill.sttp.client3::async-http-client-backend-zio:3.8.15" | |
//> using dep "com.softwaremill.sttp.client3::circe:3.8.15" | |
//> using dep "io.circe::circe-generic:0.14.5" | |
//> using dep "org.slf4j:slf4j-nop:2.0.7" | |
// --------------------- | |
import zio.*, zio.worksheet.* | |
import sttp.client3.* | |
import sttp.client3.asynchttpclient.zio.* | |
import sttp.client3.circe.* | |
import io.circe.generic.auto.* | |
import io.circe.parser.* | |
import io.circe.syntax.* | |
case class ClientInfo(clientIP: String, userAgent: String) // derives io.circe.codec.Codec.AsObject | |
// ------------------------------------------------------------- | |
// THE APPLICATION LOGIC | |
val logic: ZIO[SttpBackend[Task, Any], Throwable, Unit] = for { | |
backend <- ZIO.service[SttpBackend[Task, Any]] | |
request = basicRequest.get(uri"https://mapland.fr/clientInfo").response(asJson[ClientInfo]) | |
response <- backend.send(request) | |
clientInfo <- ZIO.from(response.body) | |
_ <- Console.printLine(clientInfo.userAgent) | |
_ <- Console.printLine(clientInfo.asJson.toString) | |
} yield () | |
// ------------------------------------------------------------- | |
val httpRealLayer = AsyncHttpClientZioBackend.layer() | |
// ------------------------------------------------------------- | |
val httpStubLayer = ZLayer.succeed( | |
AsyncHttpClientZioBackend.stub | |
.whenRequestMatches(_.uri.toString() == "https://mapland.fr/clientInfo") | |
.thenRespond("""{"clientIP":"127.0.0.1","userAgent":"curl/42"}""") | |
) | |
// ------------------------------------------------------------- | |
val app = for { | |
_ <- logic.provide(httpRealLayer) // EXECUTED USING REAL STTP ENVIRONNEMENT | |
_ <- logic.provide(httpStubLayer) // EXECUTED USING STUBBED STTP ENVIRONNEMENT | |
} yield () | |
app.unsafeRun |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment