From 530aa0e51cca1cbf619ad7ed242837a8d6919a13 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Tue, 18 Aug 2020 21:13:42 -0700 Subject: [PATCH] Add a ZIO-style Service type for idiomaticity points 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 ;) --- .../src/main/scala/sttp/client/httpclient/zio/package.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httpclient-backend/zio/src/main/scala/sttp/client/httpclient/zio/package.scala b/httpclient-backend/zio/src/main/scala/sttp/client/httpclient/zio/package.scala index f656e2c647..bb7946d7a0 100644 --- a/httpclient-backend/zio/src/main/scala/sttp/client/httpclient/zio/package.scala +++ b/httpclient-backend/zio/src/main/scala/sttp/client/httpclient/zio/package.scala @@ -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.