Reusable utilities for FastAPI
Project description
🎨⚡️🔥 Reusable Utilities for FastAPI
Source Code: https://github.com/priyanshu-panwar/fastapi-utilities
Inspired From: dmontagu/fastapi-utils
[🔥New🔥] FastAPI CLI Tool
With our CLI Tool you can get a skeleton project built to get you started with the code.
How to use
- Using
poetry
:poetry run cli init
- Using
pip
:python3 -m cli init
Features
This package includes a number of utilities to help reduce boilerplate and reuse common functionality across projects:
- 🕒Repeated Tasks: Easily trigger periodic tasks on server startup using repeat_every.
from fastapi_utilities import repeat_every
@router.on_event('startup')
@repeat_every(seconds=3)
async def print_hello():
print("hello")
- 👷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")
- 🕒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
- 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!")
Requirements
This package is intended for use with any recent version of FastAPI and Python 3.7+.
Installation
pip install fastapi-utilities
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_utilities-0.2.1a0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81fe477f8aeaf0331fadcd63a209e638138db105058114a364e50fbb9d2c5ca6 |
|
MD5 | e945a3cfc4d34ab53dd400b8d0350891 |
|
BLAKE2b-256 | 906f0bb773161ab2cc4132c721db95eed1b430b04c25056e530a8126d2fb06c9 |
Close
Hashes for fastapi_utilities-0.2.1a0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4948d11d99308ade4f1d10c29ce419978313b919546ecf24dbd06e9d88e0be6 |
|
MD5 | d09065e68bad4ef34264b0c4346e725b |
|
BLAKE2b-256 | 0bb8a836625bb1115ef1b315ea3a2e6aad559500bc528e56a6d1c700fc07ba29 |