Releases: priyanshu-panwar/fastapi-utilities
Releases · priyanshu-panwar/fastapi-utilities
0.3.0
What's Changed
- [✨Update✨] How to use with Latest FastAPI version by @priyanshu-panwar in #15
- Bump idna from 3.4 to 3.7 by @dependabot in #16
- Include the invalid cron expression in the exception message by @benjamb in #21
- Bump certifi from 2023.7.22 to 2024.7.4 by @dependabot in #19
- Resolve
imports
by @priyanshu-panwar in #23
New Contributors
Full Changelog: 0.2.0...0.3.0
What's Changed
- [✨Update✨] How to use with Latest FastAPI version by @priyanshu-panwar in #15
- Bump idna from 3.4 to 3.7 by @dependabot in #16
- Include the invalid cron expression in the exception message by @benjamb in #21
- Bump certifi from 2023.7.22 to 2024.7.4 by @dependabot in #19
- Resolve
imports
by @priyanshu-panwar in #23
New Contributors
Full Changelog: 0.2.0...0.3.0
✨ Introduction of `cli` tool
✨ 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`
🚨Support for python3.12
🚀Cached Sessions
- 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
- 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
- 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
- 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")