Handles Content-Type & Accept header serialization and deserialization for you
Find a file
Gavin M. Roy 0c1c757b37 Merge pull request #11 from sprockets/remove-deprecation
Remove _mark_deprecated wrapper.
2016-02-29 14:39:09 -05:00
docs Remove _mark_deprecated wrapper. 2016-02-29 14:35:07 -05:00
requires Add s.m.mediatype.transcoders.MsgPackTranscoder. 2016-01-18 07:58:07 -05:00
sprockets Remove _mark_deprecated wrapper. 2016-02-29 14:35:07 -05:00
.gitignore Initial commit 2015-06-09 09:28:29 -04:00
.travis.yml Add Python3.5 to test coverage. 2016-01-10 11:37:51 -05:00
examples.py Make content-type parameter to add_transcoder optional. 2016-02-01 10:06:46 -05:00
LICENSE Happy New Year, the 2016 edition. 2016-01-10 13:52:31 -05:00
MANIFEST.in RST 2015-08-07 17:14:37 -04:00
README.rst Make content-type parameter to add_transcoder optional. 2016-02-01 10:06:46 -05:00
setup.cfg Add Python3.5 to test coverage. 2016-01-10 11:37:51 -05:00
setup.py Repackage sprockets.mixins.mediatype into a package. 2016-01-10 11:36:46 -05:00
tests.py ContentMixin: Raise 400 when body decode fails. 2016-02-22 09:58:58 -05:00
tox.ini tox.ini: Fix skip missing interpreters flag. 2016-01-16 09:56:38 -05:00

sprockets.mixins.mediatype
==========================
A mixin that performs Content-Type negotiation and request/response
(de)serialization.

|Documentation| |Build Badge| |Package Info|

This mix-in adds two methods to a ``tornado.web.RequestHandler`` instance:

- ``get_request_body() -> dict``: deserializes the request body according
  to the HTTP ``Content-Type`` header and returns the deserialized body.
- ``send_response(object)``: serializes the response into the content type
  requested by the ``Accept`` header.

Support for a content types is enabled by calling ``add_binary_content_type``,
``add_text_content_type`` or the ``add_transcoder`` functions with the
``tornado.web.Application`` instance, the content type, encoding and decoding
functions as parameters:

.. code-block:: python

   import json

   from sprockets.mixins.mediatype import content
   from tornado import web

   def make_application():
       application = web.Application([
           # insert your handlers here
       ])

       content.add_text_content_type(application,
                                     'application/json', 'utf-8',
                                     json.dumps, json.loads)

       return application

The *add content type* functions will add a attribute to the ``Application``
instance that the mix-in uses to manipulate the request and response bodies.
The *add_transcoder* function is similar except that it takes an object
that implements transcoding methods instead of simple functions.  The
``transcoders`` module includes ready-to-use transcoders for a few content
types:

.. code-block:: python

   from sprockets.mixins.mediatype import content, transcoders
   from tornado import web

   def make_application():
       application = web.Application([
           # insert your handlers here
       ])

       content.add_transcoder(application, transcoders.JSONTranscoder())

       return application

In either case, the ``ContentMixin`` uses the registered content type
information to provide transparent content type negotiation for your
request handlers.

.. code-block:: python

   from sprockets.mixins.mediatype import content
   from tornado import web

   class SomeHandler(content.ContentMixin, web.RequestHandler):
       def get(self):
           self.send_response({'data': 'value'})
           self.finish()

       def post(self):
           body = self.get_request_body()
           # do whatever
           self.send_response({'action': 'performed'})
           self.finish()

Based on the settings stored in the ``Application`` instance and the HTTP
headers, the request and response data will be handled correctly or the
appropriate HTTP exceptions will be raised.

.. |Documentation| image:: https://readthedocs.org/projects/sprocketsmixinsmedia-type/badge/?version=latest
   :target: https://sprocketsmixinsmedia-type.readthedocs.org/
.. |Build Badge| image:: https://travis-ci.org/sprockets/sprockets.mixins.media_type.svg
   :target: https://travis-ci.org/sprockets/sprockets.mixins.media_type
.. |Package Info| image:: https://img.shields.io/pypi/v/sprockets.mixins.mediatype.svg
   :target: https://pypi.python.org/pypi/sprockets.mixins.mediatype