Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
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/63111a33f62818e348b8926fd06d07d1 to your computer and use it in GitHub Desktop.
Save dacr/63111a33f62818e348b8926fd06d07d1 to your computer and use it in GitHub Desktop.
ZIO learning - playing with sttp async http client and circe json with complex data types / published by https://github.com/dacr/code-examples-manager #a8a4242c-77f5-4a7a-affe-5b8a2bb44a32/aea85c641dbb14f77cd9df031075881c5aaf380c
// summary : ZIO learning - playing with sttp async http client and circe json with complex data types
// 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 : a8a4242c-77f5-4a7a-affe-5b8a2bb44a32
// 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.*
def run(): Unit = {
case class Slide(
title: String,
`type`: String,
items: Option[List[String]] // WARN - Encapsulate into an Option if the field can be omitted
)
case class SlideShow(
author: String,
date: String,
slides: List[Slide],
title: String
)
case class Response(
slideshow: SlideShow
)
// -------------------------------------------------------------
// THE APPLICATION LOGIC
val logic = for {
sttpBackend <- ZIO.service[SttpBackend[Task, Any]]
request = basicRequest.get(uri"https://httpbin.org/json").response(asJson[Response])
response <- sttpBackend.send(request)
clientInfo <- ZIO.fromEither(response.body)
_ <- Console.printLine(clientInfo.toString)
} yield ()
// -------------------------------------------------------------
// EXECUTED USING STUBBED STTP ENVIRONNEMENT
{
val referenceResponse =
"""{
| "slideshow": {
| "author": "Yours Truly",
| "date": "date of publication",
| "slides": [
| {
| "title": "Wake up to WonderWidgets!",
| "type": "all"
| },
| {
| "items": [
| "Why <em>WonderWidgets</em> are great",
| "Who <em>buys</em> WonderWidgets"
| ],
| "title": "Overview",
| "type": "all"
| }
| ],
| "title": "Sample Slide Show"
| }
|}
|""".stripMargin
val httpClientLayer = ZLayer.succeed(
AsyncHttpClientZioBackend.stub
.whenRequestMatches(_.uri.toString() == "https://httpbin.org/json")
.thenRespond(referenceResponse)
)
logic.provideLayer(httpClientLayer).unsafeRun
}
// -------------------------------------------------------------
// EXECUTED USING REAL STTP ENVIRONNEMENT
{
val httpClientLayer = AsyncHttpClientZioBackend.layer()
logic.provideLayer(httpClientLayer).unsafeRun
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment