sprockets.mixins.mediatype
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.
Before adding support for specific content types, you SHOULD install the content settings into your tornado.web.Application instance. If you don’t install the content settings, then an instance will be created for you by the mix-in; however, the created instance will be empty. You should already have a function that creates the Application instance. If you don’t, now is a good time to add one.
from sprockets.mixins.mediatype import content
from tornado import web
def make_application():
application = web.Application([
# insert your handlers here
])
content.install(application, 'application/json', 'utf-8')
return application
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:
import json
from sprockets.mixins.mediatype import content
from tornado import web
def make_application():
application = web.Application([
# insert your handlers here
])
content.install(application, 'application/json', 'utf-8')
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:
from sprockets.mixins.mediatype import content, transcoders
from tornado import web
def make_application():
application = web.Application([
# insert your handlers here
])
content.install(application, 'application/json', 'utf-8')
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.
from sprockets.mixins.mediatype import content
from tornado import web
class SomeHandler(content.ContentMixin, web.RequestHandler):
def get(self):
self.send_response({'data': 'value'})
def post(self):
body = self.get_request_body()
# do whatever
self.send_response({'action': 'performed'})
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.
Metadata
Release files for sprockets.mixins.mediatype 3.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sprockets.mixins.mediatype-3.0.4.tar.gz | 19.6 kB | Without provenance | Details for sprockets.mixins.mediatype-3.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sprockets.mixins.mediatype-3.0.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for sprockets.mixins.mediatype-3.0.4-py2.py3-none-any.whl |
Total release size: 32.3 kB
Release files / sprockets.mixins.mediatype-3.0.4.tar.gz
| Download URL | sprockets.mixins.mediatype-3.0.4.tar.gz |
|---|---|
| Size | 19.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c081fab291d851e934197bfbaf0605e7beabe46e761ce599f39b5a245152107
|
|
BLAKE2b-256 checksum How to use checksums |
c75e870ff37044aba890b58e668884470392499661f27ba8740c19dc2aedea50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0
|
Release files / sprockets.mixins.mediatype-3.0.4-py2.py3-none-any.whl
| Download URL | sprockets.mixins.mediatype-3.0.4-py2.py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c58a95939faef423aaf0391ec661c0a03b95dfe8d5a523e3b6f29a3b550fc94e
|
|
BLAKE2b-256 checksum How to use checksums |
79b7447693f93eed809edaa77bfb282d5faa37cd221b4b5adcb274884094a17c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0
|
Security for sprockets.mixins.mediatype 3.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.