Async Transaction Middleware for FastAPI/Starlette
Project description
transaction-middleware
Async Transaction Middleware for FastAPI/Starlette.
Installation
Using pip:
pip install transaction-middleware
Using poetry
poetry add transaction-middleware
How to use it
Transaction Middleware follows the middleware protocol and, therefore, should be added as a middleware to your FastApi or Starlette application.
The steps, using FastAPI:
from fastapi import FastAPI, Depends
from starlette.requests import Request
from starlette.responses import Response
from transaction_middleware import (
TransactionMiddleware,
get_transaction_id,
transaction_id_required,
)
app: FastAPI = FastAPI()
app.add_middleware(TransactionMiddleware)
@app.get(
"/items/{id}",
tags=["Item"],
)
async def read_items(
request: Request,
response: Response,
id: str,
transaction_id: str = Depends(get_transaction_id()),
):
return {
"id": id,
"transaction_id": transaction_id if transaction_id else "No transaction ID",
}
Then set the environment variables (or your .env file)
TRANSACTION_MIDDLEWARE_LOG_LEVEL=DEBUG
TRANSACTION_MIDDLEWARE_HEADER=X-Transaction-ID
Launch the server.
Call the method you want to test, and, optionally, set the transaction Id on the headers.
curl -X GET http://localhost:8000/items/1234 -H "X-Transaction-ID: 2fyJr1FbRj603pH4rweEfEzQ"
Middleware configuration
The middleware configuration is done by environment variables (or using and .env file if your project uses python-dotenv).
The main variables are shown in the table below:
Name | Description | Values | Default |
---|---|---|---|
TRANSACTION_MIDDLEWARE_LOG_LEVEL | Log level for the application | DEBUG, INFO, WARNING, ERROR, CRITICAL | INFO |
TRANSACTION_MIDDLEWARE_LOG_FORMAT | Log format | See python logger documentation | %(log_color)s%(levelname)-9s%(reset)s %(asctime)s %(name)s %(message)s |
TRANSACTION_MIDDLEWARE_HEADER | Name for the header | Any String | X-Transaction-ID |
Project details
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
Close
Hashes for transaction_middleware-0.1.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0991986837389ccbb421c2b146a0443f6e6af7811d817e7c5afa196480fb5096 |
|
MD5 | b9e025bb7b473d7325846dcbfcd753c4 |
|
BLAKE2b-256 | 3a2f9bee1cfd33634637168f8f380e693aa3fa114986a995f88d6dcf626b0920 |
Close
Hashes for transaction_middleware-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c282dc205b244e0b09de9654f593ab9ca97c08c8ad8d0611ef0b0bf23255f816 |
|
MD5 | bd7f1708d7d3248f3bbc004bd81c212c |
|
BLAKE2b-256 | c8d692e2ea09988b53a48439432c7647787dcda956bda315f439f6c22a95057c |