Reference¶
content_negotiation.content_negotiation¶
Module for determining content-type based on accept header.
Example
>>> from content_negotiation import decide_content_type, NoAgreeableContentTypeError
>>>
>>> accept_headers = ["application/json", "text/html", "text/plain, text/*;q=0.8"]
>>> supported_content_types = ["text/turtle", "application/json"]
>>>
>>> try:
>>> content_type = decide_content_type(accept_headers, supported_content_types)
>>> except NoAgreeableContentTypeError:
>>> print("No agreeable content type found.")
>>> # Handle error, by returning e.g. 406 Not Acceptable
>>> print(content_type)
'application/json'
- class content_negotiation.content_negotiation.MediaRangeSpecificity(value)¶
Bases:
EnumEnum for media range specificity.
- exception content_negotiation.content_negotiation.NoAgreeableContentTypeError¶
Bases:
ExceptionException for no agreeable content type.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class content_negotiation.content_negotiation.WeightedMediaRange(media_range)¶
Bases:
objectClass for handling weighted media ranges.
- content_negotiation.content_negotiation.decide_content_type(accept_headers, supported_content_types)¶
Decide the content type based on the given accept header and supported content-types.
- Parameters
- Return type
- Returns
The content type of the response.
- Raises
NoAgreeableContentTypeError – If no agreeable content type is found.
content_negotiation.language_negotiation¶
Module for determining content-language based on accept-language header.
Example
>>> from content_negotiation import decide_language, NoAgreeableLanguageError
>>>
>>> accept_language_headers = ["en-GB;q=0.8", "nb-NO;q=0.9"]
>>> supported_languages = ["en-GB", "en", "nb-NO", "nb", "en-US"]
>>>
>>> try:
>>> content_language = decide_language(accept_language_headers, supported_languages)
>>> except NoAgreeableLanguageError:
>>> print("No agreeable language found.")
>>> # Handle error, by returning e.g. 406 Not Acceptable
>>> print(content_language)
'nb-NO'
- class content_negotiation.language_negotiation.LanguageRangeSpecificity(value)¶
Bases:
EnumEnum for media range specificity.
- exception content_negotiation.language_negotiation.NoAgreeableLanguageError¶
Bases:
ExceptionException for no agreeable language.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class content_negotiation.language_negotiation.WeightedLanguage(language)¶
Bases:
objectClass for handling weighted languages.
- content_negotiation.language_negotiation.decide_language(accept_language_headers, supported_languages)¶
Decide the language based on the given accept-language header and supported languages.
- Parameters
- Return type
- Returns
The content language of the response.
- Raises
NoAgreeableLanguageError – If no agreeable language is found.