diff --git a/zio-http-cli/src/main/scala/zio/http/endpoint/cli/Retriever.scala b/zio-http-cli/src/main/scala/zio/http/endpoint/cli/Retriever.scala index 73a78449acb..1e11dbae3b2 100644 --- a/zio-http-cli/src/main/scala/zio/http/endpoint/cli/Retriever.scala +++ b/zio-http-cli/src/main/scala/zio/http/endpoint/cli/Retriever.scala @@ -2,6 +2,8 @@ package zio.http.endpoint.cli import java.nio.file.Path +import scala.annotation.nowarn + import zio._ import zio.http._ @@ -27,10 +29,13 @@ private[cli] object Retriever { final case class URL(name: String, url: String, mediaType: MediaType) extends Retriever { val request = Request.get(http.URL(http.Path.decode(url))) - override def retrieve(): ZIO[Client, Throwable, FormField] = for { - client <- ZIO.serviceWith[Client](_.batched) - chunk <- client.request(request).flatMap(_.body.asChunk) - } yield FormField.binaryField(name, chunk, mediaType) + @nowarn("cat=deprecation") + override def retrieve(): ZIO[Client, Throwable, FormField] = ZIO.scoped { + for { + client <- ZIO.serviceWith[Client](_.batched) + chunk <- client.request(request).flatMap(_.body.asChunk) + } yield FormField.binaryField(name, chunk, mediaType) + } } final case class File(name: String, path: Path, mediaType: MediaType) extends Retriever { diff --git a/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala index b07bc0b9468..2771ff2a074 100644 --- a/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala @@ -62,7 +62,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { for { port <- server(streamingServer) client <- ZIO.service[Client] - response <- client.request( + response <- client.batched( Request.get(URL.decode(s"http://localhost:$port/simple-get").toOption.get), ) body <- response.body.asString @@ -72,7 +72,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { for { port <- server(streamingServer) client <- ZIO.service[Client] - response <- client.request( + response <- client.streaming( Request.get(URL.decode(s"http://localhost:$port/streaming-get").toOption.get), ) body <- response.body.asStream.chunks.map(chunk => new String(chunk.toArray)).runCollect @@ -94,7 +94,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { port <- server(streamingServer) client <- ZIO.service[Client] response <- client - .request( + .batched( Request.post( URL.decode(s"http://localhost:$port/simple-post").toOption.get, Body.fromStreamChunked( @@ -110,7 +110,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { port <- server(streamingServer) client <- ZIO.service[Client] response <- client - .request( + .streaming( Request.post( URL.decode(s"http://localhost:$port/streaming-echo").toOption.get, Body.fromStreamChunked( @@ -146,7 +146,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { for { boundary <- Boundary.randomUUID response <- client - .request( + .batched( Request .post( URL.decode(s"http://localhost:$port/form").toOption.get, @@ -178,8 +178,8 @@ object ClientStreamingSpec extends RoutesRunnableSpec { boundary <- Boundary.randomUUID stream = Form(fields.map(_._1): _*).multipartBytes(boundary) bytes <- stream.runCollect - response <- client.batched - .request( + response <- client + .batched( Request .post( URL.decode(s"http://localhost:$port/form").toOption.get, @@ -219,7 +219,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { boundary <- Boundary.randomUUID stream = form.multipartBytes(boundary).rechunk(chunkSize) response <- client - .request( + .streaming( Request .post( URL.decode(s"http://localhost:$port/form").toOption.get, @@ -242,7 +242,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { port <- server(streamingServer) client <- ZIO.service[Client] response <- client - .request( + .streaming( Request.post( URL.decode(s"http://localhost:$port/simple-post").toOption.get, Body.fromStreamChunked(ZStream.fail(new RuntimeException("Some error"))), @@ -261,7 +261,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec { client <- ZIO.service[Client] sync <- Promise.make[Nothing, Unit] response <- client - .request( + .streaming( Request.post( URL.decode(s"http://localhost:$port/streaming-echo").toOption.get, Body.fromStreamChunked( diff --git a/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala index 04ce505d541..2af23de234f 100644 --- a/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala @@ -78,7 +78,7 @@ object RequestStreamingServerSpec extends RoutesRunnableSpec { val host = req.headers.get(Header.Host).get val newRequest = req.copy(url = req.url.path("/2").host(host.hostAddress).port(host.port.getOrElse(80))) - ZIO.serviceWithZIO[Client](_.request(newRequest)) + ZIO.serviceWithZIO[Client](_.streaming(newRequest)) }, Method.POST / "2" -> handler { (req: Request) => req.body.asChunk.map { body => diff --git a/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala index d9a734b1ff2..8c726569e26 100644 --- a/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala @@ -86,20 +86,24 @@ object ZClientAspectSpec extends ZIOHttpSpec { annotations.head.contains("duration_ms"), ), ), - test("followRedirects")( - for { - port <- Server.install(redir ++ routes) - baseClient <- ZIO.service[Client] - client = baseClient - .url( - URL(Path.empty, Location.Absolute(Scheme.HTTP, "localhost", Some(port))), - ) - .batched @@ ZClientAspect.followRedirects(2)((resp, message) => ZIO.logInfo(message).as(resp)) - response <- client.request(Request.get(URL.empty / "redirect")) - } yield assertTrue( - extractStatus(response) == Status.Ok, - ), - ), + test("followRedirects") { + @nowarn("cat=deprecation") + def followRedirectsTest = { + for { + port <- Server.install(redir ++ routes) + baseClient <- ZIO.service[Client] + client = baseClient + .url( + URL(Path.empty, Location.Absolute(Scheme.HTTP, "localhost", Some(port))), + ) + .batched @@ ZClientAspect.followRedirects(2)((resp, message) => ZIO.logInfo(message).as(resp)) + response <- client.request(Request.get(URL.empty / "redirect")) + } yield assertTrue( + extractStatus(response) == Status.Ok, + ) + } + followRedirectsTest + }, ).provide( ZLayer.succeed(Server.Config.default.onAnyOpenPort), Server.customized, diff --git a/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala b/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala index c0c43825b32..69d68be3666 100644 --- a/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala @@ -54,7 +54,7 @@ object NettyStreamBodySpec extends RoutesRunnableSpec { } def makeRequest(client: Client, port: Int) = client - .request( + .streaming( Request.get(URL.decode(s"http://localhost:$port/with-content-length").toOption.get), ) diff --git a/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala b/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala index 5b1ac088591..6e1dcccbb5d 100644 --- a/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala @@ -259,7 +259,7 @@ object NettyConnectionPoolSpec extends RoutesRunnableSpec { .toRoutes .deployAndRequest { client => ZIO.foreachParDiscard(0 to 10) { _ => - client.batched.request(Request()).flatMap(_.body.asArray).repeatN(200) + client.batched(Request()).flatMap(_.body.asArray).repeatN(200) } }(Request()) .as(assertCompletes) diff --git a/zio-http/shared/src/main/scala/zio/http/ZClient.scala b/zio-http/shared/src/main/scala/zio/http/ZClient.scala index 1b8aa6431b9..ed319198a9c 100644 --- a/zio-http/shared/src/main/scala/zio/http/ZClient.scala +++ b/zio-http/shared/src/main/scala/zio/http/ZClient.scala @@ -18,6 +18,8 @@ package zio.http import java.net.{InetSocketAddress, URI} +import scala.annotation.nowarn + import zio._ import zio.stacktracer.TracingImplicits.disableAutoTrace @@ -37,6 +39,7 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( bodyDecoder: ZClient.BodyDecoder[Env, Err, Out], driver: ZClient.Driver[Env, ReqEnv, Err], ) extends HeaderOps[ZClient[Env, ReqEnv, In, Err, Out]] { self => + @nowarn("cat=deprecation") def apply(request: Request)(implicit ev: Body <:< In, trace: Trace): ZIO[Env & ReqEnv, Err, Out] = self.request(request) @@ -175,6 +178,7 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( ): ZClient[Env, ReqEnv, In, Err2, Out] = transform(bodyEncoder.refineOrDie(pf), bodyDecoder.refineOrDie(pf), driver.refineOrDie(pf)) + @deprecated("Use `batched` or `streaming` instead", since = "3.0.0") def request(request: Request)(implicit ev: Body <:< In, trace: Trace): ZIO[Env & ReqEnv, Err, Out] = { def makeRequest(body: Body) = { driver.request( @@ -233,11 +237,33 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( ev2: ReqEnv =:= Scope, ): ZStream[R & Env, E0, A] = ZStream.unwrapScoped[R & Env] { self - .request(request) + .streaming(request) .asInstanceOf[ZIO[R & Env & Scope, Err, Out]] .fold(ZStream.fail(_), f) } + def streaming( + request: Request, + )(implicit ev: Body <:< In, trace: Trace, ev1: ReqEnv =:= Scope): ZIO[Env & ReqEnv, Err, Out] = { + def makeRequest(body: Body) = { + driver.request( + self.version ++ request.version, + request.method, + self.url ++ request.url, + self.headers ++ request.headers, + body, + sslConfig, + proxy, + ) + } + if (bodyEncoder == ZClient.BodyEncoder.identity) + bodyDecoder.decodeZIO(makeRequest(request.body)) + else + bodyEncoder + .encode(ev(request.body)) + .flatMap(body => bodyDecoder.decodeZIO(makeRequest(body))) + } + def ssl(ssl: ClientSSLConfig): ZClient[Env, ReqEnv, In, Err, Out] = copy(sslConfig = Some(ssl)) @@ -279,7 +305,7 @@ object ZClient extends ZClientPlatformSpecific { * memory, allowing to stream response bodies */ def batched(request: Request)(implicit trace: Trace): ZIO[Client, Throwable, Response] = - ZIO.serviceWithZIO[Client](_.batched.request(request)) + ZIO.serviceWithZIO[Client](_.batched(request)) def fromDriver[Env, ReqEnv, Err](driver: Driver[Env, ReqEnv, Err]): ZClient[Env, ReqEnv, Body, Err, Response] = ZClient( @@ -312,7 +338,7 @@ object ZClient extends ZClientPlatformSpecific { * request's resources (i.e., `Scope`) */ def streaming(request: Request)(implicit trace: Trace): ZIO[Client & Scope, Throwable, Response] = - ZIO.serviceWithZIO[Client](_.request(request)) + ZIO.serviceWithZIO[Client](_.batched(request)) /** * Executes an HTTP request, and transforms the response to a `ZStream` using diff --git a/zio-http/shared/src/main/scala/zio/http/endpoint/internal/EndpointClient.scala b/zio-http/shared/src/main/scala/zio/http/endpoint/internal/EndpointClient.scala index b0fee686e4e..a171b75ab33 100644 --- a/zio-http/shared/src/main/scala/zio/http/endpoint/internal/EndpointClient.scala +++ b/zio-http/shared/src/main/scala/zio/http/endpoint/internal/EndpointClient.scala @@ -62,7 +62,7 @@ private[endpoint] final case class EndpointClient[P, I, E, O, A <: AuthType]( for { authInput <- authProvider config <- CodecConfig.codecRef.get - response <- client.request(withDefaultAcceptHeader(config, authInput)).orDie + response <- client.batched(withDefaultAcceptHeader(config, authInput)).orDie } yield response requested.flatMap { response =>