Skip to content

Possibility to inject FastAPI Request object as argument of router dependency #363

Open
@AshurovRustam

Description

@AshurovRustam

Hi mates, trying use this beautiful lib with FastApi as better IoC container but not sure whether it is possible to inject http Request object as argument of FastApi router dependency.

So let's say I have a router where a Service is injected:
@router.get('/') @inject async def get(search_service: SearchService = Depends(Provide[Container.service])):...

Service itself has ctor parameter of http Request type:
class Service: def __init__(self, request: Request):...

And in container it is registered via Factory provider:
service=providers.Factory(Service)

As result during http request argument is not given and exception is thrown.
However FastApi framework without DependencyInjector able to analyze constructor parameters and provide instance of http request in case it is required via Depends built in DI mechanism:
@router.get('/') async def get(search_service: SearchService = Depends(SearchService)):...
But for sure in future Service could have other dependencies and I prefer to use dedicated IoC container with its separated bindings
So yeah the question is: is it possible to get http Request object somehow automatically?

Many thanks

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions