Date post: | 30-Jun-2015 |
Category: |
Technology |
Upload: | ortus-solutions-corp |
View: | 115 times |
Download: | 0 times |
REST APIsREpresentational State Transfer of anApplication Programming Interface
Who Am I?@Francainath
This is what I do:
(+ )
(REST)
In the Beginning...
Changing landscape
Where we are today:
PROBLEM:
How can we make our app work on allthese devices?
solution #1: RESPONSIVE DESIGN
solution #2: CUSTOM APPS Unique app for each environment
Are we gonna write a new app forEVERY DEVICE?
0:01
ColdBox REST
REST APIsREpresentational StateTransfer of anApplication Programming InterfaceWe are going to transform ourColdBox program into an API andaccess it through a RESTful interface
STEP #1: APIStart w/a app
STEP #1: APIMake it headless & start api module
STEP #2: RESTURI routes are built up w/a combo ofresources and actions
Resources = objectsmade/managed thru the modelrepresented by nouns
Actions = functionsbuilt in the handlersrepresented by verbs
Basic Handler
RoutesRoutes = "how we reach our handlers"built in the ModuleConfig.cfcBuild them up with:
resource (nouns)action (verbs)
basic ModuleConfig.cfc
Example routesList players?(baseURL)/api/player/listGet a player? (http verb GET)(baseURL)/api/player/:playerIDMake a player? (http verb POST)(baseURL)/api/player/:playerID
- docs
Returning data"The most common way to return datafrom your handler's action is to usethe even object's renderData()method. It takes complex data andturns it into a string representation"
Return data formatsXMLJSONTEXTWDDXPDFHTML
Setting up a return
Well, that was
Wasn't it?
Well, it was...
Bare Bones!
0:03
Beyond "bare bones"Status CodesCachingSecurityRestricting HTTP VerbsError HandlingRelax
Status Codes200 "OK"202 "Created"400 "Bad Request"401 "Unauthorized"404 "Not Found"500 "Server Error"
Using Status Codes1. event.renderData() (shortcut)
statusCode & statusMessage attr2. event.setHTTPHeader() (manual)
statusCode & statusText attr
Restricting HTTP verbsin our routes, we mapped HTTP verbsto handler actions, but how can weprevent users from hitting resourcesw/the wrong verbs? (aka a GET to adelete function)
this.allowedMethodsallowedMethods property in handlercan restrict those connections for us
RELAX!!!
and now...
ResourcesColdbox REST docs
http://wiki.coldbox.org/wiki/Building_Rest_APIs.cfm
Coldbox REST Refcard (PDF)https://github.com/ColdBox/cbox-refcards/raw/master/ColdBox%20REST%20APIs/ColdBox-REST-APIs-Refcard.pdf
Coldbox Relax docshttp://wiki.coldbox.org/wiki/Projects:Relax.cfm