-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use zio-http Client instead of sttp3 (#691)
* 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
1 parent
d5f6b96
commit 6b197fd
Showing
10 changed files
with
65 additions
and
119 deletions.
There are no files selected for viewing
15 changes: 5 additions & 10 deletions
15
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyApp.scala
This file contains 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
35 changes: 17 additions & 18 deletions
35
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/Client.scala
This file contains 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
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 _) | ||
|
||
} |
12 changes: 0 additions & 12 deletions
12
opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/package.scala
This file was deleted.
Oops, something went wrong.
12 changes: 3 additions & 9 deletions
12
...xample/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ClientApp.scala
This file contains 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
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 | ||
) | ||
} |
20 changes: 10 additions & 10 deletions
20
.../src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/http/HttpClient.scala
This file contains 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
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 _) | ||
|
||
} |
12 changes: 0 additions & 12 deletions
12
...-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/package.scala
This file was deleted.
Oops, something went wrong.
12 changes: 3 additions & 9 deletions
12
opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyApp.scala
This file contains 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
This file contains 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
12 changes: 0 additions & 12 deletions
12
opentracing-example/src/main/scala/zio/telemetry/opentracing/example/package.scala
This file was deleted.
Oops, something went wrong.
This file contains 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