Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sttpClientTag #2312

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AsyncHttpClientZioBackend private (
closeClient: Boolean,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder,
webSocketBufferCapacity: Option[Int]
) extends AsyncHttpClientBackend[Task, ZioStreams, ZioStreams with WebSockets](
) extends AsyncHttpClientBackend[Task, ZioStreams, ZioWebSocketsStreams](
asyncHttpClient,
new RIOMonadAsyncError[Any],
closeClient,
Expand Down Expand Up @@ -105,20 +105,16 @@ object AsyncHttpClientZioBackend {
closeClient: Boolean,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder,
webSocketBufferCapacity: Option[Int]
): SttpBackend[Task, ZioStreams with WebSockets] =
): SttpBackend[Task, ZioWebSocketsStreams] =
new FollowRedirectsBackend(
new AsyncHttpClientZioBackend(runtime, asyncHttpClient, closeClient, customizeRequest, webSocketBufferCapacity)
)

// work-around for "You must not use an intersection type, yet have provided SttpBackend[Task, ZioStreams & WebSockets]", #2244
implicit val sttpClientTag: Tag[SttpBackend[Task, ZioStreams with WebSockets]] =
Tag.materialize[SttpBackend[Task, ZioStreams]].asInstanceOf[Tag[SttpBackend[Task, ZioStreams with WebSockets]]]

def apply(
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Task[SttpBackend[Task, ZioStreams with WebSockets]] =
): Task[SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO
.runtime[Any]
.flatMap(runtime =>
Expand All @@ -137,21 +133,21 @@ object AsyncHttpClientZioBackend {
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): ZIO[Scope, Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): ZIO[Scope, Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO.acquireRelease(apply(options, customizeRequest, webSocketBufferCapacity))(_.close().ignore)

def layer(
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Layer[Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): Layer[Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZLayer.scoped(scoped(options, customizeRequest, webSocketBufferCapacity))

def usingConfig(
cfg: AsyncHttpClientConfig,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Task[SttpBackend[Task, ZioStreams with WebSockets]] =
): Task[SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO
.runtime[Any]
.flatMap(runtime =>
Expand All @@ -170,14 +166,14 @@ object AsyncHttpClientZioBackend {
cfg: AsyncHttpClientConfig,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): ZIO[Scope, Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): ZIO[Scope, Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO.acquireRelease(usingConfig(cfg, customizeRequest, webSocketBufferCapacity))(_.close().ignore)

def layerUsingConfig(
cfg: AsyncHttpClientConfig,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Layer[Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): Layer[Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZLayer.scoped(scopedUsingConfig(cfg, customizeRequest, webSocketBufferCapacity))

/** @param updateConfig
Expand All @@ -188,7 +184,7 @@ object AsyncHttpClientZioBackend {
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Task[SttpBackend[Task, ZioStreams with WebSockets]] =
): Task[SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO
.runtime[Any]
.flatMap(runtime =>
Expand All @@ -211,7 +207,7 @@ object AsyncHttpClientZioBackend {
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): ZIO[Scope, Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): ZIO[Scope, Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO.acquireRelease(usingConfigBuilder(updateConfig, options, customizeRequest, webSocketBufferCapacity))(
_.close().ignore
)
Expand All @@ -224,22 +220,22 @@ object AsyncHttpClientZioBackend {
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Layer[Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): Layer[Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZLayer.scoped(scopedUsingConfigBuilder(updateConfig, options, customizeRequest, webSocketBufferCapacity))

def usingClient[R](
runtime: Runtime[R],
client: AsyncHttpClient,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): SttpBackend[Task, ZioStreams with WebSockets] =
): SttpBackend[Task, ZioWebSocketsStreams] =
AsyncHttpClientZioBackend(runtime, client, closeClient = false, customizeRequest, webSocketBufferCapacity)

def layerUsingClient(
client: AsyncHttpClient,
customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity,
webSocketBufferCapacity: Option[Int] = AsyncHttpClientBackend.DefaultWebSocketBufferCapacity
): Layer[Nothing, SttpBackend[Task, ZioStreams with WebSockets]] =
): Layer[Nothing, SttpBackend[Task, ZioWebSocketsStreams]] =
ZLayer.scoped(
ZIO.acquireRelease(
ZIO.runtime.map((runtime: Runtime[Any]) =>
Expand All @@ -255,7 +251,7 @@ object AsyncHttpClientZioBackend {
*
* See [[SttpBackendStub]] for details on how to configure stub responses.
*/
def stub: SttpBackendStub[Task, ZioStreams with WebSockets] =
def stub: SttpBackendStub[Task, ZioWebSocketsStreams] =
SttpBackendStub(new RIOMonadAsyncError[Any])

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package sttp.client3.asynchttpclient

import _root_.zio._
import sttp.capabilities.Effect
import sttp.capabilities.{Effect, WebSockets}
import sttp.capabilities.zio.ZioStreams
import sttp.client3._
import sttp.client3.impl.zio.ExtendEnv

package object zio {

type ZioWebSocketsStreams = ZioStreams & WebSockets

/** Type alias to be used as the sttp ZIO service (mainly in ZIO environment). */
type SttpClient = SttpBackend[Task, ZioStreams]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HttpClientZioBackend private (
) extends HttpClientAsyncBackend[
Task,
ZioStreams,
ZioStreams with WebSockets,
ZioWebSocketsStreams,
Publisher[ju.List[ByteBuffer]],
ZioStreams.BinaryStream
](
Expand Down Expand Up @@ -101,7 +101,7 @@ object HttpClientZioBackend {
closeClient: Boolean,
customizeRequest: HttpRequest => HttpRequest,
customEncodingHandler: ZioEncodingHandler
): SttpBackend[Task, ZioStreams with WebSockets] =
): SttpBackend[Task, ZioWebSocketsStreams] =
new FollowRedirectsBackend(
new HttpClientZioBackend(
client,
Expand All @@ -115,7 +115,7 @@ object HttpClientZioBackend {
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: HttpRequest => HttpRequest = identity,
customEncodingHandler: ZioEncodingHandler = PartialFunction.empty
): Task[SttpBackend[Task, ZioStreams with WebSockets]] = {
): Task[SttpBackend[Task, ZioWebSocketsStreams]] = {
ZIO.executor.flatMap(executor =>
ZIO.attempt(
HttpClientZioBackend(
Expand All @@ -132,7 +132,7 @@ object HttpClientZioBackend {
options: SttpBackendOptions = SttpBackendOptions.Default,
customizeRequest: HttpRequest => HttpRequest = identity,
customEncodingHandler: ZioEncodingHandler = PartialFunction.empty
): ZIO[Scope, Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): ZIO[Scope, Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO.acquireRelease(apply(options, customizeRequest, customEncodingHandler))(
_.close().ignore
)
Expand All @@ -141,7 +141,7 @@ object HttpClientZioBackend {
client: HttpClient,
customizeRequest: HttpRequest => HttpRequest = identity,
customEncodingHandler: ZioEncodingHandler = PartialFunction.empty
): ZIO[Scope, Throwable, SttpBackend[Task, ZioStreams with WebSockets]] =
): ZIO[Scope, Throwable, SttpBackend[Task, ZioWebSocketsStreams]] =
ZIO.acquireRelease(
ZIO.attempt(HttpClientZioBackend(client, closeClient = true, customizeRequest, customEncodingHandler))
)(_.close().ignore)
Expand All @@ -166,7 +166,7 @@ object HttpClientZioBackend {
client: HttpClient,
customizeRequest: HttpRequest => HttpRequest = identity,
customEncodingHandler: ZioEncodingHandler = PartialFunction.empty
): SttpBackend[Task, ZioStreams with WebSockets] =
): SttpBackend[Task, ZioWebSocketsStreams] =
HttpClientZioBackend(
client,
closeClient = false,
Expand Down Expand Up @@ -198,5 +198,5 @@ object HttpClientZioBackend {
*
* See [[SttpBackendStub]] for details on how to configure stub responses.
*/
def stub: SttpBackendStub[Task, ZioStreams with WebSockets] = SttpBackendStub(new RIOMonadAsyncError[Any])
def stub: SttpBackendStub[Task, ZioWebSocketsStreams] = SttpBackendStub(new RIOMonadAsyncError[Any])
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import sttp.client3.impl.zio.ExtendEnv

package object zio {

/** Type alias to be used as the sttp ZIO service (mainly in ZIO environment). */
type SttpClient = SttpBackend[Task, ZioStreams & WebSockets]
type ZioWebSocketsStreams = ZioStreams & WebSockets

// work-around for "You must not use an intersection type, yet have provided SttpBackend[Task, ZioStreams & WebSockets]", #2244
implicit val sttpClientTag: Tag[SttpClient] =
Tag.materialize[SttpBackend[Task, ZioStreams]].asInstanceOf[Tag[SttpClient]]
/** Type alias to be used as the sttp ZIO service (mainly in ZIO environment). */
type SttpClient = SttpBackend[Task, ZioWebSocketsStreams]

/** Sends the request. Only requests for which the method & URI are specified can be sent.
*
Expand All @@ -28,15 +26,15 @@ package object zio {
* Known exceptions are converted to one of `SttpClientException`. Other exceptions are kept unchanged.
*/
def send[T](
request: Request[T, Effect[Task] with ZioStreams with WebSockets]
request: Request[T, Effect[Task] with ZioWebSocketsStreams]
): ZIO[SttpClient, Throwable, Response[T]] =
ZIO.serviceWithZIO[SttpClient](_.send(request))

/** A variant of `send` which allows the effects that are part of the response handling specification (when using
* websockets or resource-safe streaming) to use an `R` environment.
*/
def sendR[T, R](
request: Request[T, Effect[RIO[R, *]] with ZioStreams with WebSockets]
request: Request[T, Effect[RIO[R, *]] with ZioWebSocketsStreams]
): ZIO[SttpClient with R, Throwable, Response[T]] =
ZIO.serviceWithZIO[SttpClient](_.extendEnv[R].send(request))
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ object GetAndParseJsonZioCirce extends ZIOAppDefault {
_ <- Console.printLine(s"Got response code: ${response.code}")
_ <- Console.printLine(response.body.toString)
} yield ()
}.provideLayer(HttpClientZioBackend.layer())
}.provideLayer(ZLayer.debug("additional layer") ++ HttpClientZioBackend.layer())

}
Loading