Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active May 18, 2020 08:43
Show Gist options
  • Save adamw/ef9ee2f55edd7c4f876cb2b22c6476f2 to your computer and use it in GitHub Desktop.
Save adamw/ef9ee2f55edd7c4f876cb2b22c6476f2 to your computer and use it in GitHub Desktop.
object Server {
import Endpoints._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
// should fail with Error if user not found
def authorize(authToken: AuthToken): Future[Either[Error, User]] = ???
def findPetForUser(user: User, id: UUID): Future[Either[Error, Option[Pet]]] = ???
def addPetToUser(user: User, pet: Pet): Future[Either[Error, Unit]] = ???
val getPetWithLogic = getPet.serverLogicPart(authorize).andThen {
case (user, id) =>
findPetForUser(user, id).map {
case Right(Some(pet)) => Right(pet)
case Right(None) => Left(Error("Not found", StatusCode.NotFound))
case Left(error) => Left(error)
}
}
val addPetWithLogic = addPet.serverLogicPart(authorize)
.andThen((addPetToUser _).tupled)
// the endpoints are now interpreted as an akka.http.scaladsl.Route
import akka.http.scaladsl.server.Route
import sttp.tapir.server.akkahttp._
val routes: Route = List(getPetWithLogic, addPetWithLogic).toRoute
// expose routes using akka-http
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment