Last active
May 25, 2024 10:19
-
-
Save dacr/f87f1076ba87bd761e35d123c7f6b465 to your computer and use it in GitHub Desktop.
ZIO learning - http client using sttp with retries / published by https://github.com/dacr/code-examples-manager #c7e3ac74-e42c-4138-b0ec-4122eb2a588d/4e67cefbe1cfabe646ee54d1a9a167870363aaff
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 - http client using sttp with retries | |
// keywords : scala, zio, learning, pure-functional, 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 : c7e3ac74-e42c-4138-b0ec-4122eb2a588d | |
// created-on : 2021-04-04T17:33:20Z | |
// 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.client4::zio:4.0.0-M5" | |
// --------------------- | |
import zio.*, zio.worksheet.* | |
import sttp.client4.*, sttp.client4.basicRequest.* | |
import sttp.client4.httpclient.zio.HttpClientZioBackend | |
object App extends ZIOAppDefault { | |
val contentLogic = for { | |
backend <- HttpClientZioBackend.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("timeout"))(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