Package: webapicontrollers
Web API ControllersDescriptionSimple Web API controller framework for FastAPIInstallationTo install this package, use pip:pipinstallwebapicontrollersExamplefromfastapiimportFastAPI,HTTPException,Requestfromfastapi.responsesimportJSONResponsefromsrc.webapicontrollersimportAPIController,Get,Post,Patch,Delete,Put,RoutePrefix@RoutePrefix('/test')classTestController(APIController):def__init__(self,app:FastAPI)->None:super().__init__(app)@Get('/')asyncdefget(self)->dict:return{"method":"GET","path":"/"}@Get('/400')asyncdefget_bad_request(self)->dict:raiseHTTPException(status_code=400,detail="Bad Request")@Get('/401')asyncdefget_not_authorized(self)->dict:raiseHTTPException(status_code=401,detail="Not Authorized")@Get('/403')asyncdefget_forbidden(self)->dict:raiseHTTPException(status_code=403,detail="Forbidden")@Get('/404')asyncdefget_not_found(self)->dict:raiseHTTPException(status_code=404,detail="Not Found")@Get('/405')asyncdefget_method_not_allowed(self)->dict:raiseHTTPException(status_code=405,detail="Method Not Allowed")@Get('/500')asyncdefget_internal_server_error(self)->dict:raiseHTTPException(status_code=500,detail="Internal Server Error")@Get('/{arg}')asyncdefget_with_arg(self,arg)->dict:return{"method":"GET","path":"/","arg":arg}@Post('/')asyncdefpost(self)->dict:return{"method":"POST","path":"/"}@Post('/{arg}')asyncdefpost_with_arg(self,arg)->dict:return{"method":"POST","path":"/","arg":arg}@Put('/')asyncdefput(self)->dict:return{"method":"PUT","path":"/"}@Put('/{arg}')asyncdefput_with_arg(self,arg)->dict:return{"method":"PUT","path":"/","arg":arg}@Patch('/')asyncdefpatch(self)->dict:return{"method":"PATCH","path":"/"}@Patch('/{arg}')asyncdefpatch_with_arg(self,arg)->dict:return{"method":"PATCH","path":"/","arg":arg}@Delete('/')asyncdefdelete(self)->dict:return{"method":"DELETE","path":"/"}@Delete('/{arg}')asyncdefdelete_with_arg(self,arg)->dict:return{"method":"DELETE","path":"/","arg":arg}defbad_request(self,request:Request,exc:HTTPException)->JSONResponse:# Custom handling codereturnsuper().bad_request(request,exc)defnot_authorized(self,request:Request,exc:HTTPException)->JSONResponse:# Custom handling codereturnsuper().not_authorized(request,exc)defforbidden(self,request:Request,exc:HTTPException)->JSONResponse:# Custom handling codereturnsuper().forbidden(request,exc)defnot_found(self,request:Request,exc:HTTPException)->JSONResponse:# Custom handling codereturnsuper().not_found(request,exc)defmethod_not_allowed(self,request:Request,exc:HTTPException)->JSONResponse:# Custom handling codereturnsuper().method_not_allowed(request,exc)definternal_server_error(self,request:Request,exc:HTTPException)->JSONResponse:# Custom handling codereturnsuper().internal_server_error(request,exc)app=FastAPI()TestController(app)Known IssuesIf you overide the handler methods such as not_found etc. in more than one cotnroller only one handler will be registered on a last one wins basis.
Implementing a per route prefix handling system is on the to do list.If you create a base controller class and then overide it's methods in a derived class the path needs to be the same in both methods.
If you don't do this then FastAPI gets confused about which handler maps to which path.CautionThis project is in a very early state and might not be very useful to anyone yet. There is no support avilable, use at your own risk.LicenseThis project is licensed under theMIT License.