Skip to main content

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:

# 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 = 'json'
    mimetypes = ['text/html']

    def configure(self, template):
        self.template = template

    def render(self, obj):
        return render(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/<id>')
@app.route('/posts/<id>.<format>')
@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 and Pylons, 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.

Metadata

Release files for negotiate 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for negotiate 0.0.1
File Size Uploaded Provenance Actions
negotiate-0.0.1.tar.gz 4.9 kB Without provenance Details for negotiate-0.0.1.tar.gz

Release files / negotiate-0.0.1.tar.gz

Download URL negotiate-0.0.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8dbb81a77beda735605a1b16592291524912a7b94fe868c7404f80d901fb66f3
BLAKE2b-256 checksum
How to use checksums
b65fe8905340f93d244508f7613c1e2efdd5f61dd79915e1294250789c0ab69f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for negotiate 0.0.1

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.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page