A library for deciding content-type based on media ranges in HTTP Accept headers.
Project description
content-negotiation
A small Python library supporting content-negotiation.
It is used to decide content type based on a list of media ranges, as well as deciding content-language based on the accpe-language header.
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.1.13
% pipx install nox==2022.1.7
% pipx inject nox nox-poetry==0.9.0
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
Built Distribution
File details
Details for the file content-negotiation-1.1.0.tar.gz
.
File metadata
- Download URL: content-negotiation-1.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0 CPython/3.8.10 Linux/5.15.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 732acd83102dc9b73b46f075b7fe1bce0236a0b61db0e622f1ecaa108164e45e |
|
MD5 | 0104ad68e1335bd08637d9dd2c4db929 |
|
BLAKE2b-256 | de2b0e813e0c17bb515f55298e1cd07b8b7deb31186d8ead554bcfd69c54ca67 |
File details
Details for the file content_negotiation-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: content_negotiation-1.1.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0 CPython/3.8.10 Linux/5.15.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4201133d7204ffd9ceea4d9f286dbb531d7ec85389889127e40adbba674f1fdf |
|
MD5 | e1593c217f1fe0caa8b745c589a1871d |
|
BLAKE2b-256 | 9c92648c3c93106de15d0610f1de1ac2757c2c44a908055e350c91407f1fd5bc |