Skip to content

fullfacing/akkaMonixSttpBackend

Repository files navigation

CircleCI Maven Central Scala Steward badge

akkaMonixSttpBackend

Introduction:
akkaMonixSttpBackend is a backend for sttp using Akka-HTTP to handle requests, and with Task as the response Monad and Observable as the streaming type. It is a modification of the Akka-HTTP backend provided by sttp, with instances of Futures deferred to Tasks and Akka-Streams Sources converted to Observables.

The motivation behind creating this backend as opposed to using the existing Monix wrapped async-http-client backend is to give an alternative for projects that already have Akka-HTTP as a dependency, removing the need for the async-http-client dependency as well.

Installation:
Add the following sbt dependency:
"com.fullfacing" %% "sttp-akka-monix-task" % "1.5.0"

Usage:
Usage is identical to the Akka-HTTP backend with only the response type differing:

import akka.util.ByteString
import com.fullfacing.akka.monix.task.backend.AkkaMonixHttpBackend
import sttp.client.{Response, SttpBackend, _}
import monix.eval.Task
import monix.reactive.Observable

implicit val backend: SttpBackend[Task, Observable[ByteString], NothingT] = AkkaMonixHttpBackend()

// To set the request body as a stream:
val observable: Observable[ByteString] = ???

sttp
  .streamBody(observable)
  .post(uri"...")
  .send()

// To receive the response body as a stream:
val response: Task[Response[Observable[ByteString]]] =
  sttp
    .post(uri"...")
    .response(asStream[Observable[ByteString]])
    .send()