========================================================
accept-types - Use the correct accept type for a request
========================================================
``accept-types`` helps your application respond to a HTTP request in a way that a client prefers.
The ``Accept`` header of an HTTP request informs the server which MIME types the client is expecting
back from this request, with weighting to indicate the most prefered. If your server can respond in
multiple formats (e.g.: JSON, XML, HTML), the client can easily tell your server which is the
prefered format without resorting to hacks like '&format=json' on the end of query strings.
Usage
=====
``get_best_match``
------------------
When provided with an ``Accept`` header and a list of types your server can respond with, this function
returns the clients most prefered type. This function will only return one of the acceptable types you
passed in, or ``None`` if no suitable type was found:
.. code:: python
from accept_type import get_best_match
def get_the_info(request):
info = gather_info()
return_type = get_best_match(request.META.get('HTTP_ACCEPT'), ['text/html', 'application/xml', 'text/json'])
if return_type == 'application/xml':
return render_xml(info)
elif return_type == 'text/json':
return render_json(info)
elif return_type == 'text/html':
return render_html(info)
elif return_type == None:
return HttpResponse406()
``parse_header``
----------------
When provided with an ``Accept`` header, this will parse it and return a sorted list of the clients
accepted mime types. These will be instances of the ``AcceptableType`` class.
.. code:: python
>>> from accept_type import parse_header
>>> parse_header('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
['text/html, weight 1', 'application/xhtml+xml, weight 1', 'application/xml, weight 0.9', '*/*, weight 0.8']
``AcceptableType``
------------------
``AcceptableType`` instances represent one of the types that a client is willing to accept. This
type could include wildcards, to match more than one MIME type.
.. code:: python
>>> from accept_type import AcceptableType
>>> type = AcceptableType('image/*;q=0.9')
AcceptableType
>>> type.mime_type
'image/*'
>>> type.weight
0.9
>>> type.matches('image/png')
True
>>> type.matches('text/html')
False
Raw data
{
"_id": null,
"home_page": "https://bitbucket.org/tim_heap/python-accept-types",
"name": "accept-types",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Tim Heap",
"author_email": "tim@timheap.me",
"download_url": "https://files.pythonhosted.org/packages/a3/84/6f51d94019411892c9f7fa9d461d4cef06beb35d54cd9944ea19728c4d45/accept-types-0.4.1.tar.gz",
"platform": "",
"description": "========================================================\naccept-types - Use the correct accept type for a request\n========================================================\n\n``accept-types`` helps your application respond to a HTTP request in a way that a client prefers.\nThe ``Accept`` header of an HTTP request informs the server which MIME types the client is expecting\nback from this request, with weighting to indicate the most prefered. If your server can respond in\nmultiple formats (e.g.: JSON, XML, HTML), the client can easily tell your server which is the\nprefered format without resorting to hacks like '&format=json' on the end of query strings.\n\n\nUsage\n=====\n\n``get_best_match``\n------------------\n\nWhen provided with an ``Accept`` header and a list of types your server can respond with, this function\nreturns the clients most prefered type. This function will only return one of the acceptable types you\npassed in, or ``None`` if no suitable type was found:\n\n.. code:: python\n\n\tfrom accept_type import get_best_match\n\n\tdef get_the_info(request):\n\t\tinfo = gather_info()\n\n\t\treturn_type = get_best_match(request.META.get('HTTP_ACCEPT'), ['text/html', 'application/xml', 'text/json'])\n\n\t\tif return_type == 'application/xml':\n\t\t\treturn render_xml(info)\n\n\t\telif return_type == 'text/json':\n\t\t\treturn render_json(info)\n\n\t\telif return_type == 'text/html':\n\t\t\treturn render_html(info)\n\n\t\telif return_type == None:\n\t\t\treturn HttpResponse406()\n\n``parse_header``\n----------------\n\nWhen provided with an ``Accept`` header, this will parse it and return a sorted list of the clients\naccepted mime types. These will be instances of the ``AcceptableType`` class.\n\n.. code:: python\n\n\t>>> from accept_type import parse_header\n\t>>> parse_header('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')\n\t['text/html, weight 1', 'application/xhtml+xml, weight 1', 'application/xml, weight 0.9', '*/*, weight 0.8']\n\n``AcceptableType``\n------------------\n\n``AcceptableType`` instances represent one of the types that a client is willing to accept. This\ntype could include wildcards, to match more than one MIME type.\n\n.. code:: python\n\n\t>>> from accept_type import AcceptableType\n\t>>> type = AcceptableType('image/*;q=0.9')\n\tAcceptableType\n\t>>> type.mime_type\n\t'image/*'\n\t>>> type.weight\n\t0.9\n\t>>> type.matches('image/png')\n\tTrue\n\t>>> type.matches('text/html')\n\tFalse\n\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Determine the best content to send in an HTTP response",
"version": "0.4.1",
"project_urls": {
"Homepage": "https://bitbucket.org/tim_heap/python-accept-types"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0824559c0f1959134a0e7615a0ece082734209b1942365ec479cbe3bcca9098b",
"md5": "92230a07a9d82dace81a40f81720d43b",
"sha256": "c87feccdffb66b02f9343ff387d7fd5c451ccb2e1221fbd37ea0cedef5cf290f"
},
"downloads": -1,
"filename": "accept_types-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "92230a07a9d82dace81a40f81720d43b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4995,
"upload_time": "2019-08-30T07:52:02",
"upload_time_iso_8601": "2019-08-30T07:52:02.619571Z",
"url": "https://files.pythonhosted.org/packages/08/24/559c0f1959134a0e7615a0ece082734209b1942365ec479cbe3bcca9098b/accept_types-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a3846f51d94019411892c9f7fa9d461d4cef06beb35d54cd9944ea19728c4d45",
"md5": "824024083f014833e2094c655dec1a53",
"sha256": "fb27099716d8f0360408c8ca86d69dbfed44455834b70d1506250abe521b535a"
},
"downloads": -1,
"filename": "accept-types-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "824024083f014833e2094c655dec1a53",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3703,
"upload_time": "2019-08-30T07:52:04",
"upload_time_iso_8601": "2019-08-30T07:52:04.405576Z",
"url": "https://files.pythonhosted.org/packages/a3/84/6f51d94019411892c9f7fa9d461d4cef06beb35d54cd9944ea19728c4d45/accept-types-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-08-30 07:52:04",
"github": false,
"gitlab": false,
"bitbucket": true,
"codeberg": false,
"bitbucket_user": "tim_heap",
"bitbucket_project": "python-accept-types",
"lcname": "accept-types"
}