-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added monix.Observable implementation
This is basically a conversion to/from the internal fs2.Stream.
- Loading branch information
Laurence Lavigne
committed
Apr 24, 2018
1 parent
bb9f8a4
commit 3666518
Showing
8 changed files
with
314 additions
and
95 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
65 changes: 0 additions & 65 deletions
65
modules/http/server/src/test/scala/GreeterRestClient.scala
This file was deleted.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
modules/http/server/src/test/scala/GreeterRestClients.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,108 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.rpc.http | ||
|
||
import cats.effect._ | ||
import fs2.Stream | ||
import io.circe.generic.auto._ | ||
import io.circe.syntax._ | ||
import org.http4s._ | ||
import org.http4s.circe._ | ||
import org.http4s.client._ | ||
import org.http4s.dsl.io._ | ||
|
||
class UnaryGreeterRestClient[F[_]: Effect](uri: Uri) { | ||
|
||
private implicit val responseDecoder: EntityDecoder[F, HelloResponse] = jsonOf[F, HelloResponse] | ||
|
||
def getHello()(implicit client: Client[F]): F[HelloResponse] = { | ||
val request = Request[F](Method.GET, uri / "getHello") | ||
client.expect[HelloResponse](request) | ||
} | ||
|
||
def sayHello(arg: HelloRequest)(implicit client: Client[F]): F[HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHello") | ||
client.expect[HelloResponse](request.withBody(arg.asJson)) | ||
} | ||
|
||
} | ||
|
||
class Fs2GreeterRestClient[F[_]: Effect](uri: Uri) { | ||
|
||
import freestyle.rpc.http.RestClient._ | ||
|
||
private implicit val responseDecoder: EntityDecoder[F, HelloResponse] = jsonOf[F, HelloResponse] | ||
|
||
def sayHellos(arg: Stream[F, HelloRequest])(implicit client: Client[F]): F[HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHellos") | ||
client.expect[HelloResponse](request.withBody(arg.map(_.asJson))) | ||
} | ||
|
||
def sayHelloAll(arg: HelloRequest)(implicit client: Client[F]): Stream[F, HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHelloAll") | ||
client.streaming(request.withBody(arg.asJson))(_.asStream[HelloResponse]) | ||
} | ||
|
||
def sayHellosAll(arg: Stream[F, HelloRequest])( | ||
implicit client: Client[F]): Stream[F, HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHellosAll") | ||
client.streaming(request.withBody(arg.map(_.asJson)))(_.asStream[HelloResponse]) | ||
} | ||
|
||
} | ||
|
||
class MonixGreeterRestClient[F[_]: Effect](uri: Uri)(implicit sc: monix.execution.Scheduler) { | ||
|
||
import freestyle.rpc.http.RestClient._ | ||
import freestyle.rpc.http.Util._ | ||
import monix.reactive.Observable | ||
|
||
private implicit val responseDecoder: EntityDecoder[F, HelloResponse] = jsonOf[F, HelloResponse] | ||
|
||
def sayHellos(arg: Observable[HelloRequest])(implicit client: Client[F]): F[HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHellos") | ||
client.expect[HelloResponse](request.withBody(arg.toStream.map(_.asJson))) | ||
} | ||
|
||
def sayHelloAll(arg: HelloRequest)(implicit client: Client[F]): Observable[HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHelloAll") | ||
client.streaming(request.withBody(arg.asJson))(_.asStream[HelloResponse]).toObservable | ||
} | ||
|
||
def sayHellosAll(arg: Observable[HelloRequest])( | ||
implicit client: Client[F]): Observable[HelloResponse] = { | ||
val request = Request[F](Method.POST, uri / "sayHellosAll") | ||
client | ||
.streaming(request.withBody(arg.toStream.map(_.asJson)))(_.asStream[HelloResponse]) | ||
.toObservable | ||
} | ||
|
||
} | ||
|
||
object RestClient { | ||
|
||
import io.circe.Decoder | ||
import io.circe.jawn.CirceSupportParser.facade | ||
import jawnfs2._ | ||
|
||
implicit class ResponseOps[F[_]](response: Response[F]) { | ||
|
||
def asStream[A](implicit decoder: Decoder[A]): Stream[F, A] = | ||
if (response.status.code != 200) throw UnexpectedStatus(response.status) | ||
else response.body.chunks.parseJsonStream.map(_.as[A]).rethrow | ||
} | ||
} |
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
Oops, something went wrong.