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.

how to inject FastAPI bearer instances into Depends

See original GitHub issue

Hi, 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Digoyacommented, Nov 2, 2021

I ended up with something like this:

class ApplicationContainer(containers.DeclarativeContainer):
    partial_service = providers.Factory(Service, kw="foo")

@inject
def service(fastapi_dep=Depends(FastAPI_dependency), service=Depends(Provider[ApplicationContainer.partial_service])):
    return service(fastapi_dep)

ApplicationContainer.service = service

Later can be used as:

@router.post("/route")
@inject
async def handler(service=Depends(ApplicationContainer.service)):
    print(service)

1reaction
adriangbcommented, Aug 1, 2021

I think part of the trouble stems from the fact that Depends looks for an instance of Security at compile time, before anything is injected. I believe it would get an instance of whatever Provide[Container.bearer] returns, which is not going to be an instance of fastapi.security.base.SecurityBase.

Read more comments on GitHub >

github_iconTop 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 >

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