{ "info": { "author": "Nick Stenning", "author_email": "nick@whiteink.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Framework :: Pylons", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware" ], "description": "negotiate\r\n#########\r\n\r\nSmart, simple content negotiation for Python web applications.\r\n\r\nContent negotiation can be difficult to do well. Ideally, your code should be\r\nDRY, and you wouldn't be repeating the same old boilerplate in multiple view\r\nmethods in order to emit the same domain object in different formats.\r\n``negotiate`` helps make your life even easier by allowing you to decorate\r\nyour view methods with formatters that automatically translate your domain\r\nobjects into the format requested by the client.\r\n\r\nIt's really simple to use. Hopefully this example (for a Flask application)\r\nmakes the main points clear::\r\n\r\n # First, we write a couple of formatters that specify how to translate the\r\n # output of the view function into a particular format. Here we define a\r\n # JSON formatter and an HTML formatter that takes a template parameter.\r\n\r\n from negotiate.flask import Formatter\r\n\r\n class JSONFormatter(Formatter):\r\n format = 'json'\r\n mimetypes = ['application/json']\r\n\r\n def render(self, obj):\r\n return json.dumps(obj)\r\n\r\n class HTMLFormatter(Formatter):\r\n format = 'json'\r\n mimetypes = ['text/html']\r\n\r\n def configure(self, template):\r\n self.template = template\r\n\r\n def render(self, obj):\r\n return render(self.template, **obj)\r\n\r\n # Then, when building the application, we decorate the view function with the\r\n # \"negotiate\" decorator, listing the formats in which this view is available.\r\n\r\n from negotiate.flask import negotiate\r\n\r\n @app.route('/posts/')\r\n @app.route('/posts/.')\r\n @negotiate(JSONFormatter)\r\n @negotiate(HTMLFormatter, template='post.html')\r\n def view_post(id, format=None):\r\n post = Posts.by_id(id)\r\n\r\n if post is None:\r\n abort(404)\r\n if not g.user.authorize('read', post):\r\n abort(401)\r\n\r\n return {'post': post}\r\n\r\nThe 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.\r\n\r\nSupport\r\n*******\r\n\r\n``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.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/nickstenning/negotiate", "keywords": "contenttype contentnegotiation http hypermedia", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "negotiate", "package_url": "https://pypi.org/project/negotiate/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/negotiate/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/nickstenning/negotiate" }, "release_url": "https://pypi.org/project/negotiate/0.0.1/", "requires_dist": null, "requires_python": null, "summary": "Negotiate: smart, simple content negotiation for Python web applications", "version": "0.0.1" }, "last_serial": 795225, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "2fbd9f86d634d4ce690c63743489d5df", "sha256": "8dbb81a77beda735605a1b16592291524912a7b94fe868c7404f80d901fb66f3" }, "downloads": -1, "filename": "negotiate-0.0.1.tar.gz", "has_sig": false, "md5_digest": "2fbd9f86d634d4ce690c63743489d5df", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4857, "upload_time": "2012-04-02T02:26:50", "url": "https://files.pythonhosted.org/packages/b6/5f/e8905340f93d244508f7613c1e2efdd5f61dd79915e1294250789c0ab69f/negotiate-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2fbd9f86d634d4ce690c63743489d5df", "sha256": "8dbb81a77beda735605a1b16592291524912a7b94fe868c7404f80d901fb66f3" }, "downloads": -1, "filename": "negotiate-0.0.1.tar.gz", "has_sig": false, "md5_digest": "2fbd9f86d634d4ce690c63743489d5df", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4857, "upload_time": "2012-04-02T02:26:50", "url": "https://files.pythonhosted.org/packages/b6/5f/e8905340f93d244508f7613c1e2efdd5f61dd79915e1294250789c0ab69f/negotiate-0.0.1.tar.gz" } ] }