summaryrefslogtreecommitdiff
path: root/vendor/CherryPy-3.2.0/sphinx/source/progguide/REST.rst
blob: 2a2cf5423f67f9c03e4d52a81a1039dabc34a466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
*****************************************
Creating RESTful applications in CherryPy
*****************************************

Introduction
============

REST (Representational State Transfer) is an architectural style that
is well-suited to implementation in CherryPy. Both REST and CherryPy
heavily leverage the HTTP protocol but otherwise carry minimal
requisite overhead. This chapter briefly discusses the purpose of
REST and an example implementation in CherryPy.

REST in a nutshell
==================

The REST architectural style describes how domain models and their state
are referenced and transferred. The primary goal of REST is promote
certain advantageous qualities of a distributed system, namely high
visibility, scalability, extensibility.

Terminology
-----------

 - "resources" are concepual objects represented by the system - any
   information that can be named is a resource.
 - "state" is data held by or associated with resources
 - "representations" are information in state with a specific encoding
 - "methods" are invoked to transfer or mutate state.
 - an "identifier" is a URL or URI which uniquely and usually globally
   references a resource.

More information on REST can be found in abundance in Wikipedia and
other readily-available resources.

Implementing REST in CherryPy
=============================

From the canonical `Roy Fielding dissertation <http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5>`_ :

    REST is defined by four interface constraints: identification of resources;
    manipulation of resources through representations; self-descriptive messages;
    and, hypermedia as the engine of application state

This section covers each of these four contstraints and demonstrates how each
is applied in a CherryPy implementation.

Identification of Resources
---------------------------

As an HTTP service provider, resources represented in CherryPy are
referenced by HTTP URIs (Uniform Resource Identifiers). A URI consists
of four parts: scheme, hierarchical identifier, query, and fragment.
For HTTP, the scheme is always ``http`` or ``https``. The hierarchical
identifier consists of an authority (typically host/port) and a path
(similar to a file system path, but not necessarily representing an
actual path).

A single CherryPy instance is typically bound to a single
server/port pair, such that the scheme://authority portion of the URI
references the server. This aspect is configured through the
``server.socket_host`` and ``server.socket_port`` options or via another
hosting server.

Within the CherryPy server, the remainder of the hierarchical
identifier--the path--is mapped to Python objects
via the Dispatch mechanism. This behavior is highly
customizable and documented in :doc:`/concepts/dispatching`.

Using the default dispatcher and page handlers, the path of the URI
maps to a hierarchy of Python identifiers in the CherryPy app. For
example, the URI path ``/container/objects/pencil`` will result in a
call to ``app.root.container.objects.pencil()`` where ``app`` is the
CherryPy app.

Manipulation of Resources Through Representations
-------------------------------------------------

REST defines the use of the HTTP protocol and HTTP methods to implement
the standard REST methods.

 - GET retrieves the state of a specific resource,
 - PUT creates or replaces the state of a specific resource,
 - POST passes information to a resource to use at it sees fit,
 - DELETE removes resources.

The default dispatcher in CherryPy stores the HTTP method name at
:attr:`cherrypy.request.method<cherrypy._cprequest.Request.method>`.

Because HTTP defines these invocation methods, the most direct
way to implement REST using CherryPy is to utilize the
:class:`MethodDispatcher<cherrypy._cpdispatch.MethodDispatcher>`
instead of the default dispatcher. To enable
the method dispatcher, add the
following to your configuration for the root URI ("/")::

        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        }

Now, the REST methods will map directly to the same method names on
your resources. That is, a GET method on a CherryPy class implements
the HTTP GET on the resource represented by that class.

For example::

    class Resource(object):
        
        exposed = True
        
        def GET(self):
            return """Some representation of self"""
        
        def PUT(self):
            self.content = initialize_from_representation(cherrypy.request.body.read())

The concrete implementation of GET and PUT has been omitted, but the
basic concepts remain: GET returns some meaningful representation of
the resource and PUT stores an instance of an object represented by the
content in the body of the request.

Self-Descriptive Messages
-------------------------

REST enables powerful clients and intermediaries by requiring messages to be
self-descriptive; that is, everything you need to know about a message is
carried within the message itself, either in headers or within the definition
of the message's declared media type.

CherryPy gives you easy access to the headers. It's as simple as
:attr:`cherrypy.request.headers<cherrypy._cprequest.Request.headers>` and
:attr:`cherrypy.response.headers<cherrypy._cprequest.Response.headers>`!
Each is a normal Python dictionary which you can read and write as you like.
They also have additional functions to help you parse complex values according
to the HTTP spec.

CherryPy also allows you to set whatever response Content-Type you prefer,
just like any other response header. You have complete control. When reading
request entities, you can register :ref:`custombodyprocessors` for different
media types.

Hypermedia as the Engine of Application State
---------------------------------------------

REST is designed as a stateless protocol--all application state is
maintained with the application at the client. Thus, concepts such as a
"session" need not be maintained by the server. CherryPy does not enable
sessions by default, so it is well-suited to the RESTful style.

In order for the client to maintain meaningful state, however, the REST
server implementer must provide meaningful URIs which supply semantic
links between resources.

For example, a CherryPy application might have a resource index, which
a client might retrieve to inspect the application for other resources::

    class ResourceIndex(object):
        def GET(self):
            items = [item.get_href() for item in self.get_all_items()]
            return ', '.join(items)

This very simple example demonstrates how to create an index of
comma-separated hypertext references. This example assumes the client
can effectively interpret comma-separated references. In practice,
another representation such as HTML or JSON might be used.

A Quick Example
===============

For example, consider the following contrived REST+HTML specification.

1. Resources store arbitrary key/value pairs with unique keys
   (represented as a Python dict).

2. A GET request returns colon-separated key/value pairs in ``<div>``
   elements.

3. A PUT request accepts colon-separated key/value pairs in ``<div>``
   elements.

4. An index resource provides an HTML anchor tag (hypertext link) to objects
   which it indexes (where the keys represent the names and the values
   represent the link).

A REST+HTML implementation was chosen for this example as HTML defines
relative links, which keeps the example simple yet functional.

Complete Example
----------------

Brining the above code samples together and adding some basic
configuration results in the following program, which can be run
directly::

    import cherrypy

    class Resource(object):
        
        def __init__(self, content):
            self.content = content
        
        exposed = True
        
        def GET(self):
            return self.to_html()
        
        def PUT(self):
            self.content = self.from_html(cherrypy.request.body.read())

        def to_html(self):
            html_item = lambda (name,value): '<div>{name}:{value}</div>'.format(\*\*vars())
            items = map(html_item, self.content.items())
            items = ''.join(items)
            return '<html>{items}</html>'.format(**vars())

        @staticmethod
        def from_html(data):
            pattern = re.compile(r'\<div\>(?P<name>.*?)\:(?P<value>.*?)\</div\>')
            items = [match.groups() for match in pattern.finditer(data)]
            return dict(items)

    class ResourceIndex(Resource):
        def to_html(self):
            html_item = lambda (name,value): '<div><a href="{value}">{name}</a></div>'.format(\*\*vars())
            items = map(html_item, self.content.items())
            items = ''.join(items)
            return '<html>{items}</html>'.format(**vars())

    class Root(object):
        pass

    root = Root()

    root.sidewinder = Resource({'color': 'red', 'weight': 176, 'type': 'stable'})
    root.teebird = Resource({'color': 'green', 'weight': 173, 'type': 'overstable'})
    root.blowfly = Resource({'color': 'purple', 'weight': 169, 'type': 'putter'})
    root.resource_index = ResourceIndex({'sidewinder': 'sidewinder', 'teebird': 'teebird', 'blowfly': 'blowfly'})

    conf = {
        'global': {
            'server.socket_host': '0.0.0.0',
            'server.socket_port': 8000,
        },
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        }
    }

    cherrypy.quickstart(root, '/', conf)

Conclusion
==========

CherryPy provides a straightforward interface for readily creating
RESTful interfaces.