Access context in Starlette
Project description
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:
- Source: https://github.com/tomwojcik/starlette-context
- Documentation: https://starlette-context.readthedocs.io/
- Changelog: https://starlette-context.readthedocs.io/en/latest/changelog.html
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
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 Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95dbc02498478e706b51896914f467a759a72dc61aa029f38eee653ef6349a47
|
|
| MD5 |
3608e89167c7cf9704935457c11025b6
|
|
| BLAKE2b-256 |
699dc08463e756aefa7bc0f3c9aa2452b45a7bae807f4907093666b87f66dd6c
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4aa4f086f08daff828d4dfe9284123fa309b8abe75737fdbf3f552b68d0b174e
|
|
| MD5 |
3cdd58f979c2601b0264ef362b2707ce
|
|
| BLAKE2b-256 |
a5479e36b1db6616141e8e9ba5cf4668697bf9353547db0d35fef7490ad7c6ff
|