REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 ·...
Transcript of REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 ·...
![Page 2: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/2.jpg)
EASY!def api(request: HttpRequest): HttpResponse = { ???}
![Page 3: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/3.jpg)
FOR EVERY COMPLEX PROBLEM THERE IS ANANSWER THAT IS CLEAR, SIMPLE, AND
WRONG.
![Page 4: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/4.jpg)
SPRAY'S APPROACHA service is an Actor implementation that handles the incomingHttpRequests, and replies with appropriate HttpResponses.
def receive = { case request: HttpRequest => val response = HttpResponse(...) sender ! response}
![Page 5: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/5.jpg)
THE REAL DEALclass HelloWorldService extends Actor {
def receive: Receive = { case request: HttpRequest => val response = HttpResponse(...) sender ! response }
}
![Page 6: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/6.jpg)
TESTING THE SERVICESBecause the service is a plain Actor, one can test it using .TestKit
class HelloWorldServiceSpec extends TestKit(ActorSystem()) with SpecificationLike with ImplicitSender { val service = TestActorRef[HelloWorldService]
"Any request" should { "Reply with Hello, world" in { service ! HttpRequest() expectMsgType[HttpResponse].entity mustEqual HttpEntity("Hello, world") } }}
![Page 7: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/7.jpg)
HOSTING THE SERVICESUse spray-can HTTP server. We bind the services to it.
object HelloWorld extends App { val system = ActorSystem() val service = system.actorOf(Props[HelloWorldService])
IO(Http)(system) ! Http.Bind( service, "0.0.0.0", port = 8080)
Console.readLine() system.shutdown()}
class HelloWorldService extends Actor { ... }
![Page 8: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/8.jpg)
LET'S SEE NOW...[INFO] (...) Bound to /0.0.0.0:8080[WARN] (...) Configured registration timeout of 1 second expired, stopping
![Page 9: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/9.jpg)
![Page 10: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/10.jpg)
THE REAL DEALclass HelloWorldService extends Actor {
def receive: Receive = { case request: HttpRequest => val response = HttpResponse(...) sender ! response case _: Http.Connected => sender ! Http.Register(self) }
}
![Page 11: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/11.jpg)
DEMO
![Page 12: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/12.jpg)
CONVENIENT DSLIt is tedious to build a complex API by handling theHttpRequests.Construct Spray Routes using convenient DSL, and then turnthese routes to the Receive partial function.Use similar DSL to unit-test the routes
![Page 13: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/13.jpg)
ROUTED HELLO, WORLDFirst, the Route itself:
trait DemoRoute extends Directives {
val demoRoute: Route = get { complete { "Hello, world" } }}
![Page 14: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/14.jpg)
ROUTED HELLO, WORLDNext up, expressing the Receive PF using the route:
class MainService(route: Route) extends HttpServiceActor {
def receive: Receive = runRoute(route)
}
![Page 15: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/15.jpg)
TESTING OUR ROUTESpray's DSL extends to testing, too!
class DemoRouteSpec extends Specification with Specs2RouteTest with DemoRoute {
"Any request" should { "Reply with Hello, World" in { Get() ~> demoRoute ~> check { responseAs[String] mustEqual "Hello, world" } } }
}
![Page 16: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/16.jpg)
DEMO
![Page 17: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/17.jpg)
MORE DSL EXAMPLESWe can match—amongst others—on:
HTTP methods: get, post, put, ...,Paths; including path-variables and query parameters:path("customer" / IntNumber), parameter('id.as[Int])HTTP headers: headerValueByName("User-Agent"),Cookies: cookie("spray")
We combine parts of the DSL using ~
![Page 18: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/18.jpg)
DEMO
![Page 19: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/19.jpg)
COMPLETINGTo complete a route, we must provide RequestContext => ().
complete does just that, or we can do it ourselves.
trait TweetAnalysisRoute extends Directives {
val tweetAnalysisRoute: Route = post { path("tweets" / Segment) ??? }}
![Page 20: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/20.jpg)
COMPLETINGTo complete a route, we must provide RequestContext => ().
complete does just that, or we can do it ourselves.
trait TweetAnalysisRoute extends Directives {
val tweetAnalysisRoute: Route = post { path("tweets" / Segment)(sendTweetAnalysis) }
def sendTweetAnalysis(query: String) (ctx: RequestContext): Unit = { ctx.receiver ! ChunkedMessageStart(...) } }
![Page 21: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/21.jpg)
A REAL APPWe want to stream results of a Twitter search, and show:
Counts of positive and negative tweets,Counts of languages,Counts of locations
![Page 22: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/22.jpg)
A REAL APP
![Page 23: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/23.jpg)
DEMO
![Page 24: REACTIVE APIS - Chariot Solutionschariotsolutions.com/wp-content/uploads/...Jan 04, 2014 · SPRAY'S APPROACH A service is an Actor implementation that handles the incoming HttpRequests,](https://reader034.fdocuments.us/reader034/viewer/2022050516/5fa055849422494fe76a0702/html5/thumbnails/24.jpg)
THANK YOU!Source at ,Follow my on ,Look out for blog post at
github.com/eigengo/phillyete2014@honzam399
cakesolutions.net/teamblogs