Skip to content

Commit

Permalink
issue_2212 update client proxy settings (#2336)
Browse files Browse the repository at this point in the history
* issue_2212 update client proxy settings

* issue_2212 incorporate the review comments
  • Loading branch information
rajcspsg authored Aug 30, 2023
1 parent 260cc73 commit 6411c97
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions zio-http-testkit/src/main/scala/zio/http/TestClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ final case class TestClient(
headers: Headers,
body: Body,
sslConfig: Option[zio.http.ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Any, Throwable, Response] = {
val notFound: PartialFunction[Request, ZIO[Any, Response, Response]] = { case _: Request =>
ZIO.succeed(Response.notFound)
Expand Down
30 changes: 22 additions & 8 deletions zio-http/src/main/scala/zio/http/ZClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final case class ZClient[-Env, -In, +Err, +Out](
url: URL,
headers: Headers,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
bodyEncoder: ZClient.BodyEncoder[Env, Err, In],
bodyDecoder: ZClient.BodyDecoder[Env, Err, Out],
driver: ZClient.Driver[Env, Err],
Expand Down Expand Up @@ -162,6 +163,7 @@ final case class ZClient[-Env, -In, +Err, +Out](
self.headers ++ request.headers,
request.body,
sslConfig,
proxy,
),
)
else
Expand All @@ -177,6 +179,7 @@ final case class ZClient[-Env, -In, +Err, +Out](
self.headers ++ request.headers,
body,
sslConfig,
proxy,
),
),
)
Expand All @@ -197,6 +200,7 @@ final case class ZClient[-Env, -In, +Err, +Out](
headers,
body,
sslConfig,
proxy,
)

def retry[Env1 <: Env](policy: Schedule[Env1, Err, Any]): ZClient[Env1, In, Err, Out] =
Expand All @@ -218,6 +222,9 @@ final case class ZClient[-Env, -In, +Err, +Out](
def ssl(ssl: ClientSSLConfig): ZClient[Env, In, Err, Out] =
copy(sslConfig = Some(ssl))

def proxy(proxy: Proxy): ZClient[Env, In, Err, Out] =
copy(proxy = Some(proxy))

def transform[Env2, In2, Err2, Out2](
bodyEncoder: ZClient.BodyEncoder[Env2, Err2, In2],
bodyDecoder: ZClient.BodyDecoder[Env2, Err2, Out2],
Expand All @@ -228,6 +235,7 @@ final case class ZClient[-Env, -In, +Err, +Out](
url,
headers,
sslConfig,
proxy,
bodyEncoder,
bodyDecoder,
driver,
Expand Down Expand Up @@ -278,6 +286,7 @@ object ZClient {
URL.empty,
Headers.empty,
None,
None,
BodyEncoder.identity,
BodyDecoder.identity,
driver,
Expand Down Expand Up @@ -363,7 +372,7 @@ object ZClient {

trait Driver[-Env, +Err] { self =>
final def apply(request: Request)(implicit trace: Trace): ZIO[Env & Scope, Err, Response] =
self.request(request.version, request.method, request.url, request.headers, request.body, None)
self.request(request.version, request.method, request.url, request.headers, request.body, None, None)

final def disableStreaming(implicit ev: Err <:< Throwable): Driver[Env, Throwable] = {
val self0 = self.widenError[Throwable]
Expand All @@ -376,8 +385,9 @@ object ZClient {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env & Scope, Throwable, Response] =
self0.request(version, method, url, headers, body, sslConfig).flatMap { response =>
self0.request(version, method, url, headers, body, sslConfig, proxy).flatMap { response =>
response.body.asChunk.map { chunk =>
response.copy(body = Body.fromChunk(chunk))
}
Expand Down Expand Up @@ -413,8 +423,9 @@ object ZClient {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env & Scope, Err2, Response] =
self.request(version, method, url, headers, body, sslConfig).mapError(f)
self.request(version, method, url, headers, body, sslConfig, proxy).mapError(f)

override def socket[Env1 <: Env](
version: Version,
Expand Down Expand Up @@ -443,8 +454,9 @@ object ZClient {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env & Scope, Err2, Response] =
self.request(version, method, url, headers, body, sslConfig).refineOrDie(pf)
self.request(version, method, url, headers, body, sslConfig, proxy).refineOrDie(pf)

override def socket[Env1 <: Env](
version: Version,
Expand All @@ -469,10 +481,11 @@ object ZClient {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env & Scope, Err, Response]

final def request(req: Request)(implicit trace: Trace): ZIO[Env & Scope, Err, Response] =
request(req.version, req.method, req.url, req.headers, req.body, None)
request(req.version, req.method, req.url, req.headers, req.body, None, None)

final def retry[Env1 <: Env, Err1 >: Err](policy: zio.Schedule[Env1, Err1, Any]) =
new Driver[Env1, Err1] {
Expand All @@ -483,8 +496,9 @@ object ZClient {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env1 & Scope, Err1, Response] =
self.request(version, method, url, headers, body, sslConfig).retry(policy)
self.request(version, method, url, headers, body, sslConfig, proxy).retry(policy)

override def socket[Env2 <: Env1](
version: Version,
Expand Down Expand Up @@ -629,10 +643,10 @@ object ZClient {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Scope, Throwable, Response] = {
val request = Request(version, method, url, headers, body, None)
val cfg = sslConfig.fold(config)(config.ssl)

val cfg = config.copy(ssl = sslConfig.orElse(config.ssl), proxy = proxy.orElse(config.proxy))
requestAsync(request, cfg, () => WebSocketApp.unit, None)
}

Expand Down
6 changes: 4 additions & 2 deletions zio-http/src/main/scala/zio/http/ZClientAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ object ZClientAspect {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env & Scope, Err, Response] =
oldDriver
.request(version, method, url, headers, body, sslConfig)
.request(version, method, url, headers, body, sslConfig, proxy)
.sandbox
.exit
.timed
Expand Down Expand Up @@ -210,9 +211,10 @@ object ZClientAspect {
headers: Headers,
body: Body,
sslConfig: Option[ClientSSLConfig],
proxy: Option[Proxy],
)(implicit trace: Trace): ZIO[Env & Scope, Err, Response] = {
oldDriver
.request(version, method, url, headers, body, sslConfig)
.request(version, method, url, headers, body, sslConfig, proxy)
.sandbox
.exit
.timed
Expand Down
23 changes: 22 additions & 1 deletion zio-http/src/test/scala/zio/http/ClientProxySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import java.net.ConnectException
import zio.test.Assertion._
import zio.test.TestAspect.{sequential, timeout, withLiveClock}
import zio.test._
import zio.{Scope, ZIO, ZLayer, durationInt}
import zio.{Scope, Trace, ZIO, ZLayer, durationInt}

import zio.http.ZClient.{Config, DriverLive}
import zio.http.internal.{DynamicServer, HttpRunnableSpec, serverTestLayer}
import zio.http.netty.NettyConfig
import zio.http.netty.client.NettyClientDriver
Expand All @@ -49,6 +50,26 @@ object ClientProxySpec extends HttpRunnableSpec {
} yield out
assertZIO(res.either)(isLeft(isSubtype[ConnectException](anything)))
},
test("ZClient proxy respond Ok") {
val res =
for {
port <- ZIO.environmentWithZIO[DynamicServer](_.get.port)
url <- ZIO.fromEither(URL.decode(s"http://localhost:$port"))
id <- DynamicServer.deploy(Handler.ok.toHttpApp)
proxy = Proxy.empty.url(url).headers(Headers(DynamicServer.APP_ID, id))
zclient <- ZIO.serviceWith[Client](_.proxy(proxy))
out <- zclient
.request(
Request.get(url = url),
)
.provide(
Scope.default,
)
} yield out
assertZIO(res.either)(isRight)
}.provideSome[DynamicServer](
Client.default,
),
test("proxy respond Ok") {
val res =
for {
Expand Down

0 comments on commit 6411c97

Please sign in to comment.