Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inject router in class? #127

Open
fbrodrigorezino opened this issue Jul 26, 2023 · 2 comments
Open

How to inject router in class? #127

fbrodrigorezino opened this issue Jul 26, 2023 · 2 comments

Comments

@fbrodrigorezino
Copy link

fbrodrigorezino commented Jul 26, 2023

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

@yezz123
Copy link
Owner

yezz123 commented Jul 27, 2023

Hello @fbrodrigorezino can you please include a simple example of what you are planning to use?

@fbrodrigorezino
Copy link
Author

fbrodrigorezino commented Aug 18, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants