Skip to main content

aioredis_fastapi

https://dl.circleci.com/status-badge/img/gh/wiseaidev/aioredis_fastapi/tree/main.svg?style=svg License Banner

aioredis_fastapi is an asynchronous redis based session backend for FastAPI powered applications.

🚸This repository is currently under testing, kind of production-ready.🚸

🛠️ Requirements

aioredis_fastapi requires Python 3.9 or above.

To install Python 3.9, I recommend using pyenv. You can refer to this section of the readme file on how to install poetry and pyenv into your linux machine.

🚨 Installation

With pip:

python3.9 -m pip install aioredis-fastapi

or by checking out the repo and installing it with poetry:

git clone https://github.com/wiseaidev/aioredis_fastapi.git && cd aioredis_fastapi && poetry install

🚸 Usage

from typing import Any
from fastapi import Depends, FastAPI, Request, Response
from aioredis_fastapi import (
    get_session_storage,
    get_session,
    get_session_id,
    set_session,
    del_session,
    SessionStorage,
)

app = FastAPI(title=__name__)


@app.post("/set-session")
async def _set_session(
    request: Request,
    response: Response,
    session_storage: SessionStorage = Depends(get_session_storage),
):
    session_data = await request.json()
    await set_session(response, session_data, session_storage)


@app.get("/get-session")
async def _get_session(session: Any = Depends(get_session)):
    return session


@app.post("/del-session")
async def _delete_session(
    session_id: str = Depends(get_session_id),
    session_storage: SessionStorage = Depends(get_session_storage),
):
    await del_session(session_id, session_storage)
    return None

🛠️ Custom Config

from aioredis_fastapi import settings
from datetime import timedelta
import random

settings(
   redis_url="redis://localhost:6379",
   session_id_name="session-id",
   session_id_generator=lambda: str(random.randint(1000, 9999)),
   expire_time= timedelta(days=1)
)

🌐 Interacting with the endpoints

from httpx import AsyncClient
import asyncio
from aioredis_fastapi.config import settings

async def main():
    client = AsyncClient()
    r = await client.post("http://127.0.0.1:8000/set-session", json=dict(a=1, b="data", c=True))
    r = await client.get("http://127.0.0.1:8000/get-session", cookies={settings().session_id_name: "ssid"})
    print(r.text)
    return r.text

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
    loop.run_until_complete(main())
finally:
    loop.close()
    asyncio.set_event_loop(None)

🎉 Credits

The following projects were used to build and test aioredis_fastapi.

👋 Contribute

If you are looking for a way to contribute to the project, please refer to the Guideline.

📝 License

This program and the accompanying materials are made available under the terms and conditions of the GNU GENERAL PUBLIC LICENSE.

Metadata

Release files for aioredis_fastapi 1.0.1

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

Source distribution (sdist)

Source distribution for aioredis_fastapi 1.0.1
File Size Uploaded
aioredis_fastapi-1.0.1.tar.gz 16.9 kB Details

Built distribution (wheel)

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

Total release size: 49.5 kB

Release files / aioredis_fastapi-1.0.1.tar.gz

Download URL aioredis_fastapi-1.0.1.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4bf005394ea386fa0ac21029142cf126da93cf9b07585e880a0e1ca89a3b774a
BLAKE2b-256 checksum
How to use checksums
342aaad9ad02e1e31e1db7f1c8f3febd54fc2caf76a70108e10d1ab1f276e864
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-39-generic

Release files / aioredis_fastapi-1.0.1-py3-none-any.whl

Download URL aioredis_fastapi-1.0.1-py3-none-any.whl
Size 32.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0267bc67a0a86d0ba7919c12593cf19530e5d7429cb5292b36e3c0b4c2d28fa6
BLAKE2b-256 checksum
How to use checksums
9552370b98b60cdc533983fdf1dd05a9b20eb038db13766a57743cc283acb51d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-39-generic

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.1

2 release files

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