cbdw2014 - coldbox restful services
DESCRIPTION
Do you want to build a solid, robust API that you can build your snazzy mobile app or fancy new web app on? ColdBox RELAX (REstful tools for LAzy eXperts) can be your new best friend. Built upon the solid ecosystem of the ColdBox framework and integrated Box services, ColdBox RELAX can help you construct a solid, expandable, maintainable, and vigorous API for all your modern web application needs. Come learn how to build Relax APIs in this session and see how web applications can benefit from RELAX, the Box ecosystem’s RESTful services.TRANSCRIPT
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