fastapi-class
fastapi-class copied to clipboard
How to inject router in class?
Hi Yezz, I've been using version 2 for a long time.
And one of the reasons is that, as the name package suggests, I can use it as a class and inject it into different routers according to my code needs, tests, use inheritance, use as a container, etc. But version 3 and above no longer use the class format, such as the Routable.
So I was using the end: app.include_router(container[IRestAPI].router)
How can I still use the fastapi-class as a class?
Thank you
Hello @fbrodrigorezino can you please include a simple example of what you are planning to use?
I'm trying to use FastAPI as class, but in the new version, I didn't find a way to do it, everything became decorators. I'm sure I'm missing something... here goes some code.
Pseudo code:
class IRestAPI(Routable):
@get(
"/api/v1/projects",
response_model=GetManagedRepositoriesResponse,
)
def get_all_projects(
self,
) -> GeManagedRepositoriesResponse:
raise NotImplementedError
class RestAPI(IRestAPI):
def __init__(
self,
get_all_projects_use_case: GetAllProjectsUseCase,
) -> None:
self._get_all_projects_use_case = get_all_projects_use_case
super().__init__()
@get(
"/api/v1/projects",
response_model=GetManagedRepositoriesResponse,
)
def get_all_projects(
self,
) -> GetManagedRepositoriesResponse:
.....
app = FastAPI()
app.include_router(RestAPI().router)
I removed some of the code that is not necessary, but I'm using it via containers like:
container[IRestAPI] = Singleton(RestAPI)
But it does not matter in this case.
I didn't find a way to continue the class structure for the new version. I think I'm missing something, as I'm sure you support it, given it's the name of the lib. So if you can give me some directions. thank you!