<commit_before>Protocols
=========

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')

REST
----

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 a near future, an additional expose option `restverb` will allow
to use the HTTP verb to select the function, in which case the path
will not containt the function name.

The function parameters can be transmitted in two ways :

#.  As a GET query string or POST form parameters.

    Simple types are straight forward :

    ``/ws/person/get?id=5``

    Complex types, although not yet implemented, should work 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).

REST+Json
~~~~~~~~~

:name: ``'restjson'``

Implements a REST+Json protocol.

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
''''''

A json object with a single 'result' property, 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

    {
        'result': {
            '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'``

SOAP
----

:name: ``'soap'``
:package: WSME-Soap

Implements the SOAP protocol.

Options
~~~~~~~

:tns: Type namespace

ExtDirect
---------

:name: ``extdirect``
:package: WSME-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
~~~~~~~<commit_msg>Documented REST+XML and a bit SOAP<commit_after>Protocols
=========

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')

REST
----

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 a near future, an additional expose option `restverb` will allow
to use the HTTP verb to select the function, in which case the path
will not containt the function name.

The function parameters can be transmitted in two ways :

#.  As a GET query string or POST form parameters.

    Simple types are straight forward :

    ``/ws/person/get?id=5``

    Complex types, although not yet implemented, should work 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

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
~~~~~~

A json object with a single 'result' property, 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

    {
        'result': {
            '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``     | <value>3.14</value>                |
+---------------+------------------------------------+
| ``bool``      | <value>true</value>                |
+---------------+------------------------------------+
| ``Decimal``   | <value>5.46</value>                |
+---------------+------------------------------------+
| ``date``      | <value>2010-04-27</value>          |
+---------------+------------------------------------+
| ``time``      | <value>12:54:18</value>            |
+---------------+------------------------------------+
| ``datetime``  | <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>1</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'``
:package: WSME-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``
:package: WSME-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
~~~~~~~
{'fraction_non_alphanumeric': 0.2525664559113468, 'fraction_numerical': 0.005166904616221361, 'mean_word_length': 1.1734633569739952, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 17}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4693075', 'n_tokens_mistral': 3944, 'n_tokens_neox': 3686, 'n_words': 1066}