Skip to main content

Access context in Starlette

Project description

Test Suite Python PyPI version codecov Docs Downloads Language grade: Python

starlette context

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

$ pip install starlette-context

Requirements

Python 3.7+

Dependencies

  • starlette

All other dependencies from requirements-dev.txt are only needed to run tests or examples.

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_context import context, plugins
from starlette_context.middleware import RawContextMiddleware

middleware = [
    Middleware(
        RawContextMiddleware,
        plugins=(
            plugins.RequestIdPlugin(),
            plugins.CorrelationIdPlugin()
        )
    )
]

app = Starlette(middleware=middleware)


@app.route("/")
async def index(request: Request):
    return JSONResponse(context.data)


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"
}

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

Contribution

See the guide on read the docs.

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.3.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_context-0.3.2-py37-none-any.whl (11.3 kB view details)

Uploaded Python 3.7

File details

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

File metadata

  • Download URL: starlette_context-0.3.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for starlette_context-0.3.2.tar.gz
Algorithm Hash digest
SHA256 95dbc02498478e706b51896914f467a759a72dc61aa029f38eee653ef6349a47
MD5 3608e89167c7cf9704935457c11025b6
BLAKE2b-256 699dc08463e756aefa7bc0f3c9aa2452b45a7bae807f4907093666b87f66dd6c

See more details on using hashes here.

File details

Details for the file starlette_context-0.3.2-py37-none-any.whl.

File metadata

  • Download URL: starlette_context-0.3.2-py37-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for starlette_context-0.3.2-py37-none-any.whl
Algorithm Hash digest
SHA256 4aa4f086f08daff828d4dfe9284123fa309b8abe75737fdbf3f552b68d0b174e
MD5 3cdd58f979c2601b0264ef362b2707ce
BLAKE2b-256 a5479e36b1db6616141e8e9ba5cf4668697bf9353547db0d35fef7490ad7c6ff

See more details on using hashes here.

Supported by

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