Last active
February 3, 2026 20:23
-
-
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/7faa495cfb467f92e5d04a40fd9dfc2dd6b81d8a
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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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