Simple request context for ASGI compatible applications
Project description
ASGI Context
Zero dependency middleware for storing HTTP request data in scoped context. By default the library exposes the middleware for creating the context and header extrator builder which can be used e.g. for storing tracing headers.
Installation
The project is available on PyPI:
pip install asgi_context
or you can use pre-built sdist and wheels from Releases
page.
Example usage
FastAPI
from http import HTTPStatus
from fastapi import FastAPI
from asgi_context import (
http_requst_context,
ContextMiddleware,
HeadersExtractorMiddlewareFactory,
ValidationConfig,
)
app = FastAPI()
def example_headers_validator(header_value: str) -> bool:
return "example" in value
# will return 400 when missing specified headers or headers don't pass validation
example_headers_extractor_with_validation = HeadersExtractorMiddlewareFactory.build(
base_name="example_with_validation",
header_names=("X-Example",),
validation_config=ValidationConfig(
err_on_missing=HTTPStatus.BAD_REQUEST,
err_on_invalid=HTTPStatus.BAD_REQUEST,
validators={
"X-Example": example_headers_validator,
},
),
)
example_headers_extractor_without_validation = HeadersExtractorMiddlewareFactory.build(
base_name="example_without_validation",
header_names=("X-Not-Validated-Example",)
)
app.add_middleware(example_headers_extractor_with_validation)
app.add_middleware(example_headers_extractor_without_validation)
app.add_middleware(ContextMiddleware)
@app.get("/")
def index():
return http_request_context["X-Example"]
Starlite
from http import HTTPStatus
from starlite import Starlite, get
from asgi_context import (
http_requst_context,
ContextMiddleware,
HeadersExtractorMiddlewareFactory,
ValidationConfig,
)
def example_headers_validator(header_value: str) -> bool:
return "example" in value
# will return 400 when missing specified headers or headers don't pass validation
example_headers_extractor_with_validation = HeadersExtractorMiddlewareFactory.build(
base_name="example",
header_names=("X-Example",)
validation_config=ValidationConfig(
err_on_missing=HTTPStatus.BAD_REQUEST,
err_on_invalid=HTTPStatus.BAD_REQUEST,
validators={
"X-Example": example_headers_validator,
},
),
)
example_headers_extractor_without_validation = HeadersExtractorMiddlewareFactory.build(
base_name="example_without_validation",
header_names=("X-Not-Validated-Example",)
)
@get("/")
def index() -> str:
return http_request_context["X-Example"]
app = Starlite(
route_handlers=[index],
middleware=[
ContextMiddleware,
example_headers_extractor_with_validation,
example_headers_extractor_without_validation,
]
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file asgi_context-0.2.1.tar.gz
.
File metadata
- Download URL: asgi_context-0.2.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0eadd9856306087393ce3bd36b8613f8e2f869bdeca02009650e3abcdc844cef |
|
MD5 | d9ddc45e72ff5dbe2d3f3bb0063a52e7 |
|
BLAKE2b-256 | dabf7e7b12f8ff0c9ada9d15a536a85b4fc750f3db48bc1f81fedcc19329fcf8 |
File details
Details for the file asgi_context-0.2.1-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 78.6 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a342c9400efaeae1f9324511d5e985efb0cd6316217a5ecc77de15fd84733b23 |
|
MD5 | a8127a1e31d96ab1ee1180763490083f |
|
BLAKE2b-256 | 44ed71998bef8404ba925a9a9933a36d63947ae00ecd59b98a4b3f9144b1ccbf |
File details
Details for the file asgi_context-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 190.6 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a116cd3d8ed0bde0bad97a42dcdc7bec87a95a75188ba967b7d7bee12a92d5d |
|
MD5 | 5d5a2eade08be5a9b25d1a159f3e8154 |
|
BLAKE2b-256 | 83e1237aaeb951aacdbaf21d23ca3692f432bdb0244d7c30c5be75a457c5f06f |
File details
Details for the file asgi_context-0.2.1-cp312-cp312-macosx_10_9_universal2.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp312-cp312-macosx_10_9_universal2.whl
- Upload date:
- Size: 196.3 kB
- Tags: CPython 3.12, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c36ceebbb5599e3cd1e4fbf3ed02197d5c5536091ef0be59c4314421b6d18b53 |
|
MD5 | d64db3d91265941cd4f777b1f0b166a7 |
|
BLAKE2b-256 | 6cf725ac32ac3a80f5531e13597f300f260cc414c402c545dd7769940d999944 |
File details
Details for the file asgi_context-0.2.1-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 76.5 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64aee5a85906612f44346c26cb267da0c6fcb996b8fc3fdcd2967ea27226d307 |
|
MD5 | 7ed1f78008c797aad45e2c373ae18756 |
|
BLAKE2b-256 | 9ec62dfcd1af8090da3bd0161ba6ceb463b04924e3aa3501fe177a0827e6be0f |
File details
Details for the file asgi_context-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 183.9 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73255f0464447cc463bdbe3a47786615d0d171afaef06dba16b7911315561b76 |
|
MD5 | 1a97bc2ddda365d50991f01a1dc2c055 |
|
BLAKE2b-256 | e36c9d7b243ef7755cf85f8350df6be1580b2df0d18228c09b5bd0f6254595c4 |
File details
Details for the file asgi_context-0.2.1-cp311-cp311-macosx_10_9_universal2.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp311-cp311-macosx_10_9_universal2.whl
- Upload date:
- Size: 191.9 kB
- Tags: CPython 3.11, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa687a14773e88a396fcb3d817734ad8767d4d9557413ba3bca6efa60ee00c07 |
|
MD5 | bc2e3aebdf8a7964dc9621f5fb62ffd0 |
|
BLAKE2b-256 | e9faa1fe763674f3c19c4b368765f739cec525e03a9f5c7eb5b80d5e561c4a24 |
File details
Details for the file asgi_context-0.2.1-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 76.9 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9536ee773b2faf2ea0fced3709bf3b7589956d89ef2202ee8dd387221c39583b |
|
MD5 | 7c085e7f1503fa0bc72c36b45c2f8a35 |
|
BLAKE2b-256 | 501022ed36869a942482b6c78e1b297265412161ff01166c12dd1e893ba269b6 |
File details
Details for the file asgi_context-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 184.7 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7325768632503f2e90f0f79417bc505730d7fc0899d7e2366d835ad23642942 |
|
MD5 | 2db235f37f083a45e9d0b802020b1432 |
|
BLAKE2b-256 | 1e0bca4f13a42e00a2ed72dd24a951a6d1c48d896267577825c33f636fe15d71 |
File details
Details for the file asgi_context-0.2.1-cp310-cp310-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: asgi_context-0.2.1-cp310-cp310-macosx_11_0_x86_64.whl
- Upload date:
- Size: 100.4 kB
- Tags: CPython 3.10, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca4842e2cc2fd6ebf086cfebe00927fdbcbd905efc4d952b0720d26649019f74 |
|
MD5 | 34c2ebd08d8095ba4c1e012b41fca77b |
|
BLAKE2b-256 | 5399b78cd2c422fafaf6f069eab3d734d415fede03ba0465bdbaea885ae1fb6f |