Releases: yezz123/fastapi-class
Releases · yezz123/fastapi-class
Add More Classifiers & Fix Typo ✨
FastAPI-Class 1.0.0 ✨
Classes and Decorators to use FastAPI with class based routing
. In particular this allows you to
construct an instance of a class and have methods of that instance be route handlers for FastAPI & Python 3.8.
- Older Versions of Python:
- Unfortunately this does not work with
async
routes with Python versions less than 3.8 due to bugs ininspect.iscoroutinefunction
. Specifically with older versions of Pythoniscoroutinefunction
incorrectly returns false soasync
routes aren'tawait
'd. We therefore only support Python versions >= 3.8.
- Unfortunately this does not work with
Example 🐢
from ping import pong
# Some fictional ping pong class
from fastapi_class import Routable, get, delete
def parse_arg() -> argparse.Namespace:
"""parse command line arguments."""
...
class UserRoutes(Routable):
"""Inherits from Routable."""
# Note injection here by simply passing values to the constructor. Other injection frameworks also
# supported as there's nothing special about this __init__ method.
def __init__(self, pong: pong) -> None:
"""Constructor. The pong is injected here."""
self.__pong = pong
@get('/user/{name}')
def get_user_by_name(name: str) -> User:
# Use our injected pong instance.
return self.__pong.get_user_by_name(name)
@delete('/user/{name}')
def delete_user(name: str) -> None:
self.__pong.delete(name)
def main():
args = parse_args()
# Configure the pomg per command line arguments
pong = pong(args.url, args.user, args.password)
# Simple intuitive injection
user_routes = UserRoutes(pong)
app = FastAPI()
# router member inherited from Routable and configured per the annotations.
app.include_router(user_routes.router)