Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the way server logic can be specified #1871

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions core/src/main/scala/sttp/tapir/Endpoint.scala
Original file line number Diff line number Diff line change
@@ -389,6 +389,24 @@ trait EndpointServerLogicOps[A, I, E, O, -R] { outer: Endpoint[A, I, E, O, R] =>
)(implicit eIsThrowable: E <:< Throwable, eClassTag: ClassTag[E], aIsUnit: A =:= Unit): ServerEndpoint.Full[Unit, Unit, I, E, O, R, F] =
ServerEndpoint.public(this.asInstanceOf[Endpoint[Unit, I, E, O, R]], recoverErrors1[I, E, O, F](f))

/** Like [[serverLogic]], but specialised to the case when the error type is `Unit` (e.g. a fixed status code), and the result of the
* logic function is an option. A `None` is then treated as an error response.
*/
def serverLogicOption[F[_]](
f: I => F[Option[O]]
)(implicit aIsUnit: A =:= Unit, eIsUnit: E =:= Unit): ServerEndpoint.Full[Unit, Unit, I, Unit, O, R, F] = {
import sttp.monad.syntax._
ServerEndpoint.public(
this.asInstanceOf[Endpoint[Unit, I, Unit, O, R]],
implicit m =>
i =>
f(i).map {
case None => Left(())
case Some(v) => Right(v)
}
)
}

//

/** Combine this endpoint description with a function, which implements the security logic of the endpoint.
@@ -430,6 +448,24 @@ trait EndpointServerLogicOps[A, I, E, O, -R] { outer: Endpoint[A, I, E, O, R] =>
f: A => F[U]
)(implicit eIsThrowable: E <:< Throwable, eClassTag: ClassTag[E]): PartialServerEndpoint[A, U, I, E, O, R, F] =
PartialServerEndpoint(this, recoverErrors1[A, E, U, F](f))

/** Like [[serverSecurityLogic]], but specialised to the case when the error type is `Unit` (e.g. a fixed status code), and the result of
* the logic function is an option. A `None` is then treated as an error response.
*/
def serverSecurityLogicOption[U, F[_]](
f: A => F[Option[U]]
)(implicit eIsUnit: E =:= Unit): PartialServerEndpoint[A, U, I, Unit, O, R, F] = {
import sttp.monad.syntax._
PartialServerEndpoint(
this.asInstanceOf[Endpoint[A, I, Unit, O, R]],
implicit m =>
a =>
f(a).map {
case None => Left(())
case Some(v) => Right(v)
}
)
}
}

case class EndpointInfo(
13 changes: 13 additions & 0 deletions core/src/main/scala/sttp/tapir/server/PartialServerEndpoint.scala
Original file line number Diff line number Diff line change
@@ -86,4 +86,17 @@ case class PartialServerEndpoint[A, U, I, E, O, -R, F[_]](
f: U => I => F[O]
)(implicit eIsThrowable: E <:< Throwable, eClassTag: ClassTag[E]): ServerEndpoint.Full[A, U, I, E, O, R, F] =
ServerEndpoint(endpoint, securityLogic, recoverErrors2[U, I, E, O, F](f))

def serverLogicOption(f: U => I => F[Option[O]])(implicit eIsUnit: E =:= Unit): ServerEndpoint.Full[A, U, I, Unit, O, R, F] =
ServerEndpoint(
endpoint.asInstanceOf[Endpoint[A, I, Unit, O, R]],
securityLogic.asInstanceOf[MonadError[F] => A => F[Either[Unit, U]]],
implicit m =>
u =>
i =>
f(u)(i).map {
case None => Left(())
case Some(v) => Right(v)
}
)
}