Reusable utilities for FastAPI
Project description
🎨⚡️🔥 Reusable Utilities for FastAPI
Source Code: https://github.com/priyanshu-panwar/fastapi-utilities
Inspired From: dmontagu/fastapi-utils
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
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.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abda2ee6dff627bb92f3bc60c41a48e5da303947c03ce7f25184e06eea051bf6 |
|
MD5 | e112b8d54efb230e5596affd2aa353de |
|
BLAKE2b-256 | 56187287c92bc8d1283e1b9dfe450b5a833ad80cb5deb0293506bb4238c9b1f4 |