https://github.com/nickstenning/negotiate
Smart, simple content negotiation for Python web applications
https://github.com/nickstenning/negotiate
Last synced: about 1 year ago
JSON representation
Smart, simple content negotiation for Python web applications
- Host: GitHub
- URL: https://github.com/nickstenning/negotiate
- Owner: nickstenning
- Created: 2012-04-01T23:39:42.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2013-01-23T21:37:31.000Z (over 13 years ago)
- Last Synced: 2025-04-12T01:04:03.429Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 109 KB
- Stars: 10
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.mkd
Awesome Lists containing this project
README
negotiate
=========
Smart, simple content negotiation for Python web applications.
Content negotiation can be difficult to do well. Ideally, your code should be
DRY, and you wouldn't be repeating the same old boilerplate in multiple view
methods in order to emit the same domain object in different formats.
``negotiate`` helps make your life even easier by allowing you to decorate
your view methods with formatters that automatically translate your domain
objects into the format requested by the client.
It's really simple to use. Hopefully this example (for a Flask application)
makes the main points clear:
```python
# First, we write a couple of formatters that specify how to translate the
# output of the view function into a particular format. Here we define a
# JSON formatter and an HTML formatter that takes a template parameter.
from negotiate.flask import Formatter
class JSONFormatter(Formatter):
format = 'json'
mimetypes = ['application/json']
def render(self, obj):
return json.dumps(obj)
class HTMLFormatter(Formatter):
format = 'html'
mimetypes = ['text/html']
def configure(self, template):
self.template = template
def render(self, obj):
return render_template(self.template, **obj)
# Then, when building the application, we decorate the view function with the
# "negotiate" decorator, listing the formats in which this view is available.
from negotiate.flask import negotiate
@app.route('/posts/')
@app.route('/posts/.')
@negotiate(JSONFormatter)
@negotiate(HTMLFormatter, template='post.html')
def view_post(id, format=None):
post = Posts.by_id(id)
if post is None:
abort(404)
if not g.user.authorize('read', post):
abort(401)
return {'post': post}
```
The result is a view action that will return an HTML version of the post by default (i.e. with `Accept: */*` and no explicit format), or if the `.html` extension is explicitly specified, or a JSON version of the post if the `.json` extension is given or `Accept: application/json` is sent with the request.
Support
-------
`negotiate` currently supports [Flask](http://flask.pocoo.org) and [Pylons](http://pylonshq.com), although adding support for other web frameworks should be pretty easy. Have a look at `negotiate/flask.py` and `negotiate/pylons.py` to see the small amount of integration code required.