Skip to content

Commit

Permalink
add overload withRequest for HttpRcRoutes to create router from req…
Browse files Browse the repository at this point in the history
…uest
  • Loading branch information
cornerman committed Jun 14, 2024
1 parent bc63221 commit 1173daf
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions http4sServer/src/main/scala/HttpRpcRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ object HttpRpcRoutes {
def apply[PickleType: EntityDecoder[F, *]: EntityEncoder[F, *], F[_]: Concurrent](
router: Router[PickleType, F],
onError: PartialFunction[Throwable, F[Response[F]]] = PartialFunction.empty
): HttpRoutes[F] = withRequest[PickleType, F](_ => router, onError)

def withRequest[PickleType: EntityDecoder[F, *]: EntityEncoder[F, *], F[_]: Concurrent](
router: Request[F] => Router[PickleType, F],
onError: PartialFunction[Throwable, F[Response[F]]] = PartialFunction.empty
): HttpRoutes[F] = {
val dsl = Http4sDsl[F]
import dsl._
Expand All @@ -21,7 +26,7 @@ object HttpRpcRoutes {
request.pathInfo.segments match {
case Vector(apiName, methodName) =>
val path = List(apiName.decoded(), methodName.decoded())
val result = router.getFunction(path).traverse { f =>
val result = router(request).getFunction(path).traverse { f =>
request.as[PickleType].flatMap { payload =>
f(payload) match {
case Left(error) => serverFailureToResponse[F](dsl, onError)(error)
Expand All @@ -39,6 +44,11 @@ object HttpRpcRoutes {
def eventStream[F[_]: Concurrent](
router: Router[String, Stream[F, *]],
onError: PartialFunction[Throwable, F[Response[F]]] = PartialFunction.empty
): HttpRoutes[F] = eventStreamWithRequest[F](_ => router, onError)

def eventStreamWithRequest[F[_]: Concurrent](
router: Request[F] => Router[String, Stream[F, *]],
onError: PartialFunction[Throwable, F[Response[F]]] = PartialFunction.empty
): HttpRoutes[F] = {
val dsl = Http4sDsl[F]
import dsl._
Expand All @@ -47,7 +57,7 @@ object HttpRpcRoutes {
request.pathInfo.segments match {
case Vector(apiName, methodName) =>
val path = List(apiName.decoded(), methodName.decoded())
val result = router.getFunction(path).traverse { f =>
val result = router(request).getFunction(path).traverse { f =>
request.as[String].flatMap { payload =>
f(payload) match {
case Left(error) => serverFailureToResponse[F](dsl, onError)(error)
Expand Down

0 comments on commit 1173daf

Please sign in to comment.