Package: fastapi-class
Classes and Decorators to use FastAPI with Class based routingSource Code:https://github.com/yezz123/fastapi-classInstall the project:pip install fastapi-classAs you create more complex FastAPI applications, you may find yourself frequently repeating the same dependencies in multiple related endpoints.A common question people have as they become more comfortable with FastAPI is how they can reduce the number of times they have to copy/paste the same dependency into related routes.fastapi_classprovides aclass-based viewdecorator@Viewto help reduce the amount of boilerplate necessary when developing related routes.Highly inspired byFastapi-utils, Thanks to@dmontagufor the great work.Example:fromfastapiimportFastAPI,APIRouter,QueryfrompydanticimportBaseModelfromfastapi_classimportViewapp=FastAPI()router=APIRouter()classItemModel(BaseModel):id:intname:strdescription:str=None@View(router)classItemView:defpost(self,item:ItemModel):returnitemdefget(self,item_id:int=Query(...,gt=0)):return{"item_id":item_id}app.include_router(router)Response model 📦Exceptionin list need to be either function that returnfastapi.HTTPExceptionitself. In case of a function it is required to have all of it's arguments to beoptional.fromfastapiimportFastAPI,APIRouter,HTTPException,statusfromfastapi.responsesimportPlainTextResponsefrompydanticimportBaseModelfromfastapi_classimportViewapp=FastAPI()router=APIRouter()NOT_AUTHORIZED=HTTPException(401,"Not authorized.")NOT_ALLOWED=HTTPException(405,"Method not allowed.")NOT_FOUND=lambdaitem_id="item_id":HTTPException(404,f"Item with{item_id}not found.")classItemResponse(BaseModel):field:str|None=None@View(router)classMyView:exceptions={"__all__":[NOT_AUTHORIZED],"put":[NOT_ALLOWED,NOT_FOUND]}RESPONSE_MODEL={"put":ItemResponse}RESPONSE_CLASS={"delete":PlainTextResponse}defget(self):...defput(self):...defdelete(self):...app.include_router(router)Customized EndpointsfromfastapiimportFastAPI,APIRouter,HTTPExceptionfromfastapi.responsesimportPlainTextResponsefrompydanticimportBaseModelfromfastapi_classimportView,endpointapp=FastAPI()router=APIRouter()NOT_AUTHORIZED=HTTPException(401,"Not authorized.")NOT_ALLOWED=HTTPException(405,"Method not allowed.")NOT_FOUND=lambdaitem_id="item_id":HTTPException(404,f"Item with{item_id}not found.")EXCEPTION=HTTPException(400,"Example.")classUserResponse(BaseModel):field:str|None=None@View(router)classMyView:exceptions={"__all__":[NOT_AUTHORIZED],"put":[NOT_ALLOWED,NOT_FOUND],"edit":[EXCEPTION]}RESPONSE_MODEL={"put":UserResponse,"edit":UserResponse}RESPONSE_CLASS={"delete":PlainTextResponse}defget(self):...defput(self):...defdelete(self):...@endpoint(("PUT",),path="edit")defedit(self):...Note:Theedit()endpoint is decorated with the@endpoint(("PUT",), path="edit")decorator, which specifies that this endpoint should handlePUTrequests to the/editpath,
using@endpoint("PUT", path="edit")has the same effectDevelopment 🚧Setup environment 📦You should create a virtual environment and activate it:python-mvenvvenv/sourcevenv/bin/activateAnd then install the development dependencies:# Install dependenciespipinstall-e.[test,lint]Run tests 🌝You can run all the tests with:bashscripts/test.shFormat the code 🍂Execute the following command to applypre-commitformatting:bashscripts/format.shExecute the following command to applymypytype checking:bashscripts/lint.shLicenseThis project is licensed under the terms of the MIT license.