Metadata-Version: 2.1
Name: starlette-context
Version: 0.2.3
Summary: Access context in Starlette
Home-page: https://github.com/tomwojcik/starlette-context
Author: Tomasz Wojcik
License: MIT
Download-URL: https://github.com/tomwojcik/starlette-context/archive/0.2.3.tar.gz
Keywords: starlette,fastapi
Platform: any
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Build Tools
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: starlette

[![Build Status](https://travis-ci.org/tomwojcik/starlette-context.svg?branch=master)](https://travis-ci.org/tomwojcik/starlette-context)
[![](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)
[![PyPI license](https://img.shields.io/pypi/l/ansicolortags.svg)](https://pypi.python.org/pypi/ansicolortags/)
[![codecov](https://codecov.io/gh/tomwojcik/starlette-context/branch/master/graph/badge.svg)](https://codecov.io/gh/tomwojcik/starlette-context)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![](https://readthedocs.org/projects/pip/badge/?version=latest&style=plastic)](https://starlette-context.readthedocs.io/)

# 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/stable/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. Test/dev env is dockerized if you want to try them yourself.

### Example

```python
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse

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

middleware = [
    Middleware(
        ContextMiddleware,
        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.


### Contribution
All tickets or PRs are more than welcome.


