Middleware for tracking request processing time in Swarmauri applications
Project description
Swarmauri Middleware Time
Middleware for tracking request processing time in Swarmauri applications.
Features
- Logs when each request starts and when it completes using Python's
loggingmodule. - Measures the total time spent handling a request and attaches
X-Request-DurationandX-Request-Start-Timeheaders to the response. - Raises an
HTTPExceptionwith status code500if an unexpected error occurs while the downstream handler is executing.
Installation
pip
pip install swarmauri_middleware_time
Poetry
poetry add swarmauri_middleware_time
uv
# Install uv if it is not already available
curl -LsSf https://astral.sh/uv/install.sh | sh
# Add the middleware to your project
uv add swarmauri_middleware_time
Usage
The middleware exposes a standard dispatch method that matches FastAPI's middleware
interface. Instantiate it once and forward requests through dispatch inside an
@app.middleware("http") hook:
from fastapi import FastAPI, Request
from starlette.testclient import TestClient
from swarmauri_middleware_time import TimerMiddleware
app = FastAPI()
timer_middleware = TimerMiddleware()
@app.middleware("http")
async def add_timer(request: Request, call_next):
return await timer_middleware.dispatch(request, call_next)
@app.get("/ping")
async def ping():
return {"message": "pong"}
if __name__ == "__main__":
client = TestClient(app)
response = client.get("/ping")
assert response.json() == {"message": "pong"}
assert "X-Request-Duration" in response.headers
assert float(response.headers["X-Request-Duration"]) >= 0.0
assert "X-Request-Start-Time" in response.headers
print("Duration header:", response.headers["X-Request-Duration"])
print("Start time header:", response.headers["X-Request-Start-Time"])
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarmauri_middleware_time-0.8.0.dev45.tar.gz.
File metadata
- Download URL: swarmauri_middleware_time-0.8.0.dev45.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c0d3b72489674cac6130f9d957870e5c8436e1482c5415726d53d77414db74c
|
|
| MD5 |
cf6069cc83d9050e1c4d093beafd3dfe
|
|
| BLAKE2b-256 |
f6c287152f024aa3f40b47c8313f92a25ef4af335956e07f19210cf74d9e14ae
|
File details
Details for the file swarmauri_middleware_time-0.8.0.dev45-py3-none-any.whl.
File metadata
- Download URL: swarmauri_middleware_time-0.8.0.dev45-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84cfad0a92ad573e9d0114643dcca8dce960ab07a519d0c30e14edfc462ad145
|
|
| MD5 |
b33024ec7e74996f710231ee5ad706d0
|
|
| BLAKE2b-256 |
77829070ddc96a65e7740288911474d3d9bde56dd1feaa3559be81780adda51e
|