-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
2,391 additions
and
1,921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Overview | ||
|
||
This module provides interop with the [FS2](https://github.com/functional-streams-for-scala/fs2) library. | ||
|
||
|
||
# Getting Started | ||
|
||
This module requires fs2, fs-reactive-streams on the classpath. | ||
|
||
``` | ||
val fs2Version = ??? // any recent one should work | ||
libraryDependencies ++= Seq( | ||
"co.fs2" %% "fs2-core" % fs2Version, | ||
"co.fs2" %% "fs2-reactive-streams" % fs2Version, | ||
"be.wegenenverkeer" %% "rxhttp-fs2" % "2.0-RC1") | ||
``` | ||
|
||
This will pull the `be.wegenenverkeer.rxhttpClient` package in as a transitive dependency. | ||
|
||
# API | ||
|
||
This module provides a Streaming API `FSHttpApi`: | ||
|
||
``` | ||
trait Fs2HttpApi { | ||
def stream[F[_] : ConcurrentEffect](request: ClientRequest): Stream[F, ServerResponseElement] | ||
def streamDechunked[F[_] : ConcurrentEffect](request: ClientRequest, separator: String, charset: Charset): Stream[F, String] | ||
def streamDechunked[F[_] : ConcurrentEffect](request: ClientRequest, separator: String): Stream[F, String] | ||
def stream[F[_] : ConcurrentEffect, A](req: ClientRequest, transform: Array[Byte] => A): Stream[F, A] | ||
def execute[F[_] : Async, A](req: ClientRequest, tr: ServerResponse => A): F[A] | ||
def requestBuilder: ClientRequestBuilder | ||
def requestSigners: util.List[RequestSigner] | ||
} | ||
``` | ||
|
||
After importing `fs2.Implicits._` a RxJavaHttpClient can be converted into an implementation | ||
of this trait. | ||
|
||
|
||
``` | ||
val client : RxJavaHttpClient = ??? | ||
val response = client.fs2HttpApi.stream[IO, String](request, b => new String(b)) | ||
val output = response.compile.toVector.unsafeRunSync() | ||
``` | ||
|
||
# Usage | ||
|
||
Here is an example that gets the response elements (chunks) in a `fs2.Stream[IO, String]`. Obviously, | ||
the request will only fire when the effect is run. | ||
|
||
``` | ||
import scala.concurrent.ExecutionContext | ||
implicit val contextShift: ContextShift[IO] = IO.contextShift(ExecutionContext.global) | ||
import be.wegenenverkeer.rxhttp.fs2.Implicits._ | ||
val client : RxJavaHttpClient = ??? | ||
val response = client.fs2HttpApi.stream[IO, String](request, b => new String(b)) | ||
``` | ||
|
||
We can also return the complete response as a single value wrapped in an `IO`. | ||
|
||
``` | ||
val resp = client.fs2HttpApi.execute[IO, String](request, sr => sr.getResponseBody) | ||
//resp : IO[String] | ||
``` | ||
|
||
|
||
|
29 changes: 29 additions & 0 deletions
29
modules/fs2/src/main/scala/be/wegenenverkeer/rxhttp/fs2/Fs2HttpApi.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package be.wegenenverkeer.rxhttp.fs2 | ||
|
||
import java.nio.charset.Charset | ||
import java.util | ||
|
||
import be.wegenenverkeer.rxhttp._ | ||
import cats.effect.{Async, ConcurrentEffect} | ||
import _root_.fs2.Stream | ||
|
||
/** | ||
* Created by Karel Maesen, Geovise BVBA on 20/04/2020. | ||
*/ | ||
trait Fs2HttpApi { | ||
|
||
def stream[F[_] : ConcurrentEffect](request: ClientRequest): Stream[F, ServerResponseElement] | ||
|
||
def streamDechunked[F[_] : ConcurrentEffect](request: ClientRequest, separator: String, charset: Charset): Stream[F, String] | ||
|
||
def streamDechunked[F[_] : ConcurrentEffect](request: ClientRequest, separator: String): Stream[F, String] | ||
|
||
def stream[F[_] : ConcurrentEffect, A](req: ClientRequest, transform: Array[Byte] => A): Stream[F, A] | ||
|
||
def execute[F[_] : Async, A](req: ClientRequest, tr: ServerResponse => A): F[A] | ||
|
||
def requestBuilder: ClientRequestBuilder | ||
|
||
def requestSigners: util.List[RequestSigner] | ||
|
||
} |
Oops, something went wrong.