Skip to content

Commit

Permalink
Use zio-http Client instead of sttp3 (#691)
Browse files Browse the repository at this point in the history
* Use zio-http Client instead of sttp3

* Use zio-http Client instead of sttp3

* Use zio-http Client instead of sttp3

* Use zio-http Client instead of sttp3
  • Loading branch information
IvanFinochenko authored May 23, 2023
1 parent d5f6b96 commit 6b197fd
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package zio.telemetry.opentelemetry.example

import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import zio._
import zio.config.magnolia._
import zio.config.typesafe.TypesafeConfig
import zio.telemetry.opentelemetry.example.config.AppConfig
import zio.telemetry.opentelemetry.example.http.{ Client, ProxyHttpApp, ProxyHttpServer }
import zio._
import zio.http.ZClient
import zio.telemetry.opentelemetry.baggage.Baggage
import zio.telemetry.opentelemetry.context.ContextStorage
import zio.telemetry.opentelemetry.example.config.AppConfig
import zio.telemetry.opentelemetry.example.http.{ Client, ProxyHttpApp, ProxyHttpServer }
import zio.telemetry.opentelemetry.tracing.Tracing

object ProxyApp extends ZIOAppDefault {

private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig])

private val httpBackendLayer: TaskLayer[Backend] =
ZLayer.scoped {
ZIO.acquireRelease(AsyncHttpClientZioBackend())(_.close().ignore)
}

override def run: Task[ExitCode] =
ZIO
.serviceWithZIO[ProxyHttpServer](_.start.exitCode)
.provide(
configLayer,
httpBackendLayer,
ZClient.default,
Client.live,
ProxyHttpServer.live,
ProxyHttpApp.live,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
package zio.telemetry.opentelemetry.example.http

import sttp.client3._
import sttp.client3.ziojson._
import sttp.model.Uri
import zio.telemetry.opentelemetry.example.config.AppConfig
import zio._
import zio.telemetry.opentelemetry.example.Backend
import zio.http.{ Header, Headers, Request, URL }
import zio.json._
import zio.telemetry.opentelemetry.example.config.AppConfig

import java.nio.charset.StandardCharsets

case class Client(backend: Backend, config: AppConfig) {
case class Client(backend: zio.http.Client, config: AppConfig) {

private val backendUrl =
Uri
.safeApply(config.backend.host, config.backend.port)
.map(_.withPath("status"))
URL
.decode(s"http://${config.backend.host}:${config.backend.port}")
.left
.map(new IllegalArgumentException(_))

def status(headers: Map[String, String]): Task[Statuses] =
for {
url <- ZIO.fromEither(backendUrl)
response <- backend
.send(
basicRequest
.get(url.withPath("status"))
.headers(headers)
.response(asJson[Status])
)
status = response.body.getOrElse(Status.down("backend"))
request = Request
.get(url.withPath("status"))
.copy(headers = Headers(headers.map(x => Header.Custom(x._1, x._2))))
response <- backend.request(request)
json <- response.body.asString(StandardCharsets.UTF_8)
status <- ZIO
.fromEither(JsonDecoder[Status].decodeJson(json))
.catchAll(_ => ZIO.succeed(Status.down("backend")))
} yield Statuses(List(status, Status.up("proxy")))

}

object Client {

val live: RLayer[AppConfig with Backend, Client] =
val live: RLayer[AppConfig with zio.http.Client, Client] =
ZLayer.fromFunction(Client.apply _)

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package zio.telemetry.opentelemetry.instrumentation.example

import sttp.client3.httpclient.zio.HttpClientZioBackend
import zio._
import zio.config.ReadError
import zio.config.magnolia.descriptor
import zio.config.typesafe.TypesafeConfig
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig
import zio._
import zio.config.ReadError
import zio.telemetry.opentelemetry.instrumentation.example.http.HttpClient

object ClientApp extends ZIOAppDefault {

private val configLayer: Layer[ReadError[String], AppConfig] =
TypesafeConfig.fromResourcePath(descriptor[AppConfig])

private val httpBackendLayer: TaskLayer[Backend] =
ZLayer.scoped {
ZIO.acquireRelease(HttpClientZioBackend())(_.close().ignore)
}

override def run: Task[ExitCode] =
ZIO
.serviceWithZIO[HttpClient](_.health.exitCode)
.provide(
configLayer,
httpBackendLayer,
zio.http.Client.default,
HttpClient.live
)
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package zio.telemetry.opentelemetry.instrumentation.example.http

import sttp.client3._
import sttp.model.Uri
import zio._
import zio.telemetry.opentelemetry.instrumentation.example.Backend
import zio.http.{ Request, URL }
import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig

case class HttpClient(backend: Backend, config: AppConfig) {
import java.nio.charset.StandardCharsets

case class HttpClient(backend: zio.http.Client, config: AppConfig) {

private val backendUrl =
Uri
.safeApply(config.server.host, config.server.port)
.map(_.withPath("status"))
URL
.decode(s"http://${config.server.host}:${config.server.port}")
.left
.map(new IllegalArgumentException(_))

def health: Task[String] =
for {
url <- ZIO.fromEither(backendUrl)
response <- backend.send(basicRequest.get(url.withPath("health")).response(asStringAlways))
result = response.body
request = Request.get(url.withPath("health"))
response <- backend.request(request)
result <- response.body.asString(StandardCharsets.UTF_8)
} yield result

}

object HttpClient {

val live: RLayer[AppConfig with Backend, HttpClient] =
val live: RLayer[AppConfig with zio.http.Client, HttpClient] =
ZLayer.fromFunction(HttpClient.apply _)

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package zio.telemetry.opentracing.example

import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import zio._
import zio.config.magnolia._
import zio.config.typesafe.TypesafeConfig
import zio.telemetry.opentracing.OpenTracing
import zio.telemetry.opentracing.example.config.AppConfig
import zio.telemetry.opentracing.example.http.{ Client, ProxyHttpApp, ProxyHttpServer }
import zio._
import zio.telemetry.opentracing.OpenTracing

object ProxyApp extends ZIOAppDefault {

private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig])

private val httpBackendLayer: TaskLayer[Backend] =
ZLayer.scoped {
ZIO.acquireRelease(AsyncHttpClientZioBackend())(_.close().ignore)
}

override def run: Task[ExitCode] =
ZIO
.serviceWithZIO[ProxyHttpServer](_.start.exitCode)
.provide(
configLayer,
httpBackendLayer,
zio.http.Client.default,
Client.live,
ProxyHttpServer.live,
ProxyHttpApp.live,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package zio.telemetry.opentracing.example.http

import sttp.client3._
import sttp.client3.ziojson._
import sttp.model.Uri
import zio._
import zio.telemetry.opentracing.example.Backend
import zio.http.{ Header, Headers, Request, URL }
import zio.json.JsonDecoder
import zio.telemetry.opentracing.example.config.AppConfig

case class Client(backend: Backend, config: AppConfig) {
import java.nio.charset.StandardCharsets

case class Client(backend: zio.http.Client, config: AppConfig) {

private val backendUrl =
Uri
.safeApply(config.backend.host, config.backend.port)
.map(_.withPath("status"))
URL
.decode(s"http://${config.backend.host}:${config.backend.port}")
.left
.map(new IllegalArgumentException(_))

Expand All @@ -21,21 +20,21 @@ case class Client(backend: Backend, config: AppConfig) {
): Task[Statuses] =
for {
url <- ZIO.fromEither(backendUrl)
response <- backend
.send(
basicRequest
.get(url.withPath("status"))
.headers(headers)
.response(asJson[Status])
)
status = response.body.getOrElse(Status.down("backend"))
request = Request
.get(url.withPath("status"))
.copy(headers = Headers(headers.map(x => Header.Custom(x._1, x._2))))
response <- backend.request(request)
json <- response.body.asString(StandardCharsets.UTF_8)
status <- ZIO
.fromEither(JsonDecoder[Status].decodeJson(json))
.catchAll(_ => ZIO.succeed(Status.down("backend")))
} yield Statuses(List(status, Status.up("proxy")))

}

object Client {

val live: RLayer[AppConfig with Backend, Client] =
val live: RLayer[AppConfig with zio.http.Client, Client] =
ZLayer.fromFunction(Client.apply _)

}

This file was deleted.

21 changes: 11 additions & 10 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ object Dependencies {
val slf4j = "1.7.36"
val sttp3 = "3.7.0"
val zipkin = "2.16.3"
val zioHttp = "2.0.0-RC10"
val zHttp = "2.0.0-RC10"
val zioJson = "0.3.0-RC10"
val zioConfig = "3.0.1"
val zioHttp = "3.0.0-RC1"
}

lazy val zio = Seq(
Expand Down Expand Up @@ -67,7 +68,7 @@ object Dependencies {
Orgs.jaegertracing % "jaeger-client" % ExampleVersions.jaeger,
Orgs.jaegertracing % "jaeger-zipkin" % ExampleVersions.jaeger,
Orgs.softwaremillSttpClient3 %% "zio-json" % ExampleVersions.sttp3,
Orgs.d11 %% "zhttp" % ExampleVersions.zioHttp,
Orgs.d11 %% "zhttp" % ExampleVersions.zHttp,
Orgs.zio %% "zio-json" % ExampleVersions.zioJson,
Orgs.zio %% "zio-config" % ExampleVersions.zioConfig,
Orgs.zio %% "zio-config-magnolia" % ExampleVersions.zioConfig,
Expand All @@ -77,20 +78,20 @@ object Dependencies {
)

lazy val opentracingExample = example ++ Seq(
Orgs.softwaremillSttpClient3 %% "async-http-client-backend-zio" % ExampleVersions.sttp3,
"io.zipkin.reporter2" % "zipkin-reporter" % ExampleVersions.zipkin,
"io.zipkin.reporter2" % "zipkin-sender-okhttp3" % ExampleVersions.zipkin
"io.zipkin.reporter2" % "zipkin-reporter" % ExampleVersions.zipkin,
"io.zipkin.reporter2" % "zipkin-sender-okhttp3" % ExampleVersions.zipkin,
Orgs.zio %% "zio-http" % ExampleVersions.zioHttp
)

lazy val opentelemetryExample = example ++ Seq(
Orgs.softwaremillSttpClient3 %% "async-http-client-backend-zio" % ExampleVersions.sttp3,
Orgs.opentelemetry % "opentelemetry-exporter-jaeger" % Versions.opentelemetry,
Orgs.opentelemetry % "opentelemetry-sdk" % Versions.opentelemetry,
Orgs.grpc % "grpc-netty-shaded" % ExampleVersions.grpcNetty
Orgs.opentelemetry % "opentelemetry-exporter-jaeger" % Versions.opentelemetry,
Orgs.opentelemetry % "opentelemetry-sdk" % Versions.opentelemetry,
Orgs.grpc % "grpc-netty-shaded" % ExampleVersions.grpcNetty,
Orgs.zio %% "zio-http" % ExampleVersions.zioHttp
)

lazy val opentelemetryInstrumentationExample = example ++ Seq(
Orgs.softwaremillSttpClient3 %% "zio" % ExampleVersions.sttp3
Orgs.zio %% "zio-http" % ExampleVersions.zioHttp
)

}

0 comments on commit 6b197fd

Please sign in to comment.