A mixin for reporting handling content-type/accept headers
Project description
A mixin that performs Content-Type negotiation and request/response (de)serialization.
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 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:
import json
from sprockets.mixins import mediatype
from tornado import web
def make_application():
application = web.Application([
# insert your handlers here
])
mediatype.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.
from sprockets.mixins import mediatype
from tornado import web
class SomeHandler(mediatype.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sprockets.mixins.mediatype-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f24d14fc504db0cb1a002cf212297562b911f6514b8cc5f12ca5fe6e984fcb4 |
|
MD5 | d05d64a3e61592a0c316cf3a3c42a3d4 |
|
BLAKE2b-256 | 8dd1790e2dbeab86ebaff14dd3afc8d1ad7eaba57d18161b6e26b3d06cf10d61 |
Hashes for sprockets.mixins.mediatype-1.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 333dd7a77fc1e917e7ef06001313730f99b31b259aaf6ab5b09ac82e4e749711 |
|
MD5 | 1ba8890348b05c08d6400ea3635385e6 |
|
BLAKE2b-256 | 1834dfa3f5177d30f732224c5cfc58585277717346273cab61bfee120d13a14b |