intessa.conneg.accept — Utilities for manipulating HTTP Accept headers

Utilities for creating and manipulating HTTP Accept headers.

intessa.conneg.accept.make_accept_header(spec, codec_register={'xml': 'application/xml', 'json-js': 'text/javascript', 'form': 'application/x-www-form-urlencoded', 'text/javascript': <class 'intessa.conneg.default.json.JSONCodec'>, 'application/x-www-form-urlencoded': <class 'intessa.conneg.default.forms.SimpleFormCodec'>, 'text': 'text/plain', 'json': 'application/json', 'multipart/form-data': <class 'intessa.conneg.default.multipart.MultipartFormCodec'>, 'application/xml': <class 'intessa.conneg.default.xml.XMLCodec'>, 'application/json': <class 'intessa.conneg.default.json.JSONCodec'>, 'text/plain': <class 'intessa.conneg.default.text.TextCodec'>, 'multipart': 'multipart/form-data'})[source]

Build an accept header.

Works on single strings:

>>> make_accept_header('text/html')
'text/html'

Works on iterables of strings:

>>> make_accept_header(('text/html', 'application/json'))
'text/html,application/json'

Works on iterables of type/quality pairs:

>>> make_accept_header((('text/html', 0.7), ('application/json', 0.9)))
'text/html;q=0.7,application/json;q=0.9'

And mixed input:

>>> make_accept_header((('text/html', 0.7), 'application/json'))
'text/html;q=0.7,application/json'

Also works on aliases defined on the codec register. Set up an alias:

>>> from intessa.conneg.default import DEFAULT_REGISTER
>>> reg = DEFAULT_REGISTER.copy()
>>> reg.alias('app_json', 'application/json')

And then use it on its own, or in lists:

>>> make_accept_header('app_json', reg)
'application/json'
>>> make_accept_header(('app_json', 'text/html'), reg)
'application/json,text/html'
>>> make_accept_header((('app_json', 0.2), 'text/html'), reg)
'application/json;q=0.2,text/html'

Previous topic

intessa.conneg.content_type — Represent internet media types

Next topic

intessa.conneg.streaming — Utilities for streaming HTTP requests

This Page