content-negotiation
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().
Metadata
Release files for content-negotiation 2.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| content_negotiation-2.1.4.tar.gz | 9.5 kB | Without provenance | Details for content_negotiation-2.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| content_negotiation-2.1.4-py3-none-any.whl | Python 3 | none | any | Details for content_negotiation-2.1.4-py3-none-any.whl |
Total release size: 20.9 kB
Release files / content_negotiation-2.1.4.tar.gz
| Download URL | content_negotiation-2.1.4.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fbfbbdb242b33273401ec5f771f104d4e07f8b36d1435300b649624bab2160f8
|
|
BLAKE2b-256 checksum How to use checksums |
48d313b4be3bd8947fc6beb8ad21d2c86b264f5617f4127935fa30c849502d04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1020-azure
|
Release files / content_negotiation-2.1.4-py3-none-any.whl
| Download URL | content_negotiation-2.1.4-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
419a0c1731c00b87ee8127c5907bb1b10c5abd0dd33816cfe21ee4922be6c21e
|
|
BLAKE2b-256 checksum How to use checksums |
caebda7a8ad2f0169124582c2153da2acc210b1d35cff79dd7b756fadd5e2a1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1020-azure
|
Security for content-negotiation 2.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.