cornerman / sloth

Type safe RPC in scala
MIT License
96 stars 11 forks source link

integration package for http4s #242

Closed cornerman closed 5 months ago

cornerman commented 5 months ago

Use with:

libraryDependencies += "com.github.cornerman" %%% "sloth-http4s-server" % "0.7.1"
libraryDependencies += "com.github.cornerman" %%% "sloth-http4s-client" % "0.7.1"

On the server:

import sloth.Router
import sloth.ext.http4s.server.HttpRpcRoutes

// for usual rpc
val router = Router[String, IO]
val rpcRoutes: HttpRoutes[IO] = HttpRpcRoutes[String, IO](router)

// for server sent event over rpc
val router = Router[String, fs2.Stream[IO, *]]
val rpcRoutes: HttpRoutes[IO] = HttpRpcRoutes.eventStream[IO](router)

In the client:

import sloth.Client
import sloth.ext.http4s.client.HttpRpcTransport

// for usual rpc
val client = Client[String, IO](HttpRpcTransport[String, IO])
val api: MyApi[IO] = client.wire[MyApi[IO]]

// for server sent events over rpc
val client = Client[String, fs2.Stream[IO, *]](HttpRpcTransport.eventStream[IO])
val api: MyApi[fs2.Stream[IO, *]] = client.wire[MyApi[fs2.Stream[IO, *]]]

See #21