Skip to content

how to inject FastAPI bearer instances into Depends #470

Open
@mxab

Description

@mxab

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions