Tiddler 'A Model for a RESTful API' in Namespace 'TiddlyHome'

list | new | edit | delete | .html | .tw | .txt | .js

A Model for a RESTful API

BidiX - 2008-09-26 19:55:51  (created: 2008-06-10 21:03:54 ) 
tags: Documentation Design RESTful API


This article suggests a model for a RESTful API usable in a browser without using AJAX and that manage a suffix to request a particular content-type.

This model is implemented has a generic extension to Google Engine webapp. See it in action in TiddlyWiki context at TiddlyHome

Resource

The typical URI of a resource is :
{type}s{id}[.{format}]
like in /tiddlywikis/HowTo.html.


To obtain the list of all resources of a type simply :
{type}s

Membership relation

When the resource is linked to an other resource, especially to explain membership, the URI becomes :
{parent_type}s{parent_id}{children_type}s{children_id}
(ex: /tiddlywikis/HowTo/tiddlers/Upload ).

Basic functions on resource

In RESTful API, the CRUD basic functions on a persistent resource can be accessed using HTTP methods :
HTTP MethodFunction
GET {uri_id}Read {resource_id}
PUT {uri_id}Create or Update {resource_id}
DELETE {uri_id}Delete {resource_id}
POST {a_form}Ask service to Create, Update or Delete some resource using {a_form}

API to manage a resource

API callApplication function called
GET {type}sType.list()
GET {type}{id}[.{format}]id.display[_in_{format}]()
404
PUT {type}s{id}id.put()
DELETE {type}s{id}id.delete()
POST {type}sType.create_or_update()

RESTful extensions for browser

Since methods PUT and DELETE can't easily be activated in a browser (except using AJAX). The query_string part of the URL is used to simulate PUT or DELETE.
On server, the Handler first changes the query_string in the corresponding method :
URL with query_string API Call
GET {type}s{id}?deleteDELETE {type}s{id}
GET {type}s{id}?putPUT {type}s{id}
GET {type}s?newGET {type}s - with an attribut in_form = True
GET {type}s{id}?editGET {type}s{id} - with an attribut in_form = True

Specifying Content-type

To easily specify the content-type required, a suffix is added to the resource like : .html, .xml, .txt .js ...

Return codes

Finally HTTP Status are used in response to HTTP request. Their normalised meaning are used, sometimes with some restrictive sense in the API context.
HTTP CodeHTTP status messageAPI Meaning
200OKOk
201CreatedNew entity created
400Bad RequestThe URL can't be processed (Type doesn't exist, bad format requested ...)
401UnauthorizedThe logged user is not authorized to execute this action
403ForbiddenAction is not allowed
404Not FoundResource doesn't exist
405Method not allowedThis resource doesn't allow this method (operation)
406Not acceptable



This article suggests a model for a ~RESTful API usable in a browser without using AJAX and that manage a suffix to request a particular content-type.

This model is implemented has a generic extension to Google Engine webapp. See it in action in ~TiddlyWiki context at [[TiddlyHome|http://TiddlyHome.appspot.com]]

! Resource
The typical [[URI|http://en.wikipedia.org/wiki/URI]] of a resource is : 
{{{
{type}s{id}[.{format}]
}}}
like in {{{/tiddlywikis/HowTo.html}}}. 


To obtain the list of all resources of a type simply : 
{{{
{type}s
}}}

! Membership relation 
When the resource is linked to an other resource, especially to explain membership,  the URI becomes :
{{{
{parent_type}s{parent_id}{children_type}s{children_id}
}}}
 (ex: {{{/tiddlywikis/HowTo/tiddlers/Upload}}} ).

! Basic functions on resource
In RESTful API, the [[CRUD|http:/http://en.wikipedia.org/wiki/CRUD_(acronym)]] basic functions on a persistent resource can be accessed using HTTP methods :
|!HTTP Method|!Function|
|{{{GET {uri_id}}}}|Read {resource_id} |
|{{{PUT {uri_id}}}}|Create or Update {resource_id} |
|{{{DELETE {uri_id}}}}|Delete {resource_id}|
|{{{POST {a_form}}}}|Ask service to Create, Update or Delete some resource using {a_form} |

! API to manage a resource
|!API call|!Application function called|
|{{{GET {type}s}}}|Type.list()|
|{{{GET {type}{id}[.{format}]}}}|id.display[_in_{format}]()|
|~|404|
|{{{PUT {type}s{id}}}}|id.put()|
|{{{DELETE {type}s{id}}}}|id.delete()|
|{{{POST {type}s}}}|Type.create_or_update()|

! ~RESTful extensions for browser
Since methods PUT and DELETE can't easily be activated in a browser (except using AJAX). The query_string part of the URL is used to simulate PUT or DELETE. 
On server, the Handler first changes the query_string in the corresponding method :
|! URL with query_string |! API Call |
|{{{GET {type}s{id}?delete}}} |{{{DELETE {type}s{id}}}} |
|{{{GET {type}s{id}?put}}} |{{{PUT {type}s{id}}}} |
|{{{GET {type}s?new}}} |{{{GET {type}s}}} - with an attribut in_form = True |
|{{{GET {type}s{id}?edit}}} |{{{GET {type}s{id}}}} - with an attribut in_form = True |

! Specifying Content-type
To easily specify the content-type required, a suffix is added to the resource like : {{{.html}}}, {{{.xml}}}, {{{.txt}}} {{{.js}}} ...

! Return codes
Finally HTTP Status are used in response to HTTP request. Their normalised meaning are used, sometimes with some restrictive sense in the API context.
|!HTTP Code|!HTTP status message |!API Meaning|
| 200 |OK |Ok |
| 201 |Created |New entity created |
| 400 |Bad Request|The URL can't be processed (Type doesn't exist, bad format requested ...)  |
| 401 |Unauthorized|The logged user is not authorized to execute this action|
| 403 |Forbidden|Action is not allowed|
| 404 |Not Found|Resource doesn't exist|
| 405 |Method not allowed|This resource doesn't allow this method (operation)|
| 406 |Not acceptable||