Skip to content

Commit

Permalink
Scope Requests (zio#2252)
Browse files Browse the repository at this point in the history
* scope requests

* more scopes

* format

* update documentation
  • Loading branch information
adamgfraser authored Jun 14, 2023
1 parent 1bc6079 commit 8c54426
Show file tree
Hide file tree
Showing 31 changed files with 98 additions and 73 deletions.
2 changes: 1 addition & 1 deletion docs/dsl/body.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ On the client-side, `ZIO-HTTP` models content in `ClientRequest` as `Body` with
To add content while making a request using ZIO HTTP you can use the `Client.request` method:

```scala mdoc:silent
val actual: ZIO[Client, Throwable, Response] =
val actual: ZIO[Client with Scope, Throwable, Response] =
Client.request("https://localhost:8073/success", content = Body.fromString("Some string"))
```

Expand Down
2 changes: 1 addition & 1 deletion docs/dsl/headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ object SimpleClientJson extends ZIOAppDefault {
} yield ()

override def run =
program.provide(Client.default)
program.provide(Client.default, Scope.default)

}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic/http-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object SimpleClient extends ZIOAppDefault {
_ <- Console.printLine(data)
} yield ()

override val run = program.provide(Client.default)
override val run = program.provide(Client.default, Scope.default)

}

Expand Down
1 change: 1 addition & 0 deletions docs/examples/basic/https-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ object HttpsClient extends ZIOAppDefault {
NettyClientDriver.live,
DnsResolver.default,
ZLayer.succeed(NettyConfig.default),
Scope.default,
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object HttpCliApp {
)
.setHeaders(headers),
)
.provide(Client.default)
.provide(Client.default, Scope.default)
_ <- Console.printLine(s"Got response")
_ <- Console.printLine(s"Status: ${response.status}")
body <- response.body.asString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ object AuthenticationClient extends ZIOAppDefault {
_ <- Console.printLine(body)
} yield ()

override val run = program.provide(Client.default)
override val run = program.provide(Client.default, Scope.default)

}
3 changes: 2 additions & 1 deletion zio-http-example/src/main/scala/example/CliExamples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ object TestCliClient extends zio.ZIOAppDefault with TestCliEndpoints {
.provide(
EndpointExecutor.make(serviceName = "test"),
Client.default,
Scope.default,
)

lazy val clientExample: URIO[EndpointExecutor[Unit], Unit] =
lazy val clientExample: URIO[EndpointExecutor[Unit] & Scope, Unit] =
for {
executor <- ZIO.service[EndpointExecutor[Unit]]
_ <- executor(getUser(42, Location.parse("some-location").toOption.get)).debug("result1")
Expand Down
4 changes: 2 additions & 2 deletions zio-http-example/src/main/scala/example/ClientServer.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package example

import zio.{ZIO, ZIOAppDefault}
import zio.{Scope, ZIO, ZIOAppDefault}

import zio.http._

Expand All @@ -16,6 +16,6 @@ object ClientServer extends ZIOAppDefault {
}

val run = {
Server.serve(app.withDefaultErrorResponse).provide(Server.default, Client.default).exitCode
Server.serve(app.withDefaultErrorResponse).provide(Server.default, Client.default, Scope.default).exitCode
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object ClientWithDecompression extends ZIOAppDefault {
Client.live,
ZLayer.succeed(NettyConfig.default),
DnsResolver.default,
Scope.default,
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ object EndpointExamples extends ZIOAppDefault {
val x1 = getUser(42)
val x2 = getUserPosts(42, 200, "adam")

val result1: UIO[Int] = executor(x1)
val result2: UIO[List[String]] = executor(x2)
val result1: ZIO[Scope, Nothing, Int] = executor(x1)
val result2: ZIO[Scope, Nothing, List[String]] = executor(x2)

result1.zip(result2).debug
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ object GracefulShutdown extends ZIOAppDefault {
_ <- Console.printLine(body)
} yield ()).provide(
Client.default,
Scope.default,
)
}
1 change: 1 addition & 0 deletions zio-http-example/src/main/scala/example/HttpsClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object HttpsClient extends ZIOAppDefault {
NettyClientDriver.live,
DnsResolver.default,
ZLayer.succeed(NettyConfig.default),
Scope.default,
)

}
1 change: 1 addition & 0 deletions zio-http-example/src/main/scala/example/Interrupt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object MyClient extends ZIOAppDefault {
ZLayer.succeed(ZClient.Config.default.withFixedConnectionPool(2)),
ZLayer.succeed(NettyConfig.default),
DnsResolver.default,
Scope.default,
)
// .provide(Client.live, NettyClientDriver.fromConfig, ClientConfig.live(ClientConfig().withFixedConnectionPool(2)))
.debug("EXIT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object MultipartFormData extends ZIOAppDefault {
} yield response
}

private def program: ZIO[Client with Server, Throwable, Unit] =
private def program: ZIO[Client with Server with Scope, Throwable, Unit] =
for {
port <- Server.install(app)
_ <- ZIO.logInfo(s"Server started on port $port")
Expand Down Expand Up @@ -70,5 +70,6 @@ object MultipartFormData extends ZIOAppDefault {
.provide(
Server.default,
Client.default,
Scope.default,
)
}
2 changes: 1 addition & 1 deletion zio-http-example/src/main/scala/example/SimpleClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ object SimpleClient extends ZIOAppDefault {
_ <- Console.printLine(data)
} yield ()

override val run = program.provide(Client.default)
override val run = program.provide(Client.default, Scope.default)

}
5 changes: 3 additions & 2 deletions zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object TestServerSpec extends ZIOSpecDefault {
)
} yield assertTrue(response1.status == Status.Ok) &&
assertTrue(response2.status == Status.InternalServerError)
}.provideSome[Client with Driver](
}.provideSome[Client with Driver with Scope](
TestServer.layer,
),
suite("Exact Request=>Response version")(
Expand Down Expand Up @@ -79,13 +79,14 @@ object TestServerSpec extends ZIOSpecDefault {
} yield assertTrue(finalResponse.status == Status.NotFound)
},
)
.provideSome[Client with Driver](
.provideSome[Client with Driver with Scope](
TestServer.layer,
),
).provide(
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
Client.default,
NettyDriver.live,
Scope.default,
)

private def requestToCorrectPort =
Expand Down
Loading

0 comments on commit 8c54426

Please sign in to comment.