Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_time Discord

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 logging module.
  • Measures the total time spent handling a request and attaches X-Request-Duration and X-Request-Start-Time headers to the response.
  • Raises an HTTPException with status code 500 if 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.

Metadata

Release files for swarmauri_middleware_time 0.11.0.dev1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swarmauri_middleware_time 0.11.0.dev1
File Size Uploaded
swarmauri_middleware_time-0.11.0.dev1.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swarmauri_middleware_time 0.11.0.dev1
File Interpreter ABI Platform
swarmauri_middleware_time-0.11.0.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / swarmauri_middleware_time-0.11.0.dev1.tar.gz

Download URL swarmauri_middleware_time-0.11.0.dev1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
152f1c4908dabc3a2567c87b2ca881189f3f2134f48730fbcaa7a142edd2618f
BLAKE2b-256 checksum
How to use checksums
b4bbcfc5030ca77d55cda65445bb805504c6f7d1eefc5d30de74c64693f41631
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}

Release files / swarmauri_middleware_time-0.11.0.dev1-py3-none-any.whl

Download URL swarmauri_middleware_time-0.11.0.dev1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7ecc471ca45b31a43473fe756cf2406c1b6fe1c3d175ddabacfffab9b069588
BLAKE2b-256 checksum
How to use checksums
54801d913afcabc2e0c91f5f7ec9d23d909ab9dd033c7481d43dce46f4d958f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page