Skip to content

Commit

Permalink
ZClient enhancements for URL and path (#3227)
Browse files Browse the repository at this point in the history
* Adding url and path improvements to ZClient

* Adding simple test.
  • Loading branch information
otobrglez authored Dec 3, 2024
1 parent 547975e commit 1f8ef1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 6 additions & 2 deletions zio-http/shared/src/main/scala/zio/http/ZClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1f8ef1f

Please sign in to comment.