building custom apis

28
Building a custom API by Charlie van de Kerkhof (primeminister) Courier New Bold 18 pt Charlie-preso CakeFest #3 July 2009

Upload: pierre-martin

Post on 26-May-2015

1.185 views

Category:

Technology


0 download

DESCRIPTION

Slides from the talk given by Charlie van de Kerkhof during the CakeFest #3 - July 2009 Note: the original pdf and the code related to this talk can be found on cakephp.org

TRANSCRIPT

Page 1: Building custom APIs

Building a custom APIby Charlie van de Kerkhof (primeminister)

Courier New Bold18 pt

Charlie-preso

CakeFest #3 July 2009

Page 2: Building custom APIs

Goals

• RESTful with the correct HTTP code

• API like twitter to open up to the public in the near future

• Smooth integration with code for the website

• Return values HTML, XML and JSON

• Not necessarily the correct methods (POST,GET,DELETE,PUT)

Page 3: Building custom APIs

What is it?

• Link sharing (like del.icio.us) with groups and users

• Comments on links

• Trusted group of friends

• Links are not necessarily bookmarks!

Page 4: Building custom APIs

Setup

• Database

• Routes

• Model

• Controller

• Views (HTML, XML & JSON)

• Tests

Page 5: Building custom APIs

Database

Page 6: Building custom APIs

Models

• Baked the associations

• Basic validation in model

• AppModel: Using containable

• AppModel: Overwrite del() method for using ‘status’ field in tables.

Page 7: Building custom APIs

AppController• Setting up helpers and components

• Mapping request methods to actions

• Defining up HTTP response codes

• Populate POST request to $this->dataBecause this APP is also meant to get data from HTML forms

Page 8: Building custom APIs

• Controller => links, action => viewparams[‘pass’][0] = ’primeminister’

• Check nickname variable

• Find the links from this user

• Set the results and options

GET Request/links/view/primeminister.xml

Page 9: Building custom APIs

APP/controllers/links_controller.php

Page 10: Building custom APIs
Page 11: Building custom APIs

Error occursf.i. nickname is not same as loggedin user

• Calls AppController::setError()

• setError() checks which request is made (XML, JSON, HTML)

• When HTML is requested it calls the Session::setFlash method

• Other requests it sets the viewVars and renders the error template errcode & code:

errcode: to define f.e. 1007 what is missing.For developers handy to program

Code: HTTP response code

Page 12: Building custom APIs

APP/controllers/links_controller.php

Page 13: Building custom APIs

APP/app_controller.php

Page 14: Building custom APIs

continue... APP/app_controller.php

Page 15: Building custom APIs

continue... APP/app_controller.php

Page 16: Building custom APIs

Views

• Created generic template files

• XML: Xml::serialize with format=>tags

• JSON with $javascript->object($record);

• When necessary create template file in views/{controller} folder to override generic template

• Put this check in AppController::beforeRender

Page 17: Building custom APIs

APP/views/generic/xml/view.ctp

Page 18: Building custom APIs

APP/views/generic/json/view.ctp

Page 19: Building custom APIs

APP/views/links/xml/view.ctp

Page 20: Building custom APIs

Links view (XML)/links/view/primeminister.xml?limit=2&page=3

Page 21: Building custom APIs

Error view (XML)

Page 22: Building custom APIs

Routes

Page 23: Building custom APIs

Controller tests

• Test made on controller actions

• Checks for view variables

• errocode & code check

• Don’t forget to check on errors!

Page 24: Building custom APIs

tests/cases/controllers/links_controllers.test.php

Page 25: Building custom APIs

Summarize

Page 26: Building custom APIs

• Loosely map request method (POST/GET/...) to action

• For using in website and as API (!)

• Return response codes and Mento errors

• Easier for developer to check what error

• Rendering views based on generic template

• Tests on controller and viewVars (code)

Page 27: Building custom APIs

Question (& Answers?)

Page 28: Building custom APIs

Thanks!

Web: http://www.cake-toppings.comIRC: primeministerTwitter: http://twitter.com/charli3