Skip to main content

Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

Project description

starlette-context

Test Suite Python PyPI version codecov Docs Downloads

Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

Resources

Installation

# Python 3.9+
pip install starlette-context

Dependencies

  • starlette>=0.27.0

Example

import uvicorn

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route

from starlette_context import context, plugins
from starlette_context.middleware import ContextMiddleware

async def index(request: Request):
    # Access and store data in context
    context["custom_value"] = "example"
    return JSONResponse(context.data)

# Define routes
routes = [
    Route("/", endpoint=index)
]

# Configure middleware
middleware = [
    Middleware(
        ContextMiddleware,
        plugins=(
            plugins.RequestIdPlugin(),
            plugins.CorrelationIdPlugin()
        )
    )
]

# Create application with routes and middleware
app = Starlette(
    routes=routes,
    middleware=middleware
)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0")

In this example the response contains a JSON with:

{
  "X-Correlation-ID": "5ca2f0b43115461bad07ccae5976a990",
  "X-Request-ID": "21f8d52208ec44948d152dc49a713fdd",
  "custom_value": "example"
}

Context can be updated and accessed at anytime if it's created in the middleware.

Sponsorship

A huge thank you to Adverity for sponsoring the development of this OSS library.

Contribution

See the contributing guide.

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

starlette_context-0.4.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

starlette_context-0.4.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file starlette_context-0.4.0.tar.gz.

File metadata

  • Download URL: starlette_context-0.4.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.11 Linux/6.8.0-1021-azure

File hashes

Hashes for starlette_context-0.4.0.tar.gz
Algorithm Hash digest
SHA256 3242417c9354c067a4ac5009aff762dc0b322074216f664825d5d127108553be
MD5 3d603b90b8be2491861bc178e0136232
BLAKE2b-256 50a242e29208fea21d03e6487d3ae4b6f1a5bd21cf762b8b7eef95041e8c08ff

See more details on using hashes here.

File details

Details for the file starlette_context-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: starlette_context-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.11 Linux/6.8.0-1021-azure

File hashes

Hashes for starlette_context-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbcc11006587f901edd3d0a989a69a628fccf9d00c1ca3c28fab23ab88bd0093
MD5 c7399c7b0242983638a96c24a08a1fb2
BLAKE2b-256 98ec90a4479bc3e307f3ff0b56165d70e1b01cf10882872823b27984bda375e2

See more details on using hashes here.

Supported by

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