This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!
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 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.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.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'})
        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.

Release History

Release History

2.1.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

2.0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

2.0.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.4

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
sprockets.mixins.mediatype-2.1.0-py2.py3-none-any.whl (14.0 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Mar 16, 2016
sprockets.mixins.mediatype-2.1.0.tar.gz (17.0 kB) Copy SHA256 Checksum SHA256 Source Mar 16, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting