| 1 |
.. _rest: |
| 2 |
|
| 3 |
Writing RESTful Web Services with Generic Controllers |
| 4 |
===================================================== |
| 5 |
|
| 6 |
Pecan simplifies RESTful web services by providing a way to overload URLs based |
| 7 |
on the request method. For most API's, the use of `generic controller` |
| 8 |
definitions give you everything you need to build out robust RESTful |
| 9 |
interfaces (and is the *recommended* approach to writing RESTful web services |
| 10 |
in pecan): |
| 11 |
|
| 12 |
:: |
| 13 |
|
| 14 |
from pecan import abort, expose |
| 15 |
|
| 16 |
# Note: this is *not* thread-safe. In real life, use a persistent data store. |
| 17 |
BOOKS = { |
| 18 |
'0': 'The Last of the Mohicans', |
| 19 |
'1': 'Catch-22' |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
class BookController(object): |
| 24 |
|
| 25 |
def __init__(self, id_): |
| 26 |
self.id_ = id_ |
| 27 |
assert self.book |
| 28 |
|
| 29 |
@property |
| 30 |
def book(self): |
| 31 |
if self.id_ in BOOKS: |
| 32 |
return dict(id=self.id_, name=BOOKS[self.id_]) |
| 33 |
abort(404) |
| 34 |
|
| 35 |
# HTTP GET /<id>/ |
| 36 |
@expose(generic=True, template='json') |
| 37 |
def index(self): |
| 38 |
return self.book |
| 39 |
|
| 40 |
# HTTP PUT /<id>/ |
| 41 |
@index.when(method='PUT', template='json') |
| 42 |
def index_PUT(self, **kw): |
| 43 |
BOOKS[self.id_] = kw['name'] |
| 44 |
return self.book |
| 45 |
|
| 46 |
# HTTP DELETE /<id>/ |
| 47 |
@index.when(method='DELETE', template='json') |
| 48 |
def index_DELETE(self): |
| 49 |
del BOOKS[self.id_] |
| 50 |
return dict() |
| 51 |
|
| 52 |
|
| 53 |
class RootController(object): |
| 54 |
|
| 55 |
@expose() |
| 56 |
def _lookup(self, id_, *remainder): |
| 57 |
return BookController(id_), remainder |
| 58 |
|
| 59 |
# HTTP GET / |
| 60 |
@expose(generic=True, template='json') |
| 61 |
def index(self): |
| 62 |
return [dict(id=k, name=v) for k, v in BOOKS.items()] |
| 63 |
|
| 64 |
# HTTP POST / |
| 65 |
@index.when(method='POST', template='json') |
| 66 |
def index_POST(self, **kw): |
| 67 |
id_ = str(len(BOOKS)) |
| 68 |
BOOKS[id_] = kw['name'] |
| 69 |
return dict(id=id_, name=kw['name']) |
| 70 |
|
| 71 |
|
| 72 |
Writing RESTful Web Services with RestController |
| 73 |
================================================ |
| 74 |
|
| 75 |
.. _TurboGears2: http://turbogears.org |
| 76 |
|
| 77 |
For compatability with the TurboGears2_ library, Pecan also provides |
| 78 |
a class-based solution to RESTful routing, :class:`~pecan.rest.RestController`: |
| 79 |
|
| 80 |
:: |
| 81 |
|
| 82 |
from pecan import expose |
| 83 |
from pecan.rest import RestController |
| 84 |
|
| 85 |
from mymodel import Book |
| 86 |
|
| 87 |
class BooksController(RestController): |
| 88 |
|
| 89 |
@expose() |
| 90 |
def get(self, id): |
| 91 |
book = Book.get(id) |
| 92 |
if not book: |
| 93 |
abort(404) |
| 94 |
return book.title |
| 95 |
|
| 96 |
URL Mapping |
| 97 |
----------- |
| 98 |
|
| 99 |
By default, :class:`~pecan.rest.RestController` routes as follows: |
| 100 |
|
| 101 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 102 |
| Method | Description | Example Method(s) / URL(s) | |
| 103 |
+=================+==============================================================+============================================+ |
| 104 |
| get_one | Display one record. | GET /books/1 | |
| 105 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 106 |
| get_all | Display all records in a resource. | GET /books/ | |
| 107 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 108 |
| get | A combo of get_one and get_all. | GET /books/ | |
| 109 |
| | +--------------------------------------------+ |
| 110 |
| | | GET /books/1 | |
| 111 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 112 |
| new | Display a page to create a new resource. | GET /books/new | |
| 113 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 114 |
| edit | Display a page to edit an existing resource. | GET /books/1/edit | |
| 115 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 116 |
| post | Create a new record. | POST /books/ | |
| 117 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 118 |
| put | Update an existing record. | POST /books/1?_method=put | |
| 119 |
| | +--------------------------------------------+ |
| 120 |
| | | PUT /books/1 | |
| 121 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 122 |
| get_delete | Display a delete confirmation page. | GET /books/1/delete | |
| 123 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 124 |
| delete | Delete an existing record. | POST /books/1?_method=delete | |
| 125 |
| | +--------------------------------------------+ |
| 126 |
| | | DELETE /books/1 | |
| 127 |
+-----------------+--------------------------------------------------------------+--------------------------------------------+ |
| 128 |
|
| 129 |
Pecan's :class:`~pecan.rest.RestController` uses the ``?_method=`` query string |
| 130 |
to work around the lack of support for the PUT and DELETE verbs when |
| 131 |
submitting forms in most current browsers. |
| 132 |
|
| 133 |
In addition to handling REST, the :class:`~pecan.rest.RestController` also |
| 134 |
supports the :meth:`index`, :meth:`_default`, and :meth:`_lookup` |
| 135 |
routing overrides. |
| 136 |
|
| 137 |
.. warning:: |
| 138 |
|
| 139 |
If you need to override :meth:`_route`, make sure to call |
| 140 |
:func:`RestController._route` at the end of your custom method so |
| 141 |
that the REST routing described above still occurs. |
| 142 |
|
| 143 |
Nesting ``RestController`` |
| 144 |
--------------------------- |
| 145 |
|
| 146 |
:class:`~pecan.rest.RestController` instances can be nested so that child |
| 147 |
resources receive the parameters necessary to look up parent resources. |
| 148 |
|
| 149 |
For example:: |
| 150 |
|
| 151 |
from pecan import expose |
| 152 |
from pecan.rest import RestController |
| 153 |
|
| 154 |
from mymodel import Author, Book |
| 155 |
|
| 156 |
class BooksController(RestController): |
| 157 |
|
| 158 |
@expose() |
| 159 |
def get(self, author_id, id): |
| 160 |
author = Author.get(author_id) |
| 161 |
if not author_id: |
| 162 |
abort(404) |
| 163 |
book = author.get_book(id) |
| 164 |
if not book: |
| 165 |
abort(404) |
| 166 |
return book.title |
| 167 |
|
| 168 |
class AuthorsController(RestController): |
| 169 |
|
| 170 |
books = BooksController() |
| 171 |
|
| 172 |
@expose() |
| 173 |
def get(self, id): |
| 174 |
author = Author.get(id) |
| 175 |
if not author: |
| 176 |
abort(404) |
| 177 |
return author.name |
| 178 |
|
| 179 |
class RootController(object): |
| 180 |
|
| 181 |
authors = AuthorsController() |
| 182 |
|
| 183 |
Accessing ``/authors/1/books/2`` invokes :func:`BooksController.get` with |
| 184 |
``author_id`` set to ``1`` and ``id`` set to ``2``. |
| 185 |
|
| 186 |
To determine which arguments are associated with the parent resource, Pecan |
| 187 |
looks at the :func:`get_one` then :func:`get` method signatures, in that order, |
| 188 |
in the parent controller. If the parent resource takes a variable number of |
| 189 |
arguments, Pecan will pass it everything up to the child resource controller |
| 190 |
name (e.g., ``books`` in the above example). |
| 191 |
|
| 192 |
Defining Custom Actions |
| 193 |
----------------------- |
| 194 |
|
| 195 |
In addition to the default methods defined above, you can add additional |
| 196 |
behaviors to a :class:`~pecan.rest.RestController` by defining a special |
| 197 |
:attr:`_custom_actions` |
| 198 |
dictionary. |
| 199 |
|
| 200 |
For example:: |
| 201 |
|
| 202 |
from pecan import expose |
| 203 |
from pecan.rest import RestController |
| 204 |
|
| 205 |
from mymodel import Book |
| 206 |
|
| 207 |
class BooksController(RestController): |
| 208 |
|
| 209 |
_custom_actions = { |
| 210 |
'checkout': ['POST'] |
| 211 |
} |
| 212 |
|
| 213 |
@expose() |
| 214 |
def checkout(self, id): |
| 215 |
book = Book.get(id) |
| 216 |
if not book: |
| 217 |
abort(404) |
| 218 |
book.checkout() |
| 219 |
|
| 220 |
:attr:`_custom_actions` maps method names to the list of valid HTTP |
| 221 |
verbs for those custom actions. In this case :func:`checkout` supports |
| 222 |
``POST``. |