-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation of Endpoint API (#2741)
* update path codec. * rendering path codecs. * attaching docs. * attaching examples. * decoding and formatting path codecs. * endpoint overview. * endpoint with error example. * describing input and outputs. * describing errors. * transform methods. * openapi documentation. * sfix. * description of a book. * description annotation. * sfix. * remove extra layers. * generate endpoint from openapi. * fix basePath for code generator. * generating cli app from endpoints. * update example path. * fmt.
- Loading branch information
Showing
11 changed files
with
886 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
53 changes: 53 additions & 0 deletions
53
zio-http-example/src/main/scala/example/endpoint/BooksEndpointExample.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package example.endpoint | ||
|
||
import zio._ | ||
|
||
import zio.schema.annotation.description | ||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
import zio.http._ | ||
import zio.http.codec.PathCodec._ | ||
import zio.http.codec._ | ||
import zio.http.endpoint._ | ||
import zio.http.endpoint.openapi._ | ||
|
||
object BooksEndpointExample extends ZIOAppDefault { | ||
case class Book( | ||
@description("Title of the book") | ||
title: String, | ||
@description("List of the authors of the book") | ||
authors: List[String], | ||
) | ||
object Book { | ||
implicit val schema: Schema[Book] = DeriveSchema.gen | ||
} | ||
|
||
object BookRepo { | ||
val book1 = Book("Programming in Scala", List("Martin Odersky", "Lex Spoon", "Bill Venners", "Frank Sommers")) | ||
val book2 = Book("Zionomicon", List("John A. De Goes", "Adam Fraser")) | ||
val book3 = Book("Effect-Oriented Programming", List("Bill Frasure", "Bruce Eckel", "James Ward")) | ||
def find(q: String): List[Book] = { | ||
if (q.toLowerCase == "scala") List(book1, book2, book3) | ||
else if (q.toLowerCase == "zio") List(book2, book3) | ||
else List.empty | ||
} | ||
} | ||
|
||
val endpoint = | ||
Endpoint((RoutePattern.GET / "books") ?? Doc.p("Route for querying books")) | ||
.query( | ||
QueryCodec.queryTo[String]("q").examples(("example1", "scala"), ("example2", "zio")) ?? Doc.p( | ||
"Query parameter for searching books", | ||
), | ||
) | ||
.out[List[Book]](Doc.p("List of books matching the query")) ?? Doc.p( | ||
"Endpoint to query books based on a search query", | ||
) | ||
|
||
val booksRoute = endpoint.implement(handler((query: String) => BookRepo.find(query))) | ||
val openAPI = OpenAPIGen.fromEndpoints(title = "Library API", version = "1.0", endpoint) | ||
val swaggerRoutes = SwaggerUI.routes("docs" / "openapi", openAPI) | ||
val routes = Routes(booksRoute) ++ swaggerRoutes | ||
|
||
def run = Server.serve(routes.toHttpApp).provide(Server.default) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package example | ||
package example.endpoint | ||
|
||
import zio._ | ||
import zio.cli._ | ||
|
@@ -13,9 +13,8 @@ import zio.http.endpoint.cli._ | |
import zio.http.endpoint.{Endpoint, EndpointExecutor} | ||
|
||
trait TestCliEndpoints { | ||
import zio.http.codec.PathCodec._ | ||
|
||
import HttpCodec._ | ||
import zio.http.codec.PathCodec._ | ||
final case class User( | ||
@description("The unique identifier of the User") | ||
id: Int, | ||
|
@@ -82,8 +81,8 @@ object TestCliApp extends zio.cli.ZIOCliDefault with TestCliEndpoints { | |
object TestCliServer extends zio.ZIOAppDefault with TestCliEndpoints { | ||
val getUserRoute = | ||
getUser.implement { | ||
Handler.fromFunction { case (id, _) => | ||
User(id, "Juanito", Some("[email protected]")) | ||
Handler.fromFunctionZIO { case (id, _) => | ||
ZIO.succeed(User(id, "Juanito", Some("[email protected]"))).debug("Hello") | ||
} | ||
} | ||
|
||
|
@@ -101,7 +100,7 @@ object TestCliServer extends zio.ZIOAppDefault with TestCliEndpoints { | |
} | ||
} | ||
|
||
val routes = Routes(getUserRoute, getUserPostsRoute, createUserRoute) | ||
val routes = Routes(getUserRoute, getUserPostsRoute, createUserRoute) @@ Middleware.debug | ||
|
||
val run = Server.serve(routes.toHttpApp).provide(Server.default) | ||
} | ||
|
49 changes: 49 additions & 0 deletions
49
zio-http-example/src/main/scala/example/endpoint/EndpointWithError.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package example.endpoint | ||
|
||
import zio._ | ||
|
||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
import zio.http._ | ||
import zio.http.codec.PathCodec | ||
import zio.http.endpoint.Endpoint | ||
import zio.http.endpoint.EndpointMiddleware.None | ||
|
||
object EndpointWithError extends ZIOAppDefault { | ||
|
||
case class Book(title: String, authors: List[String]) | ||
|
||
object Book { | ||
implicit val schema: Schema[Book] = DeriveSchema.gen | ||
} | ||
case class NotFoundError(error: String, message: String) | ||
|
||
object NotFoundError { | ||
implicit val schema: Schema[NotFoundError] = DeriveSchema.gen | ||
} | ||
|
||
object BookRepo { | ||
def find(id: Int): ZIO[Any, String, Book] = { | ||
if (id == 1) | ||
ZIO.succeed(Book("Zionomicon", List("John A. De Goes", "Adam Fraser"))) | ||
else | ||
ZIO.fail("Not found") | ||
} | ||
} | ||
|
||
val endpoint: Endpoint[Int, Int, NotFoundError, Book, None] = | ||
Endpoint(RoutePattern.GET / "books" / PathCodec.int("id")) | ||
.out[Book] | ||
.outError[NotFoundError](Status.NotFound) | ||
|
||
val getBookHandler: Handler[Any, NotFoundError, Int, Book] = | ||
handler { (id: Int) => | ||
BookRepo | ||
.find(id) | ||
.mapError(err => NotFoundError(err, "The requested book was not found. Please try using a different ID.")) | ||
} | ||
|
||
val app = endpoint.implement(getBookHandler).toHttpApp @@ Middleware.debug | ||
|
||
def run = Server.serve(app).provide(Server.default) | ||
} |
62 changes: 62 additions & 0 deletions
62
zio-http-example/src/main/scala/example/endpoint/EndpointWithMultipleErrorsUsingEither.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package example.endpoint | ||
|
||
import zio._ | ||
|
||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
import zio.http._ | ||
import zio.http.codec.{HeaderCodec, PathCodec} | ||
import zio.http.endpoint.Endpoint | ||
import zio.http.endpoint.EndpointMiddleware.None | ||
|
||
object EndpointWithMultipleErrorsUsingEither extends ZIOAppDefault { | ||
|
||
case class Book(title: String, authors: List[String]) | ||
|
||
object Book { | ||
implicit val schema: Schema[Book] = DeriveSchema.gen | ||
} | ||
|
||
case class BookNotFound(message: String, bookId: Int) | ||
|
||
object BookNotFound { | ||
implicit val schema: Schema[BookNotFound] = DeriveSchema.gen | ||
} | ||
|
||
case class AuthenticationError(message: String, userId: Int) | ||
|
||
object AuthenticationError { | ||
implicit val schema: Schema[AuthenticationError] = DeriveSchema.gen | ||
} | ||
|
||
object BookRepo { | ||
def find(id: Int): ZIO[Any, BookNotFound, Book] = { | ||
if (id == 1) | ||
ZIO.succeed(Book("Zionomicon", List("John A. De Goes", "Adam Fraser"))) | ||
else | ||
ZIO.fail(BookNotFound("The requested book was not found.", id)) | ||
} | ||
} | ||
|
||
val endpoint: Endpoint[Int, (Int, Header.Authorization), Either[BookNotFound, AuthenticationError], Book, None] = | ||
Endpoint(RoutePattern.GET / "books" / PathCodec.int("id")) | ||
.header(HeaderCodec.authorization) | ||
.out[Book] | ||
.outError[BookNotFound](Status.NotFound) | ||
.outError[AuthenticationError](Status.Unauthorized) | ||
|
||
def isUserAuthorized(authHeader: Header.Authorization) = false | ||
|
||
val getBookHandler | ||
: Handler[Any, Either[BookNotFound, AuthenticationError], (RuntimeFlags, Header.Authorization), Book] = | ||
handler { (id: Int, authHeader: Header.Authorization) => | ||
if (isUserAuthorized(authHeader)) | ||
BookRepo.find(id).mapError(Left(_)) | ||
else | ||
ZIO.fail(Right(AuthenticationError("User is not authenticated", 123))) | ||
} | ||
|
||
val app = endpoint.implement(getBookHandler).toHttpApp @@ Middleware.debug | ||
|
||
def run = Server.serve(app).provide(Server.default) | ||
} |
65 changes: 65 additions & 0 deletions
65
zio-http-example/src/main/scala/example/endpoint/EndpointWithMultipleUnifiedErrors.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package example.endpoint | ||
|
||
import zio._ | ||
|
||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
import zio.http._ | ||
import zio.http.codec.{HeaderCodec, HttpCodec, PathCodec} | ||
import zio.http.endpoint.Endpoint | ||
import zio.http.endpoint.EndpointMiddleware.None | ||
|
||
object EndpointWithMultipleUnifiedErrors extends ZIOAppDefault { | ||
|
||
case class Book(title: String, authors: List[String]) | ||
|
||
object Book { | ||
implicit val schema: Schema[Book] = DeriveSchema.gen | ||
} | ||
|
||
abstract class AppError(message: String) | ||
|
||
case class BookNotFound(message: String, bookId: Int) extends AppError(message) | ||
|
||
object BookNotFound { | ||
implicit val schema: Schema[BookNotFound] = DeriveSchema.gen | ||
} | ||
|
||
case class AuthenticationError(message: String, userId: Int) extends AppError(message) | ||
|
||
object AuthenticationError { | ||
implicit val schema: Schema[AuthenticationError] = DeriveSchema.gen | ||
} | ||
|
||
object BookRepo { | ||
def find(id: Int): ZIO[Any, BookNotFound, Book] = { | ||
if (id == 1) | ||
ZIO.succeed(Book("Zionomicon", List("John A. De Goes", "Adam Fraser"))) | ||
else | ||
ZIO.fail(BookNotFound("The requested book was not found.", id)) | ||
} | ||
} | ||
|
||
val endpoint: Endpoint[Int, (Int, Header.Authorization), AppError, Book, None] = | ||
Endpoint(RoutePattern.GET / "books" / PathCodec.int("id")) | ||
.header(HeaderCodec.authorization) | ||
.out[Book] | ||
.outErrors[AppError]( | ||
HttpCodec.error[BookNotFound](Status.NotFound), | ||
HttpCodec.error[AuthenticationError](Status.Unauthorized), | ||
) | ||
|
||
def isUserAuthorized(authHeader: Header.Authorization) = false | ||
|
||
val getBookHandler: Handler[Any, AppError, (Int, Header.Authorization), Book] = | ||
handler { (id: Int, authHeader: Header.Authorization) => | ||
if (isUserAuthorized(authHeader)) | ||
BookRepo.find(id) | ||
else | ||
ZIO.fail(AuthenticationError("User is not authenticated", 123)) | ||
} | ||
|
||
val app = endpoint.implement(getBookHandler).toHttpApp @@ Middleware.debug | ||
|
||
def run = Server.serve(app).provide(Server.default) | ||
} |
Oops, something went wrong.