Skip to content

Instantly share code, notes, and snippets.

@kciesielski
Created May 19, 2023 12:00
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 kciesielski/da7b849f5c179d8d73b2ab562465007b to your computer and use it in GitHub Desktop.
Save kciesielski/da7b849f5c179d8d73b2ab562465007b to your computer and use it in GitHub Desktop.
//> using dep "com.softwaremill.sttp.client3::zio:3.8.15"
//> using dep "com.softwaremill.sttp.client3::slf4j-backend:3.8.15"
//> using dep "ch.qos.logback:logback-classic:1.4.7"
import org.slf4j.LoggerFactory
import sttp.client3.httpclient.zio.HttpClientZioBackend
import sttp.client3._
import sttp.client3.logging.slf4j.Slf4jLoggingBackend
import sttp.client3.logging.LogLevel
import sttp.capabilities.WebSockets
import sttp.capabilities.zio.ZioStreams
import zio._
object Main extends ZIOAppDefault {
type SttpClient = SttpBackend[Task, ZioStreams with WebSockets]
val requestbinUri = uri"https://eotetabr9f4aptr.m.pipedream.net"
def sendTestPostRequest: ZIO[SttpClient, Throwable, Unit] = ZIO.service[SttpClient].flatMap { backend =>
val file = new java.io.File("./SttpZioLog.scala")
basicRequest
.auth.bearer("token-test-value")
.post(requestbinUri)
.multipartBody(
multipart("path", "xxxx"),
multipart("overwrite", "xxxx"),
multipartFile("contents", file)
)
.response(asString)
.send(backend)
.ignore
}
def sendTestGetRequest: ZIO[SttpClient, Throwable, Unit] = ZIO.service[SttpClient].flatMap { backend =>
basicRequest
.get(requestbinUri)
.response(asString)
.send(backend)
.unit
}
val wrappedSttpBackend: ZLayer[Any, Throwable, SttpClient] = HttpClientZioBackend
.layer()
.map(env =>
ZEnvironment(
Slf4jLoggingBackend(
delegate = env.get[SttpClient],
beforeCurlInsteadOfShow = true,
logRequestBody = false,
logRequestHeaders = true,
logResponseBody = false,
logResponseHeaders = true,
beforeRequestSendLogLevel = LogLevel.Warn,
responseLogLevel = _ => LogLevel.Warn,
responseExceptionLogLevel = LogLevel.Warn
)
)
)
override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = {
(sendTestGetRequest *> sendTestPostRequest)
.provide(wrappedSttpBackend)
.exitCode
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment