Skip to content

Instantly share code, notes, and snippets.

@sammyrulez
Created June 13, 2019 14:05
Show Gist options
  • Save sammyrulez/6286dd769381b6340f3065ef683941e8 to your computer and use it in GitHub Desktop.
Save sammyrulez/6286dd769381b6340f3065ef683941e8 to your computer and use it in GitHub Desktop.
Tapir endpoint config
sealed trait ErrorInfo
case class NotFound(what: String) extends ErrorInfo
case class Unauthorized(realm: String) extends ErrorInfo
case class Unknown(code: Int, msg: String) extends ErrorInfo
case object NoContent extends ErrorInfo
case class Conflict(what: String) extends ErrorInfo
case class ErrorCodeAndMessage(code: String, message: String)
case class GenericErrorInfo(errors: List[ErrorCodeAndMessage]) extends ErrorInfo
def baseEndpoint(basePath: String): Endpoint[Unit, ErrorInfo, Unit, Nothing] = {
endpoint.errorOut(
oneOf(
statusMapping(StatusCodes.NotFound, jsonBody[NotFound].description("not found")),
statusMapping(StatusCodes.Unauthorized, jsonBody[Unauthorized].description("unauthorized")),
statusMapping(StatusCodes.NoContent, emptyOutput.map(_ => NoContent)(_ => ())),
statusMapping(StatusCodes.BadRequest, jsonBody[GenericErrorInfo]),
statusMapping(StatusCodes.Conflict, jsonBody[Conflict]),
statusDefaultMapping(jsonBody[Unknown].description("unknown")))).in(basePath).asInstanceOf[Endpoint[Unit, ErrorInfo, Unit, Nothing]]
}
val loginEndpoint: Endpoint[UserLogin, ErrorInfo, TokenHolder, Nothing] =
usersBaseEndpoint
.in("login")
.in(loginParams)
.post
.out(jsonBody[TokenHolder])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment