sprockets.mixins.mediatype/README.rst

94 lines
3.1 KiB
ReStructuredText
Raw Normal View History

2015-06-08 20:28:30 +00: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
-------------
2015-06-09 13:35:09 +00:00
http://sprocketsmixinsmedia-type.readthedocs.org/en/latest/
2015-06-08 20:28:30 +00:00
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
---------------
2015-06-09 13:35:09 +00:00
Available at http://sprocketsmixinsmedia-type.readthedocs.org/en/latest//en/latest/history.html
2015-06-08 20:28:30 +00:00
.. |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
2015-06-09 13:38:56 +00:00
.. |License| image:: http://img.shields.io/:license-mit-blue.svg
:target: http://doge.mit-license.org