This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class BooksQuery(year: Option[Year], limit: Option[Int]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val baseEndpoint: Endpoint[Unit, (StatusCode, ErrorInfo), Unit, Nothing] = | |
endpoint | |
.in("api" / "v1.0") | |
.errorOut(statusCode.and(jsonBody[ErrorInfo])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val getBooks: Endpoint[(Option[Year], Option[Int]), | |
(StatusCode, ErrorInfo), | |
List[Book], | |
Nothing] = ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.circe.generic.auto._ | |
import tapir._ | |
import tapir.json.circe._ | |
import tapir.model.StatusCode | |
val getBooks = endpoint.get | |
.in("api" / "v1.0" / "books") | |
.in(query[Option[Year]]("year")) | |
.in(query[Option[Int]]("limit")) | |
.errorOut(statusCode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tapir.Codec.PlainCodec | |
implicit val yearCodec: PlainCodec[Year] = | |
implicitly[PlainCodec[Int]].map(new Year(_))(_.year) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Year(val year: Int) extends AnyVal | |
case class Country(name: String) | |
case class Author(name: String, country: Country) | |
case class Book(id: UUID, title: String, year: Year, author: Author) | |
case class ErrorInfo(error: String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val baseEndpoint: Endpoint[Unit, (StatusCode, ErrorInfo), Unit, Nothing] = | |
endpoint | |
.in("api" / "v1.0") | |
.errorOut(statusCode.and(jsonBody[ErrorInfo])) | |
val booksQueryInput: EndpointInput[BooksQuery] = | |
query[Option[Year]]("year") | |
.and(query[Option[Int]]("limit")) | |
.mapTo(BooksQuery) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"com.softwaremill.tapir" %% "tapir-core" % "0.1" | |
"com.softwaremill.tapir" %% "tapir-akka-http-server" % "0.1" | |
"com.softwaremill.tapir" %% "tapir-json-circe" % "0.1" | |
"com.softwaremill.tapir" %% "tapir-openapi-docs" % "0.1" | |
"com.softwaremill.tapir" %% "tapir-openapi-circe-yaml" % "0.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tapir.docs.openapi._ | |
import tapir.openapi.circe.yaml._ | |
val docs: OpenAPI = List(addBook, findBook).toOpenAPI( | |
"The Tapir Library", "1.0") | |
val docsYaml: String = docs.toYaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val bookBody = jsonBody[Book] | |
.description("The book") | |
.example(Book("Pride and Prejudice", "Novel", 1813)) | |
val addBook: Endpoint[(Book, String), Unit, Boolean, Nothing] = | |
endpoint | |
.description("Adds a new book, if the user is authorized") | |
.post | |
.in("book" / "add") | |
.in(bookBody) |