Last active
October 8, 2023 08:15
-
-
Save dacr/f0cdb1231d73251c52f501d9b5300763 to your computer and use it in GitHub Desktop.
ZIO learning - playing with sttp async http client and zio json with complex data types / published by https://github.com/dacr/code-examples-manager #fad07854-de71-48ac-a3b8-6789959837c7/4184e1bbb533be9913ce526eedae10b498a8d63c
This file contains 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 zio 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 : fad07854-de71-48ac-a3b8-6789959837c7 | |
// created-on : 2022-01-23T11:09:24+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.3.1" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
//> using dep "com.softwaremill.sttp.client4::async-http-client-backend-zio:4.0.0-M5" | |
//> using dep "com.softwaremill.sttp.client4::zio-json:4.0.0-M5" | |
//> using dep "org.slf4j:slf4j-nop:2.0.7" | |
// --------------------- | |
import zio.*, zio.worksheet.* | |
import zio.json.* | |
import sttp.client4.* | |
import sttp.client4.asynchttpclient.zio.* | |
import sttp.client4.ziojson.* | |
case class Slide( | |
title: String, | |
`type`: String, | |
items: Option[List[String]] // WARN - Encapsulate into an Option if the field can be omitted | |
) derives JsonCodec | |
case class SlideShow( | |
author: String, | |
date: String, | |
slides: List[Slide], | |
title: String | |
) derives JsonCodec | |
case class Response( | |
slideshow: SlideShow | |
) derives JsonCodec | |
def run(): Unit = { | |
// ------------------------------------------------------------- | |
// 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