Last active
May 25, 2024 10:19
-
-
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/fb7cf7f6c74dc1b00a8f1844655da4fb779dc1ad
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 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.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.* | |
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