Skip to main content

A library for deciding content-type based on media ranges in HTTP Accept headers.

Project description

content-negotiation

Tests codecov PyPI Read the Docs

A small Python library supporting content-negotiation.

It is used to decide content type based on a list of media ranges in the accept header, as well as deciding content-language based on the accept-language header.

  • Media ranges/language ranges with a q-value of 0.0 will be ignored.
  • Q-values above 1.0 will be treated as 1.0. Q-values below 0.0 will be treated as 0.0.
  • When a media range is not specified, it will be treated as */*.
  • When a language range is not specified, it will be treated as *.
  • When media ranges and language ranges are equal, the first one will be returned.

For more information on the accept header, see RFC 7231, section-5.3.2. For more information on the accept-language header, see RFC 7231, section-5.3.5

Usage

Install

% pip install content-negotiation

Getting started

Content type

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

Content language

from content_negotiation import decide_content_language, NoAgreeableContentLanguageError

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_decide_language(accept_language_headers, supported_languages)
except NoAgreeableLanguageError:
    print("No agreeable language found.")
    # Handle error, by returning e.g. 406 Not Acceptable

Development

Requirements

% pipx install poetry==1.2.0
% pipx install nox==2022.8.7
% pipx inject nox nox-poetry==1.0.1

Install developer tools

% git clone https://github.com/Informasjonsforvaltning/content-negotiation.git
% cd content-negotiation
% pyenv install 3.8.13
% pyenv install 3.9.13
% pyenv install 3.10.6
% pyenv local 3.8.13 3.9.13 3.10.6
% poetry install

Run all sessions

% nox

Run all tests with coverage reporting

% nox -rs tests

Debugging

You can enter into Pdb by passing --pdb to pytest:

% nox -rs tests -- --pdb  --log-cli-level=DEBUG

You can set breakpoints directly in code by using the function breakpoint().

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

content_negotiation-2.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

content_negotiation-2.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file content_negotiation-2.1.0.tar.gz.

File metadata

  • Download URL: content_negotiation-2.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.12.13 Linux/6.17.0-1008-azure

File hashes

Hashes for content_negotiation-2.1.0.tar.gz
Algorithm Hash digest
SHA256 8b8f3ee8b9f8d88abdc08a1746dba36668e1f0463a0c96f67dca5f9911061d84
MD5 68020cc1a6be4d216d331b3c76c8513b
BLAKE2b-256 f883d13f789cbd58300dc84245a1101d5402f5df64001d10ae7ed7f28b3fcec5

See more details on using hashes here.

File details

Details for the file content_negotiation-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: content_negotiation-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.12.13 Linux/6.17.0-1008-azure

File hashes

Hashes for content_negotiation-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fbdd86fcb812d5c189714768707c8de848c1913b3e48a5af22f922c6d7194bc
MD5 432375fc1a1dadd238fd314aff331692
BLAKE2b-256 45edc2c614e8f98a8c72b47d60c8e7e9fb45d0e8ccbd4768a838de19a1323eb7

See more details on using hashes here.

Supported by

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