Skip to content

Releases: priyanshu-panwar/fastapi-utilities

✨ Introduction of `cli` tool

23 Dec 11:57
Compare
Choose a tag to compare

✨ Introduction of cli tool

Use our cli tool to get a fastapi skeleton built for you to directly get you started with coding.

How to use

  • Using poetry: poetry run cli init
  • Using pip: python3 -m cli init

🚨Support for `python3.12`

08 Oct 10:49
Compare
Choose a tag to compare

🚨Support for python3.12

🚀Cached Sessions

08 Oct 10:22
Compare
Choose a tag to compare
  • Cached Sessions: Now use cached sessions along with context manager instead of get_db.
from fastapi import FastAPI
from .db import Base, engine
from fastapi_utilities import FastAPISessionMaker, repeat_every
from .models import User
import random

app = FastAPI()
Base.metadata.create_all(bind=engine)

session_maker = FastAPISessionMaker("sqlite:///db.sqlite3")


@app.on_event("startup")
@repeat_every(seconds=5, raise_exceptions=True)
async def startup():
    print("Starting up...")
    with session_maker.context_session() as session:
        x = User(id=random.randint(0, 10000))
        session.add(x)
    print("Startup complete!")

🕒Timer Middleware

07 Oct 10:24
edc00cf
Compare
Choose a tag to compare
  • Timer Middleware: Add a middleware to the FastAPI app that logs the time taken to process a request. Optionally, also logs the average response time.The average response time is reset after every (reset_after)100,000 requests.

import asyncio
from fastapi import FastAPI, Request
from fastapi_utilities import add_timer_middleware

app = FastAPI()
add_timer_middleware(app, show_avg=True)


@app.get("/")
def read_root():
    return {"message": "Hello, World!"}

Response Logs:

INFO:     (fastapi-utilities) "GET - /" :: Time Taken :: 0.97 ms
INFO:     :: Average Response Time :: 0.97 ms

🚀Cron Jobs

06 Oct 15:21
Compare
Choose a tag to compare
  • Cron Jobs: Easily trigger cron jobs on server startup using repeat_at by providing a cron expression.
from fastapi_utilities import repeat_at

@router.on_event("startup")
@repeat_at(cron="*/2 * * * *") #every 2nd minute
async def hey():
    print("hey")

🚀Repeated Tasks

04 Oct 12:21
Compare
Choose a tag to compare
  • Repeated Tasks: Easily trigger periodic tasks on server startup.
from fastapi_utilities.repeat import repeat_every

@router.on_event('startup')
@repeat_every(seconds=3)
async def print_hello():
    print("hello")