From 24eebd8428c0226f586e39a015bde2acd3c5625e Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 30 Jan 2013 16:07:08 +0100 Subject: [PATCH] Document the tg1x and cornice adapters --- doc/integrate.rst | 212 ++++++++++++++++++++++++++++++--------------- wsmeext/cornice.py | 2 +- 2 files changed, 141 insertions(+), 73 deletions(-) diff --git a/doc/integrate.rst b/doc/integrate.rst index b0e0a29..00d2720 100644 --- a/doc/integrate.rst +++ b/doc/integrate.rst @@ -15,7 +15,8 @@ This decorator can have two different names depending on the adapter. take care of calling the adequate decorators of the framework. Generally this decorator is provided for frameworks that use - object-dispatch controllers, like Pecan_ or Turbogears_. + object-dispatch controllers, such as :ref:`adapter-pecan` and + :ref:`adapter-tg1`. ``@signature`` This decorator only set the function signature and returns a function @@ -23,7 +24,7 @@ This decorator can have two different names depending on the adapter. Generally this decorator is provided for frameworks that expects functions taking a request object as a single parameter and returning a response - object. This is the case of cornice_. + object. This is the case of :ref:`adapter-cornice`. Additionnaly, if you want to enable additionnal protocols, you will need to mount a :class:`WSRoot` instance somewhere in the application, generally @@ -59,51 +60,80 @@ WSME, which is the case if you write a WSME standalone application. application = root.wsgiapp() -Bottle ------- - -No adapter is provided yet but it should not be hard to write one, by taking -example on the cornice adapter. - -This example only show how to mount a WSRoot inside a bottle application. - -.. code-block:: python - - import bottle - import wsme - - class MyRoot(wsme.WSRoot): - @wsme.expose(unicode) - def helloworld(self): - return u"Hello World !" - - root = MyRoot(webpath='/ws', protocols=['restjson']) - - bottle.mount('/ws', root.wsgiapp()) - bottle.run() - -Pyramid -------- - -The recommended way of using WSME inside Pyramid is to use cornice. +.. _adapter-cornice: Cornice ------- +.. _cornice: http://cornice.readthedocs.org/en/latest/ + + *"* Cornice_ *provides helpers to build & document REST-ish Web Services with + Pyramid, with decent default behaviors. It takes care of following the HTTP + specification in an automated way where possible."* + + +:mod:`wsmeext.cornice` -- Cornice adapter +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. module:: wsmeext.cornice + +.. function:: signature + + Declare the parameters of a function and returns a function suitable for + cornice (ie that takes a request and returns a response). + +Example +~~~~~~~ + +.. code-block:: python + + from cornice import Service + from wsmeext.cornice import signature + import wsme.types + + hello = Service(name='hello', path='/', description="Simplest app") + + class Info(wsme.types.Base): + message = wsme.types.text + + + @hello.get() + @signature(Info) + def get_info(): + """Returns Hello in JSON or XML.""" + return Info(message='Hello World') + + + @hello.post() + @signature(None, Info) + def set_info(info): + print("Got a message: %s" % info.message) + + +.. _adapter-pecan: + Pecan ----- + *"*\ Pecan_ *was created to fill a void in the Python web-framework world – + a very lightweight framework that provides object-dispatch style routing. + Pecan does not aim to be a "full stack" framework, and therefore includes + no out of the box support for things like sessions or databases. Pecan + instead focuses on HTTP itself."* + .. warning:: A pecan application is not able to mount another wsgi application on a - subpath. For that reason, additional protocols are not supported for now. + subpath. For that reason, additional protocols are not supported for now, + ie until wsme provides a middleware that can do the same as a mounted + WSRoot. -Api -~~~ +:mod:`wsmeext.pecan` -- Pecan adapter +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. module:: wsmeext.pecan -.. function:: wsexpose(return_type, \*arg_types, **options) +.. function:: wsexpose(return_type, \*arg_types, \*\*options) See @\ :func:`signature` for parameters documentation. @@ -132,70 +162,108 @@ The `example