Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:40
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 dacr/a8b68569f7fd52598299934f25159a24 to your computer and use it in GitHub Desktop.
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/8e1a939fe981e6f0c13f88cc1ea802b63750973f
// 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.2.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