Building Apis in Scala with Playframework2
-
Upload
manish-pandit -
Category
Technology
-
view
729 -
download
0
description
Transcript of Building Apis in Scala with Playframework2
![Page 1: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/1.jpg)
Building APIs in Scala with Play! Framework 2
Silicon Valley Code Camp
Oct 5th, 2013
Manish Pandit@lobster1234
![Page 3: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/3.jpg)
Agenda
Scala
Typesafe Platform
PlayFramework
REST
Hands-on API
Open Floor
![Page 4: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/4.jpg)
Scala
Concise, yet expressive
Statically Typed via Type Inference
Rich Constructs
Functions as First Class Citizens
Power of DSL
Interoperability with the Java Ecosystem
![Page 5: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/5.jpg)
Typesafe Platform
Scala Programming Language
PlayFramework for Web Applications
sbt for builds
Akka
Typesafe Activator
Misc. Tools
![Page 6: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/6.jpg)
Typesafe Activator
A very simple way to start with Scala and Play!
Applications
Browser-based
Interactive
Comes with a good number of templates
![Page 7: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/7.jpg)
Typesafe Activator
![Page 8: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/8.jpg)
Typesafe Activator
Use if you do not have an IDE
Use it to play with various templates it comes with
Yet to mature but a (very) good start
![Page 9: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/9.jpg)
Play! Framework
Simple
Convention over Configuration
Focus on Developer Productivity
Asynchronous Processing Model
Makes testing easy
JSON support for RESTFul Applications
![Page 10: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/10.jpg)
Play! Framework
Not a J2EE compatible framework
Does not follow the traditional request/response model
![Page 11: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/11.jpg)
Creating a Play! Project
![Page 12: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/12.jpg)
Using an IDE
![Page 13: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/13.jpg)
Eclipse Import
![Page 14: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/14.jpg)
Gotchas!
Start with a fresh installation of ScalaIDE
Go to Install New Software – Select ScalaIDE – Check
Play2 Plugin
![Page 15: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/15.jpg)
Run the App!
![Page 16: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/16.jpg)
And Play! with it
http://localhost:9000
Test the app with play test
![Page 17: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/17.jpg)
Play! Core Concepts
Uses sbt as the build system
(In a special way to support always-on builds)
![Page 18: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/18.jpg)
Play! Core Concepts
MVC
Folder Structure
Controllers
Routes File
Tests
![Page 19: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/19.jpg)
Play! Actions
In simple terms, an Action takes function, that takes a
request Request, and returns a Response.
![Page 20: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/20.jpg)
Play! Actions
All routable methods of a Controller are Actions
(Request => Result) function
![Page 21: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/21.jpg)
The almighty Action
A simple, no-request that returns a result.
A function that makes the Request available to the block
You can pass a BodyParser to the Action as an argument
Default BodyParsers are available for common content
types
![Page 22: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/22.jpg)
Play! Console Demo
run vs. ~run
With ~ run or run, change the app with syntax error
Fix the app and retry
Use the app with ~ test in Play! Console
Change the code and watch the tests fail
Change the tests and watch them pass
![Page 23: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/23.jpg)
The Routes file
Type-safe way to define mappings from URLs to Actions
Rich support for basic data types
Handling of defaults
![Page 24: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/24.jpg)
RESTFul APIs
Not JSON over HTTP
Not JSON representation of your Database
No Spec or official standard
![Page 25: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/25.jpg)
RESTFul APIs
Envision the entities as HTTP resources
Envision the interactions as HTTP methods
Read RFC 2616
![Page 26: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/26.jpg)
Play! Hands-on API
Model – A Person
APIs – Get all, Get by First/Last/Zip, Add a person
JSON Request and Response
![Page 27: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/27.jpg)
Model
object PersonModel {
val persons = ArrayBuffer(Person("Manish", "Pandit", 94568), Person("John", "Doe", 95051), Person("My", "Neighbor", 94568))
def add(p: Person) = persons += p
def getAll = persons
def getByFirstName(first: String) = persons.filter(_.first == first)
def getByLastName(last: String) = persons.filter(_.last == last)
def getByZip(zip: Int) = persons.filter(_.zip == zip)
}
case class Person(first: String, last: String, zip: Int)
![Page 28: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/28.jpg)
Controller
object SimpleAPI extends Controller {
implicit val personWrites = Json.writes[Person] implicit val personReads = Json.reads[Person]
def getAll = Action { Ok(Json.toJson(PersonModel.getAll)) }
def add = Action(parse.json) { request => Logger.info(s"Body is $request.body") request.body.validate(personReads) match { case x: JsError => BadRequest case x: JsSuccess[Person] => PersonModel.add(x.get) Created } }
def getByFirstName(first: String) = Action { val list = PersonModel.getByFirstName(first) if (list.isEmpty) NotFound else Ok(Json.toJson(PersonModel.getByFirstName(first))) }
}
![Page 29: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/29.jpg)
Routes# ~~~~# Routes# This file defines all application routes (Higher priority routes first)# ~~~~
# Home page
GET /persons controllers.SimpleAPI.getAll
GET /persons/first/:first controllers.SimpleAPI.getByFirstName(first)
GET /persons/last/:last controllers.SimpleAPI.getByLastName(last)
GET /persons/zip/:zip controllers.SimpleAPI.getbyZip(zip:Int)
GET /persons/default controllers.SimpleAPI.getByFirstName(first="Manish")
POST /persons controllers.SimpleAPI.add
![Page 30: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/30.jpg)
Play! Packaging
Use play dist to package the app as a zip file
Run using the start script
![Page 31: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/31.jpg)
Play! Advanced Topics
Reactive Programming
Akka, Futures, Promises
Play WS Library
Anorm and Slick
![Page 32: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/32.jpg)
Further reading
http://www.playframework.com/
http://typesafe.com/platform
StackOverflow
Source : https://github.com/lobster1234/SVCC2013
![Page 33: Building Apis in Scala with Playframework2](https://reader036.fdocuments.in/reader036/viewer/2022062617/54b736384a7959be4c8b45ed/html5/thumbnails/33.jpg)
We are hiring!
netflix.github.io
techblog.netflix.com
jobs.netflix.com