question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FastAPI / enable wiring for the DynamicContainer

See original GitHub issue

Hello, I am struggled to implements FastAPI endpoints to work with DynamicContainer, something like this:

from dependency_injector import providers
from dependency_injector.containers import DynamicContainer
from dependency_injector.wiring import Provide
from fastapi import FastAPI, Depends, APIRouter


class ProductServices:
    def __init__(self):
        self.product_prefix = "P_"

    def get_product(self, product_id: int):
        return f"{self.product_prefix}{product_id}"


class RatingServices:
    def __init__(self, product_services=None):
        self.product_services = product_services

    def rate(self, product_id: int, point: int):
        product = self.product_services.get_product(product_id=product_id)
        return f"{product} has been rated {point} points"


router = APIRouter()


@router.get('/rate')
def rating(
        rating_services: RatingServices = Depends(Provide['rating_services'])
):
    if rating_services:
        return {
            'msg': rating_services.rate(10, 1)
        }


def populate_container(_container):
    _container.product_services = providers.Factory(ProductServices)
    _container.rating_services = providers.Factory(RatingServices,
                                                   product_services=_container.product_services)


def create_app():
    container = DynamicContainer()
    populate_container(container)

    app = FastAPI()
    app.container = container
    app.include_router(router=router)

    return app


app = create_app()

I can get an instance of rating_services after populate_container by calling container.rating_services() but cannot find a way to inject into FastAPI endpoint. Everytime making a request to /rate endpoint, FastAPI will response that:

AttributeError: 'Provide' object has no attribute 'rate'

Please help or show me any workaround. Thanks alot

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Mar 20, 2021

I’ve published version 4.30.0 that removes restriction on wiring a dynamic container. My apologies again for the confusion 😃 Closing the issue. Feel free to comment.

1reaction
rmk135commented, Mar 19, 2021

I’ve got an exception while make it wiring: “Can wire only an instance of the declarative container”

Oh, gosh, I’m so sorry…

I tested the code from previous comment on a branch where this exception is already removed. I’ll release that change and you would be able to wire dynamic container.

Apologies for a confusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using dynamic containers with Fastapi endpoint. #401 - GitHub
Wiring feature supports only declarative containers wiring. ... how to create the dynamic container during startup. i would have to somehow ...
Read more >
Changelog — Dependency Injector 4.41.0 documentation
Update FastAPI Redis example to use aioredis version 2 #613. Update documentation on creating ... 4.30.0¶. Remove restriction to wire a dynamic container....
Read more >
Dependencies - First Steps - FastAPI - tiangolo
FastAPI has a very powerful but intuitive Dependency Injection system. It is designed to be very simple to use, ... Share database connections....
Read more >
High-performing Apps With Python: A FastAPI Tutorial - Toptal
Python's FastAPI framework enables engineers to rapidly build new applications by ... so that it knows how to establish a connection with the...
Read more >
Getting Started - FastApi-MAIL
If you service does not provide username use sender address for connection. MAIL_PASSWORD : Password for authentication; MAIL_SERVER : SMTP Mail server.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found