Blacksheep Context
Introduction
Middleware for Blacksheep 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.
Requirements
- Python 3.7+
- Blacksheep 1.0.7+
Installation
$ pip install blacksheep-context
Usage
A complete example shown below.
from blacksheep.server import Application
from blacksheep.messages import Request, Response
from blacksheep.server.responses import json
from blacksheep_context import context
from blacksheep_context.middleware import ContextMiddleware
from blacksheep_context.plugins import BasePlugin, HeaderPlugin
class RequestIdPlugin(HeaderPlugin):
header_key = b'X-Request-Id'
# Every plugin must provide this attribute
context_key = 'request-id'
# Fetches only first value of header, can be False to insert all values of header into context
single_value_header = True
# Also allow you to add some data from context into response
async def enrich_response(self, response: Response) -> None:
response.add_header(b'X-Request-Id', context['request_id'].encode('utf-8'))
class MyCustomPlugin(BasePlugin):
context_key = 'user-data'
# You can customize fetching data from request
async def process_request(self, request: Request):
try:
data = await request.json()
return data.get('user-id')
except Exception:
return None
ctx_middleware = ContextMiddleware(plugins=[RequestIdPlugin(), MyCustomPlugin()])
app_ = Application()
app_.middlewares.append(ctx_middleware)
@app_.router.post('/ctx')
def return_context(request):
assert context.exists() is True
return json(context.copy())
Contributing
This project is absolutely open to contributions so if you have a nice idea, create an issue to let the community discuss it.
Metadata
Release files for blacksheep-context 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| blacksheep-context-0.1.0.tar.gz | 4.9 kB | Without provenance | Details for blacksheep-context-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| blacksheep_context-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for blacksheep_context-0.1.0-py3-none-any.whl |
Total release size: 12.1 kB
Release files / blacksheep-context-0.1.0.tar.gz
| Download URL | blacksheep-context-0.1.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
11ace9e01694a3bc0a7431de49baa0f42899e508aa04d337fe02805588deec85
|
|
BLAKE2b-256 checksum How to use checksums |
8f41473de026450665f9501062abfeb96cf77d77c922e20d2747cfb2b38aa5b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.8.10 Linux/5.8.0-1033-azure
|
Release files / blacksheep_context-0.1.0-py3-none-any.whl
| Download URL | blacksheep_context-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2cf8cd3c9208411a769ab611217f197577ac8fcdc4f5fbc43fffee1a84dff86b
|
|
BLAKE2b-256 checksum How to use checksums |
dcda4bd91075fe8fcfc6e8b53bbb40d8c9681484eccb8be06eec78939be2c78f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.8.10 Linux/5.8.0-1033-azure
|
Security for blacksheep-context 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.