From 1f8ef1ff69c303d7e821ee4ef86205e5365fb3fe Mon Sep 17 00:00:00 2001 From: Oto Brglez Date: Tue, 3 Dec 2024 17:57:25 +0100 Subject: [PATCH] ZClient enhancements for URL and path (#3227) * Adding url and path improvements to ZClient * Adding simple test. --- zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala | 10 ++++++++++ zio-http/shared/src/main/scala/zio/http/ZClient.scala | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala index 51695c4c0d..48a0519b74 100644 --- a/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala @@ -120,6 +120,16 @@ object ClientSpec extends RoutesRunnableSpec { app.deploy(Request(headers = Headers(Header.Authorization.Unparsed("", "my-token")))).flatMap(_.body.asString) assertZIO(responseContent)(equalTo("my-token")) } @@ timeout(5.seconds), + test("URL and path manipulation on client level") { + for { + baseURL <- DynamicServer.httpURL + _ <- + Handler.ok.toRoutes.deployAndRequest { c => + (c.updatePath(_ / "my-service") @@ ZClientAspect.requestLogging()).batched.get("/hello") + }.runZIO(()) + loggedUrl <- ZTestLogger.logOutput.map(_.collectFirst { case m => m.annotations("url") }.mkString) + } yield assertTrue(loggedUrl == baseURL + "/my-service/hello") + }, ) override def spec = { 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 014d77cca6..ce6d47b332 100644 --- a/zio-http/shared/src/main/scala/zio/http/ZClient.scala +++ b/zio-http/shared/src/main/scala/zio/http/ZClient.scala @@ -151,8 +151,10 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( def path(path: String): ZClient[Env, ReqEnv, In, Err, Out] = self.path(Path(path)) - def path(path: Path): ZClient[Env, ReqEnv, In, Err, Out] = - copy(url = url.copy(path = path)) + def path(path: Path): ZClient[Env, ReqEnv, In, Err, Out] = updatePath(_ => path) + + def updatePath(f: Path => Path): ZClient[Env, ReqEnv, In, Err, Out] = + copy(url = url.copy(path = f(url.path))) def patch(suffix: String)(implicit ev: Body <:< In, trace: Trace): ZIO[Env & ReqEnv, Err, Out] = request(Method.PATCH, suffix)(ev(Body.empty)) @@ -263,6 +265,8 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( def uri(uri: URI): ZClient[Env, ReqEnv, In, Err, Out] = url(URL.fromURI(uri).getOrElse(URL.empty)) def url(url: URL): ZClient[Env, ReqEnv, In, Err, Out] = copy(url = url) + + def updateURL(f: URL => URL): ZClient[Env, ReqEnv, In, Err, Out] = copy(url = f(url)) } object ZClient extends ZClientPlatformSpecific {