Skip to content

Commit

Permalink
Remove Blocking from httpclient-zio completely
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Sep 29, 2020
1 parent 2406318 commit f37371d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 52 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ lazy val httpClientFs2Backend =
.dependsOn(fs2 % compileAndTest)

lazy val httpClientZioBackend =
httpClientBackendProject("zio", includeDotty = true)
httpClientBackendProject("zio", includeDotty = false)
.settings(
libraryDependencies ++=
Seq(
Expand Down
6 changes: 3 additions & 3 deletions docs/backends/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Class Effect type Supported stre
==================================== ============================ ================================================= ========================== ===================
``HttpURLConnectionBackend`` None (``Identity``) n/a no no
``TryHttpURLConnectionBackend`` ``scala.util.Try`` n/a no no
``AkkaHttpBackend`` ``scala.concurrent.Future`` ``akka.stream.scaladsl.Source[ByteString, Any]`` yes (regular & streaming) no
``AkkaHttpBackend`` ``scala.concurrent.Future`` ``akka.stream.scaladsl.Source[ByteString, Any]`` yes (regular & streaming) yes
``AsyncHttpClientFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular) no
``AsyncHttpClientScalazBackend`` ``scalaz.concurrent.Task`` n/a yes (regular) no
``AsyncHttpClientZioBackend`` ``zio.Task`` ``zio.stream.Stream[Throwable, Byte]`` yes (regular & streaming) no
Expand All @@ -39,9 +39,9 @@ Class Effect type Supported stre
``HttpClientFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular) no
``HttpClientMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming) no
``HttpClientFs2Backend`` ``F[_]: cats.effect.Async`` ``fs2.Stream[F, Byte]`` yes (regular & streaming) yes

This comment has been minimized.

Copy link
@ghostbuster91

ghostbuster91 Sep 29, 2020

Contributor

This isn't true as well. Currently we require Concurrent and ContextShift instances for a given F[_]

This comment has been minimized.

Copy link
@adamw

adamw Sep 29, 2020

Author Member

What isn't true?

This comment has been minimized.

Copy link
@ghostbuster91

ghostbuster91 Sep 29, 2020

Contributor

It isn't true that for example F[_] in HttpClientFs2Backend requires an instance of Async typeclass. It requires Concurrent + ContextShift which I think are not the same things.

This comment has been minimized.

Copy link
@adamw

adamw Sep 30, 2020

Author Member

True, though that's only a hint as what kind of types can be used there, not necessarily comprehensive documentation. Would just take precious space :)

``HttpClientZioBackend`` ``zio.RIO[Blocking, *]`` ``zio.stream.ZStream[Blocking, Throwable, Byte]`` yes (regular & streaming) yes
``HttpClientZioBackend`` ``zio.Task`` ``zio.stream.Stream[Throwable, Byte]`` yes (regular & streaming) yes
``FinagleBackend`` ``com.twitter.util.Future`` n/a no no
==================================== ============================ ================================================= ==========================
==================================== ============================ ================================================= ========================== ===================
```

The backends work with Scala 2.11, 2.12 and 2.13 (with some exceptions for 2.11). Moreover, `HttpURLConnectionBackend`, `AsyncHttpClientFutureBackend`, `AsyncHttpClientZioBackend`, `HttpClientSyncBackend`, `HttpClientFutureBackend` and `HttpClientZioBackend` are additionally built with Dotty (Scala 3).
Expand Down
2 changes: 1 addition & 1 deletion docs/backends/zio.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type SttpClient = Has[SttpBackend[Task, ZioStreams with WebSockets]]
// or, when using Java 11 & HttpClient

package sttp.client3.httpclient.zio
type SttpClient = Has[SttpBackend[BlockingTask, BlockingZioStreams with WebSockets]]
type SttpClient = Has[SttpBackend[Task, ZioStreams with WebSockets]]
```

The lifecycle of the `SttpClient` service is described by `ZLayer`s, which can be created using the `.layer`/`.layerUsingConfig`/... methods on `AsyncHttpClientZioBackend` / `HttpClientZioBackend`.
Expand Down
45 changes: 23 additions & 22 deletions generated-docs/out/backends/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,29 @@ Each backend has two type parameters:
Below is a summary of all the JVM backends; see the sections on individual backend implementations for more information:

```eval_rst
==================================== ============================ ================================================= ==========================
Class Effect type Supported stream type Supports websockets
==================================== ============================ ================================================= ==========================
``HttpURLConnectionBackend`` None (``Identity``) n/a no
``TryHttpURLConnectionBackend`` ``scala.util.Try`` n/a no
``AkkaHttpBackend`` ``scala.concurrent.Future`` ``akka.stream.scaladsl.Source[ByteString, Any]`` yes (regular & streaming)
``AsyncHttpClientFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular)
``AsyncHttpClientScalazBackend`` ``scalaz.concurrent.Task`` n/a yes (regular)
``AsyncHttpClientZioBackend`` ``zio.Task`` ``zio.stream.Stream[Throwable, Byte]`` yes (regular & streaming)
``AsyncHttpClientMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming)
``AsyncHttpClientCatsBackend`` ``F[_]: cats.effect.Async`` n/a no
``AsyncHttpClientFs2Backend`` ``F[_]: cats.effect.Async`` ``fs2.Stream[F, Byte]`` yes (regular & streaming)
``OkHttpSyncBackend`` None (``Identity``) n/a yes (regular)
``OkHttpFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular)
``OkHttpMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming)
``Http4sBackend`` ``F[_]: cats.effect.Effect`` ``fs2.Stream[F, Byte]`` no
``HttpClientSyncBackend`` None (``Identity``) n/a no
``HttpClientFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular)
``HttpClientMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming)
``HttpClientZioBackend`` ``zio.RIO[Blocking, *]`` ``zio.stream.ZStream[Blocking, Throwable, Byte]`` yes (regular & streaming)
``FinagleBackend`` ``com.twitter.util.Future`` n/a no
==================================== ============================ ================================================= ==========================
==================================== ============================ ================================================= ========================== ===================
Class Effect type Supported stream type Supports websockets Fully non-blocking
==================================== ============================ ================================================= ========================== ===================
``HttpURLConnectionBackend`` None (``Identity``) n/a no no
``TryHttpURLConnectionBackend`` ``scala.util.Try`` n/a no no
``AkkaHttpBackend`` ``scala.concurrent.Future`` ``akka.stream.scaladsl.Source[ByteString, Any]`` yes (regular & streaming) yes
``AsyncHttpClientFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular) no
``AsyncHttpClientScalazBackend`` ``scalaz.concurrent.Task`` n/a yes (regular) no
``AsyncHttpClientZioBackend`` ``zio.Task`` ``zio.stream.Stream[Throwable, Byte]`` yes (regular & streaming) no
``AsyncHttpClientMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming) no
``AsyncHttpClientCatsBackend`` ``F[_]: cats.effect.Async`` n/a no no
``AsyncHttpClientFs2Backend`` ``F[_]: cats.effect.Async`` ``fs2.Stream[F, Byte]`` yes (regular & streaming) no
``OkHttpSyncBackend`` None (``Identity``) n/a yes (regular) no
``OkHttpFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular) no
``OkHttpMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming) no
``Http4sBackend`` ``F[_]: cats.effect.Effect`` ``fs2.Stream[F, Byte]`` no no
``HttpClientSyncBackend`` None (``Identity``) n/a no no
``HttpClientFutureBackend`` ``scala.concurrent.Future`` n/a yes (regular) no
``HttpClientMonixBackend`` ``monix.eval.Task`` ``monix.reactive.Observable[ByteBuffer]`` yes (regular & streaming) no
``HttpClientFs2Backend`` ``F[_]: cats.effect.Async`` ``fs2.Stream[F, Byte]`` yes (regular & streaming) yes
``HttpClientZioBackend`` ``zio.Task`` ``zio.stream.Stream[Throwable, Byte]`` yes (regular & streaming) yes
``FinagleBackend`` ``com.twitter.util.Future`` n/a no no
==================================== ============================ ================================================= ========================== ===================
```

The backends work with Scala 2.11, 2.12 and 2.13 (with some exceptions for 2.11). Moreover, `HttpURLConnectionBackend`, `AsyncHttpClientFutureBackend`, `AsyncHttpClientZioBackend`, `HttpClientSyncBackend`, `HttpClientFutureBackend` and `HttpClientZioBackend` are additionally built with Dotty (Scala 3).
Expand Down
2 changes: 1 addition & 1 deletion generated-docs/out/backends/zio.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type SttpClient = Has[SttpBackend[Task, ZioStreams with WebSockets]]
// or, when using Java 11 & HttpClient

package sttp.client3.httpclient.zio
type SttpClient = Has[SttpBackend[BlockingTask, BlockingZioStreams with WebSockets]]
type SttpClient = Has[SttpBackend[Task, ZioStreams with WebSockets]]
```

The lifecycle of the `SttpClient` service is described by `ZLayer`s, which can be created using the `.layer`/`.layerUsingConfig`/... methods on `AsyncHttpClientZioBackend` / `HttpClientZioBackend`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class HttpClientZioHttpTest extends HttpTest[Task] with ZioTestBase {

"compile" - {
"SttpClient usage" in {
import _root_.zio.blocking._
val request = basicRequest.post(uri"http://example.com").body("hello")
send(request).provideLayer(Blocking.live >+> HttpClientZioBackend.layer())
send(request).provideLayer(HttpClientZioBackend.layer())
succeed
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package sttp.client3.impl.zio

import sttp.client3.testing.ConvertToFuture
import zio.{Exit, Runtime, Task, ZEnv, ZIO}
import zio.blocking.Blocking
import zio.{Exit, Runtime, Task, ZEnv}

import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success}

trait ZioTestBase {
val runtime: Runtime[ZEnv] = Runtime.default

type BlockingTask[A] = ZIO[Blocking, Throwable, A]

val convertZioTaskToFuture: ConvertToFuture[Task] = new ConvertToFuture[Task] {
override def toFuture[T](value: Task[T]): Future[T] = {
val p = Promise[T]()
Expand All @@ -29,22 +26,4 @@ trait ZioTestBase {
p.future
}
}

val convertZioBlockingTaskToFuture: ConvertToFuture[BlockingTask] = new ConvertToFuture[BlockingTask] {
override def toFuture[T](value: BlockingTask[T]): Future[T] = {
val p = Promise[T]()

runtime.unsafeRunSync(value) match {
case Exit.Failure(c) =>
p.complete(
Failure(
c.failures.headOption.orElse(c.defects.headOption).getOrElse(new RuntimeException(s"Unknown cause: $c"))
)
)
case Exit.Success(v) => p.complete(Success(v))
}

p.future
}
}
}

0 comments on commit f37371d

Please sign in to comment.