FastAPI / enable wiring for the DynamicContainer
See original GitHub issueHello, 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:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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.