cats-effect backend

The Cats Effect backend is asynchronous. It can be created for any type implementing the cats.effect.Concurrent typeclass, such as cats.effect.IO. Sending a request is a non-blocking, lazily-evaluated operation and results in a wrapped response. There’s a transitive dependency on cats-effect.

To use, add the following dependency to your project:

"com.softwaremill.sttp.client" %% "async-http-client-backend-cats" % "2.0.3"

This backend depends on async-http-client, uses Netty behind the scenes and supports effect cancellation.

Alternatively, the http4s backend can also be created for a type implementing the cats-effect’s Effect typeclass, and supports streaming as in fs2.

Next you’ll need to define a backend instance as an implicit value. This can be done in two basic ways:

  • by creating an effect, which describes how the backend is created, or instantiating the backend directly. In this case, you’ll need to close the backend manually
  • by creating a Resource, which will instantiate the backend and close it after it has been used

A non-comprehensive summary of how the backend can be created is as follows:

import sttp.client.asynchttpclient.cats.AsyncHttpClientCatsBackend

AsyncHttpClientCatsBackend().flatMap { implicit backend => ... }

// or, if you'd like to use custom configuration:
AsyncHttpClientCatsBackend.usingConfig(asyncHttpClientConfig).flatMap { implicit backend => ... }

// or, if you'd like to use adjust the configuration sttp creates:
AsyncHttpClientCatsBackend.usingConfigBuilder(adjustFunction, sttpOptions).flatMap { implicit backend => ... }

// or, if you'd like the backend to be wrapped in cats-effect Resource:
AsyncHttpClientCatsBackend.resource().use { implicit backend => ... }

// or, if you'd like to instantiate the AsyncHttpClient yourself:
implicit val sttpBackend = AsyncHttpClientCatsBackend.usingClient(asyncHttpClient)

Streaming

This backend doesn’t support non-blocking streaming.

Websockets

The backend supports websockets by wrapping a low-level Java interface, sttp.client.asynchttpclient.WebSocketHandler.