Negotiates and handles Content-Type / Accept header negotiation for you.
Too often we roll our own serialization middleware based on Content-Type or Accept HTTP headers, and too often it's done incorrectly. This library aims to provide a very simple interface for handling this task for you so you can spend your time elsewhere.
pip install arbitrator
>>> import json
>>> import msgpack
>>> from tornado import web
>>> class MyHandler(arbitrator.MediaTypeMixn, web.RequestHandler):
...
... def initalize(self):
... media_types = {
... 'application/json': json,
... 'application/msgpack': msgpack,
... 'application/x-msgpack': msgpack
... }
... super(MyHandler, self).initalize(supported_media_types=media_types)
...
... def get(self, account_id):
... payload = self.load_payload(
... self.request.headers['Content-Type'],
... self.request.content,
... )
... # ... do stuff with the payload dictionary
... content_type, stringified = self.dump_payload(
... self.request.headers['Accept'],
... payload,
... )
... self.set_status(200)
... self.set_header('Content-Type', content_type)
... self.finish(stringified)
If you want to help make this project better you are officially an awesome person.
Pull requests or Github issues are always welcome. If you want to contribute a patch please do the following.
- Fork this repo and create a new branch
- Do work
- Add tests for your work (Mandatory)
- Submit a pull request
- Wait for Coveralls and Travis-CI to run through your PR
- I'll review it and merge it
As a note, code without sufficient tests will not be merged.