get_best_mimetype(header_value,
within,
default=None,
check_params=False,
return_full=False)
Iterates through 'header_value' and checks if it finds any match in
'within'.
When */* is part of header_value and no candidate was found this
function returns the first media-type of 'within'
Consider the following examples:
>>> from amplee.http_helper import get_best_mimetype
>>> l = ['application/rdf+xml', 'application/atom+xml']
>>> a = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
>>> get_best_mimetype(a, l)
>>> 'application/rdf+xml'
>>> get_best_mimetype(a, l, 'application/atom+xml')
'application/atom+xml'
>>> a = 'text/xml,application/xml,application/atom+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> l = ['application/rdf+xml', 'entry']
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> a = 'text/xml,application/xml,text/html;q=0.9,text/plain;q=0.8,image/png'
>>> get_best_mimetype(a, l) # returns None
>>> a = 'application/xml;q=0.9,application/rdf+xml;q=0.8,application/atom+xml'
>>> l = ['application/rdf+xml', 'entry']
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> a = 'application/xml;q=0.9,application/rdf+xml;q=0.8,application/atom+xml;q=0.1'
>>> get_best_mimetype(a, l)
'application/rdf+xml'
>>> a = 'application/xml,application/rdf+xml;q=0.8,application/atom+xml'
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> l = ['application/rdf+xml', 'application/atom+xml', 'application/xhtml+xml']
>>> a = 'application/*'
>>> get_best_mimetype(a, l)
>>> 'application/xhtml+xml'
>>> l = [u'application/atom+xml;type=entry', u'application/x-www-form-urlencoded']
>>> a = u'application/atom+xml;type=entry;some=yu'
>>> get_best_mimetype(a, l, check_params=True)
>>> a = u'application/atom+xml;type=entry'
>>> get_best_mimetype(a, l, check_params=True)
u'application/atom+xml'
>>> get_best_mimetype(a, l, check_params=True, return_full=True)
u'application/atom+xml;type=entry'
>>> a = u'application/atom+xml;type=entry;some=yu'
>>> get_best_mimetype(a, l, check_params=['type'])
u'application/atom+xml'
>>> get_best_mimetype(a, l, check_params=['type'], return_full=True)
u'aPPlication/atom+xml; some=yu;type=entry'
>>> get_best_mimetype(a, l)
u'application/atom+xml'
The ``header_value`` is a string respecting the HTTP Accept header
format as defined in section 14.1 of RFC 2616.
The ``within`` argument is a list of acceptable media-type
strings.
The ``default`` value is returned when no match was found.
The ``check_params``, if provided, may be a list of keys (string)
that should be matched between ``header_value`` and headers ``within``
or it can be a boolean. If ``True`` then every parameters will be
tested, if ``False`` (default) the test won't occur. Setting it to
``True`` ensures that if a match is found it will be exactly the one
wanted but this is a more restrictive matching scheme.
If ``return_full`` is ``True`` it returns the media-type along with
its parameters. Otherwise it returns only the ``media-type``.
-
|