{ "info": { "author": "Richard Jones", "author_email": "richard@cottagelabs.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Negotiator\r\n==========\r\n\r\nProper Content Negotiation for Python\r\n\r\nIntroduction\r\n------------\r\n\r\nNegotiator offers a framework for making content negotiation decisions based on the HTTP accept headers.\r\n\r\nNOTE it currently only formally supports Accept and Accept-Language, but it is a short haul to support for Accept-Charset and Accept-Encoding (TODO)\r\n\r\n\r\nBasic Usage\r\n-----------\r\n\r\nImport all the objects from the negotiator module\r\n\r\n >>> from negotiator import ContentNegotiator, AcceptParameters, ContentType, Language\r\n\r\nSpecify the default parameters. These are the parameters which will be used in place of any HTTP Accept headers which are not present in the negotiation request. For example, if the Accept-Language header is not passed to the negotiator it will assume that the client request is for \"en\"\r\n\r\n >>> default_params = AcceptParameters(ContentType(\"text/html\"), Language(\"en\"))\r\n\r\nSpecify the list of acceptable formats that the server supports\r\n\r\n >>> acceptable = [AcceptParameters(ContentType(\"text/html\"), Language(\"en\"))]\r\n >>> acceptable.append(AcceptParameters(ContentType(\"text/json\"), Language(\"en\")))\r\n\r\nCreate an instance of the negotiator, ready to accept negotiation requests\r\n\r\n >>> cn = ContentNegotiator(default_params, acceptable)\r\n\r\nA simple negotiate on the HTTP Accept header \"text/json;q=1.0, text/html;q=0.9\", asking for json, and if not json then html\r\n\r\n >>> acceptable = cn.negotiate(accept=\"text/json;q=1.0, text/html;q=0.9\")\r\n\r\nThe negotiator indicates that the best match the server can give to the client's request is text/json in english\r\n\r\n >>> acceptable\r\n AcceptParameters:: Content Type: text/json;Language: en;\r\n\r\n\r\nAdvanced Usage\r\n--------------\r\n\r\nImport all the objects from the negotiator module\r\n\r\n >>> from negotiator import ContentNegotiator, AcceptParameters, ContentType, Language\r\n\r\nSpecify the default parameters. These are the parameters which will be used in place of any HTTP Accept headers which are not present in the negotiation request. For example, if the Accept-Language header is not passed to the negotiator it will assume that the client request is for \"en\"\r\n\r\n >>> default_params = AcceptParameters(ContentType(\"text/html\"), Language(\"en\"))\r\n\r\nSpecify the list of acceptable formats that the server supports. For this advanced example we specify html, json and pdf in a variety of languages\r\n\r\n >>> acceptable = [AcceptParameters(ContentType(\"text/html\"), Language(\"en\"))]\r\n >>> acceptable.append(AcceptParameters(ContentType(\"text/html\"), Language(\"fr\")))\r\n >>> acceptable.append(AcceptParameters(ContentType(\"text/html\"), Language(\"de\")))\r\n >>> acceptable.append(AcceptParameters(ContentType(\"text/json\"), Language(\"en\")))\r\n >>> acceptable.append(AcceptParameters(ContentType(\"text/json\"), Language(\"cz\")))\r\n >>> acceptable.append(AcceptParameters(ContentType(\"application/pdf\"), Language(\"de\")))\r\n\r\nspecify the weighting that the negotiator should apply to the different Accept headers. A higher weighting towards content type will prefer content type variations over language variations (e.g. if there are two formats which are equally acceptable to the client, in different languages, a content_type weight higher than a language weight will return the parameters according to the server's preferred content type.\r\n\r\n >>> weights = {\"content_type\" : 1.0, \"language\" : 0.5}\r\n\r\nCreate an instance of the negotiator, ready to accept negotiation requests\r\n\r\n >>> cn = ContentNegotiator(default_params, acceptable, weights)\r\n\r\nset up some more complex accept headers (you can try modifying the order of the elements without q values, and the q values themselves, to see different results).\r\n\r\n >>> accept = \"text/html, text/json;q=1.0, application/pdf;q=0.5\"\r\n >>> accept_language = \"en;q=0.5, de, cz, fr\"\r\n\r\nnegotiate over both headers, looking for an optimal solution to the client request\r\n\r\n >>> acceptable = cn.negotiate(accept, accept_language)\r\n\r\nThe negotiator indicates the best fit to the client request is text/html in German\r\n\r\n >>> acceptable\r\n AcceptParameters:: Content Type: text/html;Language: de;\r\n\r\n\r\nPreference Ordering Rules\r\n-------------------------\r\n\r\nThe Negotiator organises the preferences in each accept header into a sequence,\r\nfrom highest q value to lowest, grouping together equal q values.\r\n\r\nFor example, the HTTP Accept header:\r\n\r\n \"text/html, text/json;q=1.0, application/pdf;q=0.5\"\r\n \r\nWould result in the following preference sequence (as a python dictionary):\r\n\r\n {\r\n 1.0 : [\"text/json\", \"text/html\"],\r\n 0.5 : [\"application/pdf\"]\r\n }\r\n\r\nWhile the HTTP Accept-Language header:\r\n\r\n \"en;q=0.5, de, cz, fr\"\r\n \r\nWould result in the following preference sequence (as a python dictionary):\r\n\r\n {\r\n 1.0 : [\"de\"],\r\n 0.8 : [\"cz\"],\r\n 0.6 : [\"fr\"],\r\n 0.5 : [\"en\"]\r\n }\r\n\r\n(In reality, the q values for de, cz and fr would be evenly spaced between 1.0 and 0.5, using floating point numbers as the keys)\r\n\r\n\r\nCombined Preference Ordering Rules\r\n----------------------------------\r\n\r\nThe negotiator will compute all the possible allowed combinations and their weighted overall q values.\r\n\r\nGiven that the server supports the following combinations (from the code example above):\r\n\r\n text/html, en\r\n text/html, fr\r\n text/html, de\r\n text/json, en\r\n text/json, cz\r\n application/pdf, de\r\n\r\nAnd given the weights:\r\n\r\n w = {\"content_type\" : 1.0, \"language\" : 0.5}\r\n\r\nWe can calculate the combined q value of each allowed (by both server and client) option, using the equation:\r\n\r\n overall_q = w[\"content_type\"] * content_type_q + w[\"language\"] * language_q\r\n \r\nSo, for the above options and q values from the previous section, we can generate the preference list (as a python dictionary):\r\n\r\n {\r\n 1.5 : [\"text/html, de\"],\r\n 1.4 : [\"text/json, cz\"],\r\n 1.3 : [\"text/html, fr\"],\r\n 1.25 : [\"text/html, en\", \"text/json, en\"]\r\n 1.0 : [\"application/pdf, de\"]\r\n }\r\n\r\nIt is clear, then, why the negotiator in the Advanced Usage section selected \"text/html, de\" as its preferred format.\r\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "UNKNOWN", "keywords": null, "license": "CC0", "maintainer": null, "maintainer_email": null, "name": "negotiator", "package_url": "https://pypi.org/project/negotiator/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/negotiator/", "project_urls": { "Download": "UNKNOWN", "Homepage": "UNKNOWN" }, "release_url": "https://pypi.org/project/negotiator/1.0.0/", "requires_dist": null, "requires_python": null, "summary": "Proper Content Negotiation for Python\n \n The Negotiator is a library for decision making over Content Negotiation requests.\n It takes the standard HTTP Accept headers (Accept, Accept-Language, Accept-Charset,\n Accept-Encoding) and rationalises them against the parameters acceptable by the\n server; it then makes a recommendation as to the appropriate response format.\n \n This version of the Negotiator also supports the SWORDv2 extensions to HTTP Accept\n in the form of Accept-Packaging.", "version": "1.0.0" }, "last_serial": 795226, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "b31dd661bd6f391b969a27747c083f4d", "sha256": "139eeb1f56af27cfc8a2d6f361c84fa517d1ce3f440c477b741689d771590be3" }, "downloads": -1, "filename": "negotiator-1.0.0.tar.gz", "has_sig": false, "md5_digest": "b31dd661bd6f391b969a27747c083f4d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11984, "upload_time": "2012-03-14T18:59:17", "url": "https://files.pythonhosted.org/packages/d5/9c/520938226e807536f742829f0de288260496ebd5506495363f63615a404c/negotiator-1.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b31dd661bd6f391b969a27747c083f4d", "sha256": "139eeb1f56af27cfc8a2d6f361c84fa517d1ce3f440c477b741689d771590be3" }, "downloads": -1, "filename": "negotiator-1.0.0.tar.gz", "has_sig": false, "md5_digest": "b31dd661bd6f391b969a27747c083f4d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11984, "upload_time": "2012-03-14T18:59:17", "url": "https://files.pythonhosted.org/packages/d5/9c/520938226e807536f742829f0de288260496ebd5506495363f63615a404c/negotiator-1.0.0.tar.gz" } ] }