Handles Content-Type & Accept header serialization and deserialization for you
Find a file
Dave Shawley e5023a420f RST
2015-08-07 17:14:37 -04:00
docs Fix up badges a bit 2015-06-09 09:42:21 -04:00
requires RST 2015-08-07 17:14:37 -04:00
sprockets RST 2015-08-07 17:14:37 -04:00
.gitignore Initial commit 2015-06-09 09:28:29 -04:00
.travis.yml Fix up badges a bit 2015-06-09 09:42:21 -04:00
LICENSE Initial commit 2015-06-09 09:28:29 -04:00
MANIFEST.in RST 2015-08-07 17:14:37 -04:00
README.rst Fix up badges a bit 2015-06-09 09:42:21 -04:00
setup.cfg Update setup.cfg to include coverage 2015-06-09 10:29:35 -04:00
setup.py RST 2015-08-07 17:14:37 -04:00
tests.py Initial commit 2015-06-09 09:28:29 -04:00

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

|Version| |Downloads| |Status| |Coverage| |License|

Installation
------------
``sprockets.mixins.media_type`` is available on the
`Python Package Index <https://pypi.python.org/pypi/sprockets.mixins.media_type>`_
and can be installed via ``pip`` or ``easy_install``:

.. code:: bash

  pip install sprockets.mixins.media_type

Documentation
-------------
http://sprocketsmixinsmedia-type.readthedocs.org/en/latest/

Example
-------
The following example demonstrates how to use the Mix-in to handle media
type validation and serialization.

.. code:: python

    from tornado import web, gen
    from sprockets.mixins import media_type


    class MyRequestHandler(media_type.MediaTypeMixin, web.RequestHandler):

            @gen.coroutine
            def post(self, **kwargs):
                # Validate the Content-Type header using the Mix-in
                if not self.is_valid_content_type():
                    self.set_status(415, 'Unsupported content type')
                    self.finish()
                    return

                # Deserialize your request payload
                data = self.decode_request()

                # Ensure that you get some data out of it!
                if not data:
                    self.set_status(400)
                    self.finish()
                    return

                # Manipulate your data and do business stuff with it
                data.pop('the_key')

                self.set_status(200)

                # Automatically serialize your data using the HTTP Accept headers
                self.write(data)

            @gen.coroutine
            def get(self, some_id):
                # Validate the Accept headers using the Mix-in
                if not self.is_valid_accept_header():
                    self.set_status(406, 'Invalid Accept header')
                    self.finish()
                    return

                # Maybe do some lookups from the database or get some data from somewhere
                data = {'some_id': some_id}

                self.set_status(200)

                # Automatically serialize your data using the HTTP Accept headers
                self.write(data)


Version History
---------------
Available at http://sprocketsmixinsmedia-type.readthedocs.org/en/latest//en/latest/history.html

.. |Version| image:: https://img.shields.io/pypi/v/sprockets.mixins.media_type.svg?
   :target: http://badge.fury.io/py/sprockets.mixins.media_type

.. |Status| image:: https://img.shields.io/travis/sprockets/sprockets.mixins.media_type.svg?
   :target: https://travis-ci.org/sprockets/sprockets.mixins.media_type

.. |Coverage| image:: https://img.shields.io/codecov/c/github/sprockets/sprockets.mixins.media_type.svg?
   :target: https://codecov.io/github/sprockets/sprockets.mixins.media_type?branch=master

.. |Downloads| image:: https://img.shields.io/pypi/dm/sprockets.mixins.media_type.svg?
   :target: https://pypi.python.org/pypi/sprockets.mixins.media_type

.. |License| image:: http://img.shields.io/:license-mit-blue.svg
   :target: http://doge.mit-license.org