Skip to main content

Session middleware for maintaining session state across requests.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_session


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-ID request header (customisable through session_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_id and mirrors it in the session_id response cookie (customisable through session_cookie).
  • Uses a configurable max_age to 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swarmauri_middleware_session-0.8.0.dev45.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_middleware_session-0.8.0.dev45.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_session-0.8.0.dev45.tar.gz
  • Upload date:
  • Size: 8.1 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

Hashes for swarmauri_middleware_session-0.8.0.dev45.tar.gz
Algorithm Hash digest
SHA256 674c511db0b501f502b201259992071b07cd68d356673bc4d85c131f67d8395b
MD5 b3a09d9d89083aa3a8f352f68d3b6409
BLAKE2b-256 73ca4c87c801d7c430e63a09ef3f74b2e273517384d4011a051f4d21cf051437

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_session-0.8.0.dev45-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_session-0.8.0.dev45-py3-none-any.whl
  • Upload date:
  • Size: 9.1 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

Hashes for swarmauri_middleware_session-0.8.0.dev45-py3-none-any.whl
Algorithm Hash digest
SHA256 8353f514ef8cbe3d2615f98b44870979cad24ed9de827f086a5a282f385a1c40
MD5 b78b1a11cdf4ab575e76022a52bba194
BLAKE2b-256 e78267e8f473cbb2a462d199589516c30e42ce7d8618225bca19f4980faae9ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page