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,
]
)
Metadata
Release files for asgi_context 0.2.1
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 |
|---|---|---|---|---|
| asgi_context-0.2.1.tar.gz | 5.4 kB | Without provenance | Details for asgi_context-0.2.1.tar.gz |
Built distributions (wheels)
Total release size: 1.3 MB
Release files / asgi_context-0.2.1.tar.gz
| Download URL | asgi_context-0.2.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0eadd9856306087393ce3bd36b8613f8e2f869bdeca02009650e3abcdc844cef
|
|
BLAKE2b-256 checksum How to use checksums |
dabf7e7b12f8ff0c9ada9d15a536a85b4fc750f3db48bc1f81fedcc19329fcf8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp312-cp312-win_amd64.whl
| Download URL | asgi_context-0.2.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 78.6 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
a342c9400efaeae1f9324511d5e985efb0cd6316217a5ecc77de15fd84733b23
|
|
BLAKE2b-256 checksum How to use checksums |
44ed71998bef8404ba925a9a9933a36d63947ae00ecd59b98a4b3f9144b1ccbf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
| Download URL | asgi_context-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 190.6 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
7a116cd3d8ed0bde0bad97a42dcdc7bec87a95a75188ba967b7d7bee12a92d5d
|
|
BLAKE2b-256 checksum How to use checksums |
83e1237aaeb951aacdbaf21d23ca3692f432bdb0244d7c30c5be75a457c5f06f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp312-cp312-macosx_10_9_universal2.whl
| Download URL | asgi_context-0.2.1-cp312-cp312-macosx_10_9_universal2.whl |
|---|---|
| Size | 196.3 kB |
| Tags | CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64) |
|
SHA-256 checksum How to use checksums |
c36ceebbb5599e3cd1e4fbf3ed02197d5c5536091ef0be59c4314421b6d18b53
|
|
BLAKE2b-256 checksum How to use checksums |
6cf725ac32ac3a80f5531e13597f300f260cc414c402c545dd7769940d999944
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp311-cp311-win_amd64.whl
| Download URL | asgi_context-0.2.1-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 76.5 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
64aee5a85906612f44346c26cb267da0c6fcb996b8fc3fdcd2967ea27226d307
|
|
BLAKE2b-256 checksum How to use checksums |
9ec62dfcd1af8090da3bd0161ba6ceb463b04924e3aa3501fe177a0827e6be0f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
| Download URL | asgi_context-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 183.9 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
73255f0464447cc463bdbe3a47786615d0d171afaef06dba16b7911315561b76
|
|
BLAKE2b-256 checksum How to use checksums |
e36c9d7b243ef7755cf85f8350df6be1580b2df0d18228c09b5bd0f6254595c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp311-cp311-macosx_10_9_universal2.whl
| Download URL | asgi_context-0.2.1-cp311-cp311-macosx_10_9_universal2.whl |
|---|---|
| Size | 191.9 kB |
| Tags | CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64) |
|
SHA-256 checksum How to use checksums |
fa687a14773e88a396fcb3d817734ad8767d4d9557413ba3bca6efa60ee00c07
|
|
BLAKE2b-256 checksum How to use checksums |
e9faa1fe763674f3c19c4b368765f739cec525e03a9f5c7eb5b80d5e561c4a24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp310-cp310-win_amd64.whl
| Download URL | asgi_context-0.2.1-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 76.9 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
9536ee773b2faf2ea0fced3709bf3b7589956d89ef2202ee8dd387221c39583b
|
|
BLAKE2b-256 checksum How to use checksums |
501022ed36869a942482b6c78e1b297265412161ff01166c12dd1e893ba269b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
| Download URL | asgi_context-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 184.7 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
b7325768632503f2e90f0f79417bc505730d7fc0899d7e2366d835ad23642942
|
|
BLAKE2b-256 checksum How to use checksums |
1e0bca4f13a42e00a2ed72dd24a951a6d1c48d896267577825c33f636fe15d71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Release files / asgi_context-0.2.1-cp310-cp310-macosx_11_0_x86_64.whl
| Download URL | asgi_context-0.2.1-cp310-cp310-macosx_11_0_x86_64.whl |
|---|---|
| Size | 100.4 kB |
| Tags | CPython 3.10 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
ca4842e2cc2fd6ebf086cfebe00927fdbcbd905efc4d952b0720d26649019f74
|
|
BLAKE2b-256 checksum How to use checksums |
5399b78cd2c422fafaf6f069eab3d734d415fede03ba0465bdbaea885ae1fb6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.0 CPython/3.10.12
|
Security for asgi_context 0.2.1
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.