Last active
February 3, 2026 20:25
-
-
Save dacr/423622a035767944545cfba26142774f to your computer and use it in GitHub Desktop.
ZIO learning - http client using synchronous sttp with proxy support / published by https://github.com/dacr/code-examples-manager #6756aa6d-6d0e-4a87-87b1-b69cbc528917/cf06c4da352c911364bae3de17173a4891c0aef5
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 synchronous sttp with proxy support | |
| // keywords : scala, zio, learning, pure-functional, sttp, http-proxy, proxy, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 6756aa6d-6d0e-4a87-87b1-b69cbc528917 | |
| // created-on : 2022-01-23T11:36:49+01:00 | |
| // 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 "com.softwaremill.sttp.client3::async-http-client-backend-zio:3.8.15" | |
| //> using dep "org.slf4j:slf4j-nop:2.0.7" | |
| // --------------------- | |
| import zio.* | |
| import sttp.client3.* | |
| import sttp.client3.SttpBackendOptions.ProxyType | |
| import sttp.client3.asynchttpclient.zio.* | |
| case class HttpProxyConfig(host: String, port: Int, nonProxyHosts: List[String]) | |
| object HttpProxyConfig { | |
| def fromPropOrEnv(): Option[HttpProxyConfig] = | |
| import scala.util.Properties.{propOrNone, envOrNone} | |
| val keys = LazyList("http_proxy", "https_proxy") | |
| keys.flatMap { key => | |
| propOrNone(key).orElse(envOrNone(key)).flatMap(fromProvidedString) | |
| }.headOption | |
| def fromProvidedString(spec: String): Option[HttpProxyConfig] = | |
| val nonProxyHosts = scala.util.Properties.envOrElse("no_proxy", "").split(",\\s*").toList.map(_.trim) | |
| val extractor = """^https?://([^:/]+)(?::(\d+))?/?$""".r | |
| extractor.findFirstIn(spec.trim.toLowerCase()).collect { | |
| case extractor(host, null) => HttpProxyConfig(host, 80, nonProxyHosts) | |
| case extractor(host, port) => HttpProxyConfig(host, port.toInt, nonProxyHosts) | |
| } | |
| } | |
| def makeProxyConfig(config: HttpProxyConfig): SttpBackendOptions = | |
| import scala.concurrent.duration.* | |
| val proxy = SttpBackendOptions.Proxy(config.host, config.port, proxyType = ProxyType.Http, nonProxyHosts = config.nonProxyHosts) | |
| SttpBackendOptions(connectionTimeout = 5.seconds, Some(proxy)) | |
| // --------------------------------------------------------------------------- | |
| object Encapsulated extends ZIOAppDefault { | |
| val sttpBackend = | |
| import scala.concurrent.duration.* | |
| val options = | |
| HttpProxyConfig | |
| .fromPropOrEnv() | |
| .map(makeProxyConfig) | |
| .getOrElse(SttpBackendOptions.Default) | |
| .connectionTimeout(5.seconds) | |
| AsyncHttpClientZioBackend.scoped(options) | |
| override def run = for { | |
| backend <- sttpBackend | |
| result <- backend.send(basicRequest.get(uri"https://mapland.fr/clientInfo").response(asStringAlways)) | |
| _ <- Console.printLine(result.body) | |
| } yield () | |
| } | |
| Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment