[Python] How to pass an argument to a 'dependencies' function in FastAPI?

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a way to pass an argument to a function defined within the dependencies argument of, for instance, app.get? If there isn't, what other options exist ?

    I can do the following to test a request, including examining the bearer token, before it reaches the endpoint handler:

    async def verify_token(Authorization: Annotated[str, Header()]):
    # decode token and do processing based on it

    @app.get("/read_items/", dependencies=[Depends(verify_token)])
    async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]


    but what I really want to do is provide an argument to verify_token, like this:

    @app.get("/read_items/", dependencies=[Depends(verify_token("red")])
    async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]


    in this scenario verify_token would accept a string as an argument and the value of that string would be easily seen by anyone inspecting the code.

    Of course this type of outcome could be produced by using a decorator:

    @verify_token_decorator("red")
    @app.get("/read_items/")
    async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]


    but decorators cannot inspect the contents of Request headers and so such a decorator would not be able to inspect the token.

    Is there a way to do the functional equivalent of passing a value into a function within the dependencies argument of an end point?

    Continue reading...

Compartilhe esta Página