# transaction-middleware
Async Transaction Middleware for FastAPI/Starlette.
## Installation
Using pip:
```bash
pip install transaction-middleware
```
Using poetry
```bash
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:
```python
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)
```bash
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.
```bash
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 |
Raw data
{
"_id": null,
"home_page": "https://impalah.github.io/transaction-middleware/",
"name": "transaction-middleware",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "transaction, middleware, fastapi, starlette",
"author": "impalah",
"author_email": "impalah@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/7a/f1/3a37b7248fe0fcf6c82db50edf34b724dbed0676581acde9da777fb5ca11/transaction_middleware-0.1.7.tar.gz",
"platform": null,
"description": "# transaction-middleware\n\nAsync Transaction Middleware for FastAPI/Starlette.\n\n## Installation\n\nUsing pip:\n\n```bash\npip install transaction-middleware\n```\n\nUsing poetry\n\n```bash\npoetry add transaction-middleware\n```\n\n\n## How to use it\n\nTransaction Middleware follows the middleware protocol and, therefore, should be added as a middleware to your FastApi or Starlette application.\n\nThe steps, using FastAPI:\n\n```python\n\nfrom fastapi import FastAPI, Depends\nfrom starlette.requests import Request\nfrom starlette.responses import Response\nfrom transaction_middleware import (\n TransactionMiddleware,\n get_transaction_id,\n transaction_id_required,\n)\n\napp: FastAPI = FastAPI()\napp.add_middleware(TransactionMiddleware)\n\n@app.get(\n \"/items/{id}\",\n tags=[\"Item\"],\n)\nasync def read_items(\n request: Request,\n response: Response,\n id: str,\n transaction_id: str = Depends(get_transaction_id()),\n):\n return {\n \"id\": id,\n \"transaction_id\": transaction_id if transaction_id else \"No transaction ID\",\n }\n\n\n```\n\nThen set the environment variables (or your .env file)\n\n```bash\nTRANSACTION_MIDDLEWARE_LOG_LEVEL=DEBUG\nTRANSACTION_MIDDLEWARE_HEADER=X-Transaction-ID\n\n```\n\nLaunch the server.\n\nCall the method you want to test, and, optionally, set the transaction Id on the headers.\n\n```bash\ncurl -X GET http://localhost:8000/items/1234 -H \"X-Transaction-ID: 2fyJr1FbRj603pH4rweEfEzQ\"\n```\n\n\n## Middleware configuration\n\nThe middleware configuration is done by environment variables (or using and .env file if your project uses python-dotenv).\n\nThe main variables are shown in the table below:\n\n| Name | Description | Values | Default |\n| --------- | --------- | --------- | --------- |\n| TRANSACTION_MIDDLEWARE_LOG_LEVEL | Log level for the application | DEBUG, INFO, WARNING, ERROR, CRITICAL | INFO |\n| TRANSACTION_MIDDLEWARE_LOG_FORMAT | Log format | See python logger documentation | %(log_color)s%(levelname)-9s%(reset)s %(asctime)s %(name)s %(message)s |\n| TRANSACTION_MIDDLEWARE_HEADER | Name for the header | Any String | X-Transaction-ID |\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Async Transaction Middleware for FastAPI/Starlette",
"version": "0.1.7",
"project_urls": {
"Documentation": "https://impalah.github.io/transaction-middleware/",
"Homepage": "https://impalah.github.io/transaction-middleware/",
"Repository": "https://github.com/impalah/transaction-middleware",
"Source": "https://github.com/impalah/transaction-middleware"
},
"split_keywords": [
"transaction",
" middleware",
" fastapi",
" starlette"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a95443a822372188b52c4c5b2f45c465c9a7beda967eeb0cb1e9f844b7e84d2c",
"md5": "068772bc36e5e51886e681d6999149f5",
"sha256": "5425ff21f8d0c169c97742a43388120b5b9f1b7fabf5767d48c591514f84eb88"
},
"downloads": -1,
"filename": "transaction_middleware-0.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "068772bc36e5e51886e681d6999149f5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 5890,
"upload_time": "2024-05-04T11:16:45",
"upload_time_iso_8601": "2024-05-04T11:16:45.143543Z",
"url": "https://files.pythonhosted.org/packages/a9/54/43a822372188b52c4c5b2f45c465c9a7beda967eeb0cb1e9f844b7e84d2c/transaction_middleware-0.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7af13a37b7248fe0fcf6c82db50edf34b724dbed0676581acde9da777fb5ca11",
"md5": "f062def5aa0e06ce51f070d22076c072",
"sha256": "c488b53738e97a0f84af53bf05a0d8886c69408cda26b1d30809c9a6008e7525"
},
"downloads": -1,
"filename": "transaction_middleware-0.1.7.tar.gz",
"has_sig": false,
"md5_digest": "f062def5aa0e06ce51f070d22076c072",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 4338,
"upload_time": "2024-05-04T11:16:46",
"upload_time_iso_8601": "2024-05-04T11:16:46.206615Z",
"url": "https://files.pythonhosted.org/packages/7a/f1/3a37b7248fe0fcf6c82db50edf34b724dbed0676581acde9da777fb5ca11/transaction_middleware-0.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-04 11:16:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "impalah",
"github_project": "transaction-middleware",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "transaction-middleware"
}