Skip to main content

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

Project description

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 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.

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

aioredis_fastapi-1.0.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

aioredis_fastapi-1.0.0-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file aioredis_fastapi-1.0.0.tar.gz.

File metadata

  • Download URL: aioredis_fastapi-1.0.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.10 Linux/5.15.0-43-generic

File hashes

Hashes for aioredis_fastapi-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b22d36c5080c18fd1d3865496ec4f9855a38e69e8350771ed141bdcc9f195ee9
MD5 4b0981f35d1a2421f916bba4e6343ff9
BLAKE2b-256 4c28445d30c0243d7c896d5be078c2eaae56ed932fce5e841349fe22b8c7f8c8

See more details on using hashes here.

File details

Details for the file aioredis_fastapi-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: aioredis_fastapi-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 32.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.10 Linux/5.15.0-43-generic

File hashes

Hashes for aioredis_fastapi-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d036e024f2b720a395c8007cc837f343f2842a006be0e6f7ef443c5e4871bbc0
MD5 c026177d1ed2316f6a8592d08f98dfab
BLAKE2b-256 4dac6578bce2b7dc9beb93a234284fdbacf79bdcd4a572ef17b4e3e95c48a780

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