Skip to main content

Determine the best content to send in an HTTP response

Project description

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:

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.

>>> 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.

>>> 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

accept-types-0.4.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

accept_types-0.4.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file accept-types-0.4.0.tar.gz.

File metadata

  • Download URL: accept-types-0.4.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for accept-types-0.4.0.tar.gz
Algorithm Hash digest
SHA256 abbe13ea19e0b9f48cc276b618afb0d9e43ab73a051938773fdfd78650078873
MD5 e21f90c0d72a2759069707c9fc69375f
BLAKE2b-256 538cf2928624a3dfdf165aa7670f17ea463c9430eaa5ee3895e21be3f250bdbd

See more details on using hashes here.

File details

Details for the file accept_types-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: accept_types-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for accept_types-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 782184693e21bbb26896f6a14df32ce0c685229a1b94d9dfff3f282dda83e0b0
MD5 80b3abfcdd6ca83f1aeb7b3eabc90455
BLAKE2b-256 cf51a501962bd6d229c0e713924aac84336f465f687c83d1fb38e0129f73a3a3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page