Finch is a library for building composable REST APIs on top of Finagle. Finagle-OAuth2 provides asynchronous OAuth2 support for Finagle services. Together, Finch and Finagle-OAuth2 allow building purely functional REST APIs with OAuth2 authentication in Scala. Key features include request readers for parameter parsing and validation, response builders, and composable endpoint routing.
2. Finagle-OAuth2: Highlights
? Asynchronous version of Nulab¡¯s scala-oauth2-provider
!
? Finagle-friendly API: OAuth2Request, OAuth2Filter
!
? Does not depend on Finch
2
5. 1 import com.twitter.finagle.oauth2._
2 import com.twitter.finagle.oauth2.OAuth2Endpoint
3
4 val tokens: Service[Request, Response] =
5 new OAuth2Endpoint(dataHandler) with OAuthErrorInJson with OAuthTokenInJson
Finagle-OAuth2: Step #3
Issue Acces Token
5
6. Thin layer of purely-functional basic blocks
on top of Finagle for building composable
REST APIs
Finch
https://github.com/?nagle/?nch
6
7. Finch: Highlights
? Was #1 Scala trending repo on GitHub for 95 mins
!
!
!
!
!
!
!
? Happily used in production by 2 customers:
? Konfettin
? JusBrasil
!
? Super simple & lightweight (~ 1.5k SLOC)
7
8. Finch: Quickstart
1 def hello(name: String) = new Service[HttpRequest, HttpResponse] = {
2 def apply(req: HttpRequest) = for {
3 title <- OptionalParam("title")(req)
4 } yield Ok(s"Hello, ${title.getOrElse("")} $name!")
5 }
6
7 val endpoint = new Endpoint[HttpRequest, HttpResponse] {
8 def route = {
9 // routes requests like '/hello/Bob?title=Mr.'
10 case Method.Get -> Root / "hello" / name =>
11 BasicallyAuthorize("user", "password") ! hello(name)
12 }
13 }
14
15 val service: Service[HttpRequest, HttpResponse] = endpoint.toService
8
13. Finch: Responses
1 // empty response with status 200
2 val a = Ok()
3
4 // 'plain/text' response with status 404
5 val b = NotFound("body")
6
7 // 'application/json' response with status 201
8 val c = Created(JsonObject("id" -> 100))
9
10 // ¡®plain/text' response with custom header and status 403
11 val d = Forbidden.withHeaders("Some-Header" -> "Secret")("body")
13
14. Finch: Endpoints Highlights
? Finch¡¯s Endpoints are composable routers
!
? Endpoints might be treated as Scala¡¯s
PartialFunctions[Request, Service[_, _]]
!
? Endpoints are convertible to Finagle Service¡¯s
!
? Endpoints might be composed with Service¡¯s, Filter¡¯s or
other Endpoint¡¯s.
14
15. Finch:
Endpoints Composition
1 val ab: Filter[A, C, B, C] = ???
2 val bc: Endpoint[B, C] = ???
3 val cd: Service[C, D]
4
5 val ad1: Endpoint[A, D] = ab ! bc ! cd
6 val ad2: Endpoint[A, D] = ???
7
8 val ad3: Endpoint[A, D] = ad1 orElse ad2
15