Skip to main content

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transaction_middleware-0.1.7.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transaction_middleware-0.1.7-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file transaction_middleware-0.1.7.tar.gz.

File metadata

  • Download URL: transaction_middleware-0.1.7.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.3 Linux/6.5.0-1018-azure

File hashes

Hashes for transaction_middleware-0.1.7.tar.gz
Algorithm Hash digest
SHA256 c488b53738e97a0f84af53bf05a0d8886c69408cda26b1d30809c9a6008e7525
MD5 f062def5aa0e06ce51f070d22076c072
BLAKE2b-256 7af13a37b7248fe0fcf6c82db50edf34b724dbed0676581acde9da777fb5ca11

See more details on using hashes here.

File details

Details for the file transaction_middleware-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for transaction_middleware-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5425ff21f8d0c169c97742a43388120b5b9f1b7fabf5767d48c591514f84eb88
MD5 068772bc36e5e51886e681d6999149f5
BLAKE2b-256 a95443a822372188b52c4c5b2f45c465c9a7beda967eeb0cb1e9f844b7e84d2c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page