Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgi_context-0.2.1.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distributions

asgi_context-0.2.1-cp312-cp312-win_amd64.whl (78.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

asgi_context-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (190.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ x86-64

asgi_context-0.2.1-cp312-cp312-macosx_10_9_universal2.whl (196.3 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

asgi_context-0.2.1-cp311-cp311-win_amd64.whl (76.5 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

asgi_context-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (183.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ x86-64

asgi_context-0.2.1-cp311-cp311-macosx_10_9_universal2.whl (191.9 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

asgi_context-0.2.1-cp310-cp310-win_amd64.whl (76.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

asgi_context-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (184.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.28+ x86-64

asgi_context-0.2.1-cp310-cp310-macosx_11_0_x86_64.whl (100.4 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

Supported by

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