Session middleware for maintaining session state across requests.
Project description
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.
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_session-0.8.0.dev34.tar.gz.
File metadata
- Download URL: swarmauri_middleware_session-0.8.0.dev34.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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 |
aac0861df296aa761a44d123a27ee4a2f97c92da45d50dbe60347bc48d7238d0
|
|
| MD5 |
7578268bfe12212a1810794aaf736a7c
|
|
| BLAKE2b-256 |
3672e4c28b4c3a546ada863979c3e55be7e78a247c9318883dc92992b081ac3d
|
File details
Details for the file swarmauri_middleware_session-0.8.0.dev34-py3-none-any.whl.
File metadata
- Download URL: swarmauri_middleware_session-0.8.0.dev34-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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 |
5993806f110a8ffb8e9956d56b3f9262dbc39b2a604044a870fe62f0e8bbcbec
|
|
| MD5 |
c17e63ae0c872b9f723e12bfbfb421ac
|
|
| BLAKE2b-256 |
01ffed600aeb17130886f7096e948ac65fdcfa89c58518ab67c79374ec73aa45
|