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

Enum for media range specificity.

exception content_negotiation.content_negotiation.NoAgreeableContentTypeError

Bases: Exception

Exception 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: object

Class for handling weighted media ranges.

media_range()

Return the media range.

Return type

str

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
  • accept_headers (List[str]) – the accept headers.

  • supported_content_types (List[str]) – List of supported content types.

Return type

str

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

Enum for media range specificity.

exception content_negotiation.language_negotiation.NoAgreeableLanguageError

Bases: Exception

Exception 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: object

Class 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
  • accept_language_headers (List[str]) – the accept-langugage headers.

  • supported_languages (List[str]) – List of supported languages.

Return type

str

Returns

The content language of the response.

Raises

NoAgreeableLanguageError – If no agreeable language is found.