Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomwojcik/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.
https://github.com/tomwojcik/starlette-context
fastapi middleware python python3 starlette starlette-context
Last synced: 2 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/tomwojcik/starlette-context
- Owner: tomwojcik
- License: mit
- Created: 2019-11-01T16:32:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-19T05:00:26.000Z (about 1 month ago)
- Last Synced: 2024-12-12T07:12:17.897Z (9 days ago)
- Topics: fastapi, middleware, python, python3, starlette, starlette-context
- Language: Python
- Homepage: https://starlette-context.readthedocs.io/en/latest/
- Size: 413 KB
- Stars: 453
- Watchers: 7
- Forks: 24
- Open Issues: 14
-
Metadata Files:
- Readme: README.rst
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-fastapi - Starlette Context - Allows you to store and access the request data anywhere in your project, useful for logging. (Third-Party Extensions / Utils)
- awesome-fastapi - Starlette Context - Allows you to store and access the request data anywhere in your project, useful for logging. (Third-Party Extensions / Utils)
- best-of-web-python - GitHub - 28% open · ⏱️ 07.11.2023): (Monitoring)
README
|Test Suite| |Python| |PyPI version| |codecov| |Docs| |Downloads|
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.htmlInstallation
~~~~~~~~~~~~Python ~3.7.0
.. code-block:: console
$ pip install starlette-context==0.3.5
Python ^3.8
.. code-block:: console
$ pip install starlette-context>=0.3.6
Dependencies
~~~~~~~~~~~~- ``starlette``
Example
~~~~~~~.. code:: python
import uvicorn
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponsefrom starlette_context import context, plugins
from starlette_context.middleware import RawContextMiddlewaremiddleware = [
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
.. code:: json
{
"X-Correlation-ID":"5ca2f0b43115461bad07ccae5976a990",
"X-Request-ID":"21f8d52208ec44948d152dc49a713fdd"
}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 in 2022.Contribution
~~~~~~~~~~~~See the guide on `read the
docs `__... |Test Suite| image:: https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml/badge.svg
:target: https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml
.. |Python| image:: https://img.shields.io/badge/python-3.8+-blue.svg
:target: https://www.python.org/downloads/release/python-370/
.. |PyPI version| image:: https://badge.fury.io/py/starlette-context.svg
:target: https://badge.fury.io/py/starlette-context
.. |codecov| image:: https://codecov.io/gh/tomwojcik/starlette-context/branch/master/graph/badge.svg
:target: https://codecov.io/gh/tomwojcik/starlette-context
.. |Docs| image:: https://readthedocs.org/projects/pip/badge/?version=latest
:target: https://starlette-context.readthedocs.io/
.. |Downloads| image:: https://img.shields.io/pypi/dm/starlette-context