Transcript
Page 1: CBDW2014 - ColdBox RESTFul Services

REST  APIsREpresentational  State  Transfer  of  anApplication  Programming  Interface

Page 2: CBDW2014 - ColdBox RESTFul Services

Who  Am  I?@Francainath

Page 3: CBDW2014 - ColdBox RESTFul Services

This  is  what  I  do:

 (+   )

    (REST)

Page 4: CBDW2014 - ColdBox RESTFul Services

In  the  Beginning...

Page 5: CBDW2014 - ColdBox RESTFul Services

Changing  landscape

Page 6: CBDW2014 - ColdBox RESTFul Services

Where  we  are  today:

Page 7: CBDW2014 - ColdBox RESTFul Services

PROBLEM:

How  can  we  make  our  app  work  on  allthese  devices?

Page 8: CBDW2014 - ColdBox RESTFul Services

solution  #1:  RESPONSIVE  DESIGN  

Page 9: CBDW2014 - ColdBox RESTFul Services

solution  #2:  CUSTOM  APPS  Unique  app  for  each  environment  

 

Page 10: CBDW2014 - ColdBox RESTFul Services

Are  we  gonna  write  a  new  app  forEVERY  DEVICE?

Page 11: CBDW2014 - ColdBox RESTFul Services

0:01

Page 12: CBDW2014 - ColdBox RESTFul Services
Page 13: CBDW2014 - ColdBox RESTFul Services

ColdBox  REST

Page 14: CBDW2014 - ColdBox RESTFul Services

REST  APIsREpresentational  StateTransfer  of  anApplication  Programming  InterfaceWe  are  going  to  transform  ourColdBox  program  into  an  API  andaccess  it  through  a  RESTful  interface

Page 15: CBDW2014 - ColdBox RESTFul Services

STEP  #1:  APIStart  w/a    app

Page 16: CBDW2014 - ColdBox RESTFul Services

STEP  #1:  APIMake  it  headless  &  start  api  module

Page 17: CBDW2014 - ColdBox RESTFul Services

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

Page 18: CBDW2014 - ColdBox RESTFul Services

Basic  Handler

Page 19: CBDW2014 - ColdBox RESTFul Services

RoutesRoutes  =  "how  we  reach  our  handlers"built  in  the  ModuleConfig.cfcBuild  them  up  with:

resource  (nouns)action  (verbs)

Page 20: CBDW2014 - ColdBox RESTFul Services

basic  ModuleConfig.cfc

Page 21: CBDW2014 - ColdBox RESTFul Services

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

Page 22: CBDW2014 - ColdBox RESTFul Services

-­    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"

Page 23: CBDW2014 - ColdBox RESTFul Services

Return  data  formatsXMLJSONTEXTWDDXPDFHTML

Page 24: CBDW2014 - ColdBox RESTFul Services

Setting  up  a  return

Page 25: CBDW2014 - ColdBox RESTFul Services

Well,  that  was  

Wasn't  it?

Page 26: CBDW2014 - ColdBox RESTFul Services

Well,  it  was...

Bare  Bones!

Page 27: CBDW2014 - ColdBox RESTFul Services

0:03

Page 28: CBDW2014 - ColdBox RESTFul Services

Beyond  "bare  bones"Status  CodesCachingSecurityRestricting  HTTP  VerbsError  HandlingRelax

Page 29: CBDW2014 - ColdBox RESTFul Services

Status  Codes200  "OK"202  "Created"400  "Bad  Request"401  "Unauthorized"404  "Not  Found"500  "Server  Error"

Page 30: CBDW2014 - ColdBox RESTFul Services

Using  Status  Codes1.  event.renderData()  (shortcut)

statusCode  &  statusMessage  attr2.  event.setHTTPHeader()  (manual)

statusCode  &  statusText  attr

Page 31: CBDW2014 - ColdBox RESTFul Services

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)

Page 32: CBDW2014 - ColdBox RESTFul Services

this.allowedMethodsallowedMethods  property  in  handlercan  restrict  those  connections  for  us

Page 33: CBDW2014 - ColdBox RESTFul Services

RELAX!!!

and  now...

Page 34: CBDW2014 - ColdBox RESTFul Services

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


Top Related