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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
Protocols
=========
In this document the same webservice example will be used to
illustrate the different protocols. Its source code is in the
last chapter (:ref:`protocols-the-example`).
REST
----
.. note::
This chapter applies also for the different adapters, not only the native
REST implementation.
The two REST protocols share common characterics.
Each function corresponds to distinct webpath that starts with the
root webpath, followed by the controllers names if any, and finally
the function name.
For example, the functions exposed functions will be mapped to the
following paths :
- ``/ws/persons/create``
- ``/ws/persons/get``
- ``/ws/persons/list``
- ``/ws/persons/update``
- ``/ws/persons/destroy``
In addition to this trivial function mapping, a `method` option can
be given to the `expose` decorator. In such a case, the function
name can be omitted by the caller, and the dispatch will look at the
http method used in the request to select the correct function.
The function parameters can be transmitted in two ways (is using
the http method to select the function, one way or the other
may be usable) :
#. As a GET query string or POST form parameters.
Simple types are straight forward :
``/ws/person/get?id=5``
Complex types can be transmitted this way:
``/ws/person/update?p.id=1&p.name=Ross&p.hobbies[0]=Dinausaurs&p.hobbies[1]=Rachel``
#. In a Json or XML encoded POST body (see below)
The result will be return Json or XML encoded (see below).
In case of error, a 400 or 500 status code is returned, and the
response body contains details about the error (see below).
REST+Json
---------
:name: ``'restjson'``
Implements a REST+Json protocol.
This protocol is selected if:
- The request content-type is either text/javascript or application/json
- The request 'Accept' header contains 'text/javascript' or 'application.json'
- A trailing '.json' is added to the path
- A 'wsmeproto=restjson' is added in the query string
Options
~~~~~~~
:nest_result: Nest the encoded result in a result param of an object.
For example, a result of ``2`` would be ``{'result': 2}``
Types
~~~~~
+---------------+-------------------------------+
| Type | Json type |
+===============+===============================+
| ``str`` | String |
+---------------+-------------------------------+
| ``unicode`` | String |
+---------------+-------------------------------+
| ``int`` | Number |
+---------------+-------------------------------+
| ``float`` | Number |
+---------------+-------------------------------+
| ``bool`` | Boolean |
+---------------+-------------------------------+
| ``Decimal`` | String |
+---------------+-------------------------------+
| ``date`` | String (YYYY-MM-DD) |
+---------------+-------------------------------+
| ``time`` | String (hh:mm:ss) |
+---------------+-------------------------------+
| ``datetime`` | String (YYYY-MM-DDThh:mm:ss) |
+---------------+-------------------------------+
| Arrays | Array |
+---------------+-------------------------------+
| None | null |
+---------------+-------------------------------+
| Complex types | Object |
+---------------+-------------------------------+
Return
~~~~~~
The json encoded result when the response code is 200, OR a json object
with error properties ('faulcode', 'faultstring' and 'debuginfo' if
available).
For example, the /ws/person/get result looks like:
.. code-block:: javascript
{
'id': 2
'fistname': 'Monica',
'lastname': 'Geller',
'age': 28,
'hobbies': [
'Food',
'Cleaning'
]
}
And in case of error:
.. code-block:: javascript
{
'faultcode': 'Client',
'faultstring': 'id is missing'
}
REST+XML
--------
:name: ``'restxml'``
This protocol is selected if
- The request content-type is text/xml
- The request 'Accept' header contains 'text/xml'
- A trailing '.xml' is added to the path
- A 'wsmeproto=restxml' is added in the query string
Types
~~~~~
+---------------+----------------------------------------+
| Type | XML example |
+===============+========================================+
| ``str`` | .. code-block:: xml |
| | |
| | <value>a string</value> |
+---------------+----------------------------------------+
| ``unicode`` | .. code-block:: xml |
| | |
| | <value>a string</value> |
+---------------+----------------------------------------+
| ``int`` | .. code-block:: xml |
| | |
| | <value>5</value> |
+---------------+----------------------------------------+
| ``float`` | .. code-block:: xml |
| | |
| | <value>3.14</value> |
+---------------+----------------------------------------+
| ``bool`` | .. code-block:: xml |
| | |
| | <value>true</value> |
+---------------+----------------------------------------+
| ``Decimal`` | .. code-block:: xml |
| | |
| | <value>5.46</value> |
+---------------+----------------------------------------+
| ``date`` | .. code-block:: xml |
| | |
| | <value>2010-04-27</value> |
+---------------+----------------------------------------+
| ``time`` | .. code-block:: xml |
| | |
| | <value>12:54:18</value> |
+---------------+----------------------------------------+
| ``datetime`` | .. code-block:: xml |
| | |
| | <value>2010-04-27T12:54:18</value> |
+---------------+----------------------------------------+
| Arrays | .. code-block:: xml |
| | |
| | <value> |
| | <item>Dinausaurs<item> |
| | <item>Rachel<item> |
| | </value> |
+---------------+----------------------------------------+
| None | .. code-block:: xml |
| | |
| | <value nil="true"/> |
+---------------+----------------------------------------+
| Complex types | .. code-block:: xml |
| | |
| | <value> |
| | <id>1</id> |
| | <fistname>Ross</fistname> |
| | </value> |
+---------------+----------------------------------------+
Return
~~~~~~
A xml tree with a top 'result' element.
.. code-block:: xml
<result>
<id>1</id>
<firstname>Ross</firstname>
<lastname>Geller</lastname>
</result>
Errors
~~~~~~
A xml tree with a top 'error' element, having 'faultcode', 'faultstring'
and 'debuginfo' subelements:
.. code-block:: xml
<error>
<faultcode>Client</faultcode>
<faultstring>id is missing</faultstring>
</error>
SOAP
----
:name: ``'soap'``
Implements the SOAP protocol.
A wsdl definition of the webservice is available at the 'api.wsdl' subpath.
(``/ws/api.wsdl`` in our example).
The protocol is selected if the request match one of the following condition:
- The Content-Type is 'application/soap+xml'
- A header 'Soapaction' is present
Options
~~~~~~~
:tns: Type namespace
ExtDirect
---------
:name: ``extdirect``
Implements the `Ext Direct`_ protocol.
The provider definition is made available at the ``/extdirect/api.js`` subpath.
The router url is ``/extdirect/router[/subnamespace]``.
Options
~~~~~~~
:namespace: Base namespace of the api. Used for the provider definition.
:params_notation: Default notation for function call parameters. Can be
overriden for individual functions by adding the
``extdirect_params_notation`` extra option to @expose.
The possible notations are :
- ``'named'`` -- The function will take only one object parameter
in which each property will be one of the parameters.
- ``'positional'`` -- The function will take as many parameters as
the function has, and their position will determine which parameter
they are.
expose extra options
~~~~~~~~~~~~~~~~~~~~
:extdirect_params_notation: Override the params_notation for a particular
function.
.. _Ext Direct: http://www.sencha.com/products/extjs/extdirect
.. _protocols-the-example:
The example
-----------
In this document the same webservice example will be used to
illustrate the different protocols:
.. code-block:: python
class Person(object):
id = int
lastname = unicode
firstname = unicode
age = int
hobbies = [unicode]
def __init__(self, id=None, lastname=None, firstname=None, age=None,
hobbies=None):
if id:
self.id = id
if lastname:
self.lastname = lastname
if firstname:
self.firstname = firstname
if age:
self.age = age
if hobbies:
self.hobbies = hobbies
persons = {
1: Person(1, "Geller", "Ross", 30, ["Dinosaurs", "Rachel"]),
2: Person(2, "Geller", "Monica", 28, ["Food", "Cleaning"])
}
class PersonController(object):
@expose(Person)
@validate(int)
def get(self, id):
return persons[id]
@expose([Person])
def list(self):
return persons.values()
@expose(Person)
@validate(Person)
def update(self, p):
if p.id is Unset:
raise ClientSideError("id is missing")
persons[p.id] = p
return p
@expose(Person)
@validate(Person)
def create(self, p):
if p.id is not Unset:
raise ClientSideError("I don't want an id")
p.id = max(persons.keys()) + 1
persons[p.id] = p
return p
@expose()
@validate(int)
def destroy(self, id):
if id not in persons:
raise ClientSideError("Unknown ID")
class WS(WSRoot):
person = PersonController()
root = WS(webpath='ws')
|