Example of using transaction-middleware
An example of using transaction-middleware with FastAPI is provided in the examples directory of the source code repository.
To run the example, follow these instructions:
Change to the examples/fastapi directory:
`bash
cd examples/fastapi
`
Install the required dependencies:
`bash
poetry install
`
Create an .env file with the following content:
`bash
TRANSACTION_MIDDLEWARE_LOG_LEVEL=DEBUG
TRANSACTION_MIDDLEWARE_HEADER=X-Transaction-ID
`
Run the example:
`bash
poetry run dotenv run python -m uvicorn demo.main:app --host 0.0.0.0 --port 8000 --reload
`
Open your browser and navigate to http://localhost:8000/docs to access the FastAPI Swagger UI.
Use the Swagger UI to test the /items/{item_id} endpoint. The transaction ID will be included in the response headers.
You can also use the curl command to test the endpoint:
‘http://localhost:8000/items/1’ -H ‘accept: application/json’
Check the logs to see the transaction ID being logged:
You can also include the transaction id header in the request:
‘http://localhost:8000/items/1’ -H ‘accept: application/json’ -H ‘X-Transaction-ID: 123456’
The http methods you can test are:
GET /items/{item_id} : Get an item by id and return the transaction id in the response headers. Transaction id will be created if it does not exists.
GET /status/{item_id}: Get the status of an item by id and return the transaction id in the response headers. Transaction id will not be created if it does not exists.
GET /simple/{item_id}: Get an item by id and return the transaction id in the response headers. Transaction id will be created and the tipe will be UUID4.