Skip to content

Commit

Permalink
Add a ZIO-style Service type for idiomaticity points
Browse files Browse the repository at this point in the history
There's usually a `Service` trait nested within the objects which would define the interface (`send` and `openWebsocket` here).
In lieu of that refactoring, it could be nice to expose a `Service` type alias such that it could be easily used when relying on SttpClient in ones own ZLayers.

Then it would be trivial to refactor from 
```scala
  def live(token: String): ZLayer[Console with SttpClient, Nothing, SlackClient] =
    ZLayer
      .fromServices[Console.Service, SttpBackend[Task, Stream[Throwable, Byte], WebSocketHandler], SlackClient.Service](
        (console: Console.Service, sttp: SttpBackend[Task, Stream[Throwable, Byte], WebSocketHandler]) =>
          LiveSlackClient(console, sttp, token)
      )
```
to
```scala
  def live(token: String): ZLayer[Console with SttpClient, Nothing, SlackClient] =
    ZLayer
      .fromServices[Console.Service, SttpClient.Service, SlackClient.Service](
        (console: Console.Service, sttp: SttpClient.Service) =>
          LiveSlackClient(console, sttp, token)
      )
```

Ahh, much better ;)
  • Loading branch information
kitlangton authored Aug 19, 2020
1 parent e885279 commit 530aa0e
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ package object zio {
/**
* ZIO-environment service definition, which is an SttpBackend.
*/
type SttpClient = Has[SttpBackend[BlockingTask, ZStream[Blocking, Throwable, Byte], WebSocketHandler]]
type SttpClient = Has[SttpClient.Service]

object SttpClient {

type Service = SttpBackend[BlockingTask, ZStream[Blocking, Throwable, Byte], WebSocketHandler]

/**
* Sends the request. Only requests for which the method & URI are specified can be sent.
Expand Down

0 comments on commit 530aa0e

Please sign in to comment.