sprockets.mixins.mediatype/README.rst

67 lines
2.4 KiB
ReStructuredText
Raw Normal View History

sprockets.mixins.mediatype
==========================
2015-08-19 22:29:32 +00:00
A mixin that performs Content-Type negotiation and request/response
(de)serialization.
2015-06-08 20:28:30 +00:00
2015-08-26 19:10:35 +00:00
|Documentation| |Build Badge| |Package Info|
2015-08-19 22:29:32 +00:00
This mix-in adds two methods to a ``tornado.web.RequestHandler`` instance:
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
- ``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.
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
Support for a content types is enabled by calling either the
``add_binary_content_type`` or ``add_text_content_type`` function with the
``tornado.web.Application`` instance, the content type, encoding and decoding
functions as parameters:
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
.. code-block:: python
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
import json
2015-06-08 20:28:30 +00:00
from sprockets.mixins.mediatype import content
2015-08-19 22:29:32 +00:00
from tornado import web
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
def make_application():
application = web.Application([
# insert your handlers here
])
2015-06-08 20:28:30 +00:00
content.add_text_content_type(application,
'application/json', 'utf-8',
json.dumps, json.loads)
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
return application
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
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.
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
.. code-block:: python
2015-06-08 20:28:30 +00:00
from sprockets.mixins.mediatype import content
2015-08-19 22:29:32 +00:00
from tornado import web
2015-06-08 20:28:30 +00:00
class SomeHandler(content.ContentMixin, web.RequestHandler):
2015-08-19 22:29:32 +00:00
def get(self):
self.send_response({'data': 'value'})
self.finish()
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
def post(self):
body = self.get_request_body()
# do whatever
self.send_response({'action': 'performed'})
self.finish()
2015-06-08 20:28:30 +00:00
2015-08-19 22:29:32 +00:00
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.
2015-08-26 19:10:35 +00:00
.. |Documentation| image:: https://readthedocs.org/projects/sprocketsmixinsmedia-type/badge/?version=latest
:target: https://sprocketsmixinsmedia-type.readthedocs.org/
2015-08-26 19:10:35 +00:00
.. |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
2015-09-09 22:46:32 +00:00
:target: https://pypi.python.org/pypi/sprockets.mixins.mediatype