[![Test Suite](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml/badge.svg)](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml)
[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![PyPI version](https://badge.fury.io/py/starlette-context.svg)](https://badge.fury.io/py/starlette-context)
[![codecov](https://codecov.io/gh/tomwojcik/starlette-context/branch/master/graph/badge.svg)](https://codecov.io/gh/tomwojcik/starlette-context)
[![Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://starlette-context.readthedocs.io/)
![Downloads](https://img.shields.io/pypi/dm/starlette-context)
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/tomwojcik/starlette-context.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/tomwojcik/starlette-context/context: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:
* **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
```python
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
```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](https://www.adverity.com/) for sponsoring the development of this OSS library in 2022.
### Contribution
See the guide on [read the docs](https://starlette-context.readthedocs.io/en/latest/contributing.html#contributing).
Raw data
{
"_id": null,
"home_page": "https://github.com/tomwojcik/starlette-context",
"name": "starlette-context",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "starlette,fastapi",
"author": "Tomasz Wojcik",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/e9/8e/608c0333e9c1c18a25ad81292bbc83d351317fe2059c52f3b135f7777700/starlette_context-0.3.5.tar.gz",
"platform": "any",
"description": "[![Test Suite](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml/badge.svg)](https://github.com/tomwojcik/starlette-context/actions/workflows/test-suite.yml)\n[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)\n[![PyPI version](https://badge.fury.io/py/starlette-context.svg)](https://badge.fury.io/py/starlette-context)\n[![codecov](https://codecov.io/gh/tomwojcik/starlette-context/branch/master/graph/badge.svg)](https://codecov.io/gh/tomwojcik/starlette-context)\n[![Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://starlette-context.readthedocs.io/)\n![Downloads](https://img.shields.io/pypi/dm/starlette-context)\n[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/tomwojcik/starlette-context.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/tomwojcik/starlette-context/context:python)\n\n# starlette context\nMiddleware 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.\n\nResources:\n\n* **Source**: https://github.com/tomwojcik/starlette-context\n* **Documentation**: https://starlette-context.readthedocs.io/\n* **Changelog**: https://starlette-context.readthedocs.io/en/latest/changelog.html\n\n### Installation\n\n`$ pip install starlette-context`\n\n\n### Requirements\nPython 3.7+\n\n### Dependencies\n\n- `starlette`\n\nAll other dependencies from `requirements-dev.txt` are only needed to run tests or examples.\n\n### Example\n\n```python\nimport uvicorn\n\nfrom starlette.applications import Starlette\nfrom starlette.middleware import Middleware\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\n\nfrom starlette_context import context, plugins\nfrom starlette_context.middleware import RawContextMiddleware\n\nmiddleware = [\n Middleware(\n RawContextMiddleware,\n plugins=(\n plugins.RequestIdPlugin(),\n plugins.CorrelationIdPlugin()\n )\n )\n]\n\napp = Starlette(middleware=middleware)\n\n\n@app.route(\"/\")\nasync def index(request: Request):\n return JSONResponse(context.data)\n\n\nuvicorn.run(app, host=\"0.0.0.0\")\n\n```\nIn this example the response contains a json with\n```json\n{\n \"X-Correlation-ID\":\"5ca2f0b43115461bad07ccae5976a990\",\n \"X-Request-ID\":\"21f8d52208ec44948d152dc49a713fdd\"\n}\n```\n\nContext can be updated and accessed at anytime if it's created in the middleware.\n\n### Sponsorship\n\nA huge thank you to [Adverity](https://www.adverity.com/) for sponsoring the development of this OSS library in 2022.\n\n### Contribution\n\nSee the guide on [read the docs](https://starlette-context.readthedocs.io/en/latest/contributing.html#contributing).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Access context in Starlette",
"version": "0.3.5",
"split_keywords": [
"starlette",
"fastapi"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "a1502ff6e66d0b4f082a5e0922e97f50",
"sha256": "d9eeccaafd4fad13abe662295b802c8a58a7e11a0d33f933f001212472619086"
},
"downloads": -1,
"filename": "starlette_context-0.3.5-py37-none-any.whl",
"has_sig": false,
"md5_digest": "a1502ff6e66d0b4f082a5e0922e97f50",
"packagetype": "bdist_wheel",
"python_version": "py37",
"requires_python": ">=3.7",
"size": 12654,
"upload_time": "2022-11-26T15:33:32",
"upload_time_iso_8601": "2022-11-26T15:33:32.922773Z",
"url": "https://files.pythonhosted.org/packages/6e/0d/613f99d2b8e5c40fdbcfd9c04241ad7fdb6b3fc4c706dbdcd9f0cadbdec9/starlette_context-0.3.5-py37-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "ac30bb29e48831d178417c50f63414f8",
"sha256": "e6b9f905823860e9e36c013dbfcf770562f3b88bec21cb861fef2e0bd0615697"
},
"downloads": -1,
"filename": "starlette_context-0.3.5.tar.gz",
"has_sig": false,
"md5_digest": "ac30bb29e48831d178417c50f63414f8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 9235,
"upload_time": "2022-11-26T15:33:34",
"upload_time_iso_8601": "2022-11-26T15:33:34.354649Z",
"url": "https://files.pythonhosted.org/packages/e9/8e/608c0333e9c1c18a25ad81292bbc83d351317fe2059c52f3b135f7777700/starlette_context-0.3.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-11-26 15:33:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "tomwojcik",
"github_project": "starlette-context",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "starlette",
"specs": [
[
"==",
"0.14.2"
]
]
}
],
"lcname": "starlette-context"
}