This release is a pre-release and may not be stable for production use.
Swarmauri Middleware Session
Session middleware for FastAPI and Starlette style applications that keeps a lightweight, in-memory session store synchronized with every request and response.
Overview
- Accepts an incoming session identifier from the
X-Session-IDrequest header (customisable throughsession_header). - Falls back to generating a new UUID session identifier and persists it on the response when none is supplied.
- Stores per-session data in
session_storage, allowing subsequent requests to reuse state keyed by the same identifier. - Writes the active session id to
request.state.session_idand mirrors it in thesession_idresponse cookie (customisable throughsession_cookie). - Uses a configurable
max_ageto control the lifetime of the emitted cookie.
Installation
Install from PyPI using the toolchain that fits your workflow.
pip
pip install swarmauri_middleware_session
Poetry
poetry add swarmauri_middleware_session
uv
uv venv
source .venv/bin/activate
uv pip install swarmauri_middleware_session
Example
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.testclient import TestClient
from swarmauri_middleware_session import SessionMiddleware
app = FastAPI()
session_middleware = SessionMiddleware()
@app.middleware("http")
async def attach_session(request: Request, call_next):
return await session_middleware.dispatch(request, call_next)
@app.get("/greet")
async def greet(request: Request):
session_id = request.state.session_id
session_data = session_middleware.session_storage.setdefault(session_id, {})
session_data["visits"] = session_data.get("visits", 0) + 1
return JSONResponse({"session_id": session_id, "visits": session_data["visits"]})
client = TestClient(app)
first_response = client.get("/greet")
first_data = first_response.json()
second_response = client.get(
"/greet", headers={"X-Session-ID": first_response.headers["X-Session-ID"]}
)
second_data = second_response.json()
print(first_data)
print(second_data)
Running the example prints the initial visit count followed by an incremented visit count for the same session, demonstrating how the middleware keeps track of state across requests.
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_session 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)
| File | Size | Uploaded | |
|---|---|---|---|
| swarmauri_middleware_session-0.11.0.dev1.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarmauri_middleware_session-0.11.0.dev1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.4 kB
Release files / swarmauri_middleware_session-0.11.0.dev1.tar.gz
| Download URL | swarmauri_middleware_session-0.11.0.dev1.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
67ec52e4e9111b899dfd20edb59257ffa681deb3d60990743fb03508b971a9a8
|
|
BLAKE2b-256 checksum How to use checksums |
34572df61ae2a8a0294f4ae279b8bcba1a43c244ae596c85af1dec1e996fc8e7
|
| 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_session-0.11.0.dev1-py3-none-any.whl
| Download URL | swarmauri_middleware_session-0.11.0.dev1-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ccf483966c7ca0878e5608a802ccf1ead6e3b654ea6f960f112d727107bfb745
|
|
BLAKE2b-256 checksum How to use checksums |
951fcda2b12258125722255c06c1108bd6ae2323672eddd9e319c2523906c900
|
| 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}
|