Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active October 8, 2023 08:15
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/8a0c73a858638297656c2bd6af17ed9f to your computer and use it in GitHub Desktop.
Save dacr/8a0c73a858638297656c2bd6af17ed9f to your computer and use it in GitHub Desktop.
ZIO learning - http client using asynchronous sttp http client / published by https://github.com/dacr/code-examples-manager #3c6cd87b-31f6-4dcd-b595-1c1091ce268c/e762af3c17f0e76b76f041b212af9fdc089deedb
// summary : ZIO learning - http client using asynchronous sttp http client
// keywords : scala, zio, learning, 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 : 3c6cd87b-31f6-4dcd-b595-1c1091ce268c
// created-on : 2021-04-04T17:33:20Z
// 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 "org.slf4j:slf4j-nop:2.0.7"
// ---------------------
import zio.*, zio.worksheet.*
import sttp.client4.*, sttp.client4.basicRequest.*
import sttp.client4.asynchttpclient.zio.AsyncHttpClientZioBackend
object App extends ZIOAppDefault {
val contentLogic = for {
backend <- AsyncHttpClientZioBackend.scoped()
request = basicRequest.get(uri"https://mapland.fr/ip")
result <- backend.send(request)
content <- ZIO.from(result.body)
} yield content
val program = contentLogic.timeoutFail(new Exception("timedout"))(5000.millis)
val programRetried = program.retry(Schedule.exponential(1.second) && Schedule.recurs(3))
override def run =
programRetried
.tap(result => Console.printLine(result))
.provide(Scope.default)
}
App.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment