how to inject FastAPI bearer instances into Depends
See original GitHub issueHi, I’m currently struggeling on how to get this to work.
I have a third party “factory method” that provides me with a method I can use in FastAPI’s Depends resolver
from third_party import auth_factory
@app.get("/foo")
def foo(user = Depends(auth_factory(scope="read", extractor = some_token_extractor ))):
doSomething()
the oauth_factory
produces as callable with fastapi know signature
def user_checker(oauth_header:HTTPAuthorizationCredentials = Security(bearer))
...
return user
My problem now is when I want the some_token_extractor
be provided by python-dependency-injector
something like this does not work:
user_detail_extractor = Provide[Container.user_detail_extractor]
@app.get("/with_di")
def with_di(user = Depends(auth_factory(scope="read", extractor = user_detail_extractor ))):
return {
"user": user["username"]
}
class Container(DeclarativeContainer):
user_detail_extractor = providers.Callable(some_di_extractor, some_config="bla")
I have a full demo setup here https://github.com/mxab/fastapi-di-depends-issue/tree/main/fastapi_di_depends_issue
but I cannot get this test work: https://github.com/mxab/fastapi-di-depends-issue/blob/main/tests/test_fastapi_di_depends_issue.py#L27
Does this in general not work or what is it I’m missing
Thanks very much in advance
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:14 (2 by maintainers)
Top Results From Across the Web
Dependencies - First Steps - FastAPI - tiangolo
With the Dependency Injection system, you can also tell FastAPI that your path operation function also "depends" on something else that should be...
Read more >Advanced Dependencies - FastAPI
A "callable" instance¶. In Python there's a way to make an instance of a class a "callable". ... from fastapi import Depends, FastAPI...
Read more >Classes as Dependencies - FastAPI
This creates an "instance" of that class and the instance will be passed as the parameter commons to your function. Type annotation vs...
Read more >Sub-dependencies - FastAPI
Let's focus on the parameters declared: Even though this function is a dependency ("dependable") itself, it also declares another dependency (it "depends" on...
Read more >Testing Dependencies with Overrides - FastAPI
For these cases, your FastAPI application has an attribute app.dependency_overrides , it is a simple dict . To override a dependency for testing,...
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 ended up with something like this:
Later can be used as:
I think part of the trouble stems from the fact that
Depends
looks for an instance ofSecurity
at compile time, before anything is injected. I believe it would get an instance of whateverProvide[Container.bearer]
returns, which is not going to be an instance offastapi.security.base.SecurityBase
.