Package: fastapi-control
Welcome to FastAPI Control 👋FastAPI utility to implement class-based routing with controllers and dependency injection.Installpipinstallfastapi-controlUsagefromfastapiimportFastAPIfromfastapi_controlimport(APIController,add_controller,add_controllers,controller,get,inject,)# Optionally declares an abstractionclassGreeterAbstraction:defgreet(self):raiseNotImplementedError()# Implement the abstraction and make it available to the injection system# using the @inject decorator@inject(alias=GreeterAbstraction)classGretterImplementation:defgreet(self):return"Hello, world!"# It is also possible to implement without abstraction and make it available# to the injection system directly@inject()classSpanishGretterImplementation:defgreet(self):return"Hola, mundo!"@inject()classNestedGretterImplementation:# When the @inject decorator is used, the arguments of the __init__# method are automatically injected (if the @inject decorator was used# in the argument type declarations)def__init__(self,spanish_gretter:SpanishGretterImplementation)->None:self.gretter=spanish_gretterdefgreet(self):returnself.gretter.greet()# With the @controller decorator and inheriting from APIController, we can# declare class-based routing (also called controller) and it has the same# parameters as FastAPI's APIRouter@controller(prefix="/home")classHomeController(APIController):# When the @controller decorator is used, the arguments of the __init__# method are automatically injected (if the @inject decorator was used# in the argument type declarations)def__init__(self,gretter:GreeterAbstraction,spanish_gretter:SpanishGretterImplementation,nested_gretter:NestedGretterImplementation,)->None:self.gretter=gretterself.spanish_gretter=spanish_gretterself.nested_gretter=nested_gretter# The @get decorator declares the method as a GET endpoint (there are# also @post, @put, @delete, @patch decorators) and the behavior is the# same as the corresponding FastAPI decorators.@get(path="/greet")defget_greet(self):returnself.gretter.greet()@get(path="/spanish_greet")defget_spanish_greet(self):returnself.spanish_gretter.greet()@get(path="/nested_greet")defget_nested_greet(self):returnself.nested_gretter.greet()api=FastAPI()# Finally, it is necessary to add the controllers to the FastAPI instanceadd_controllers(api)# If you want to have multiple FastAPI instances with different controllers,# you can use the add_controller method to add the desired controllers to# the desired FastAPI instance one by one.other_api=FastAPI()add_controller(other_api,HomeController)InspirationsThis project is based on and inspired by theNEXTXandFastApi-RESTfulprojects.The difference withFastApi-RESTfulis thatFastAPI Controlimplements an automatic dependency injection system independent ofFastAPI.The difference withNEXTXis thatFastAPI Controlonly aims to solve the problem of class-based routes and automatic dependency injection, and uses thekinklibrary for dependency injection which is still under maintenance whileNEXTXusespython-injectwhich has not been maintained since 2020.Many thanks to the creators and maintainers of those projects for providing inspiration and guidance for this one.Authors👨🏻‍💻Leynier Gutiérrez GonzálezWebsite:leynier.devLinkedIn:@leynierGithub:@leynierTwitter:@leynier41🤝 ContributingContributions, issues and feature requests are welcome!Feel free to checkissues page. You can also take a look at thecontributing guide.Show your supportGive a ⭐️ if this project helped you!