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.

[FEATURE] Add universal dependency support

See original GitHub issue

It can often be useful to essentially add an app-wide dependency. Suppose, for example, you want to enforce http basic auth on non-production environments:

async def basic_auth(credentials: HTTPBasicCredentials = Depends(security)):
    if (settings.SITEWIDE_BASIC_AUTH and (
            credentials.username != settings.SITEWIDE_BASIC_AUTH_USERNAME or
            credentials.password != settings.SITEWIDE_BASIC_AUTH_PASSWORD)):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Incorrect email or password",
            headers={"WWW-Authenticate": "Basic"},
        )

app.add_dependency(basic_auth)

Now, you could do something similar with middleware, but then you’d miss out on dependency injection. You can already do something similar for included routers, but not for the entire app.

Just a thought.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dmontagucommented, Nov 10, 2019

You can achieve this for the full app now via:

  1. app_router = APIRouter()
  2. Add all desired routes to app_router
  3. Call app.include_router(app_router, dependencies=app_dependencies)

Maybe you realized this already, just wanted to share in case it’s useful (I use this pattern myself).


In general, I agree that it would make more sense to me to specify dependencies when initializing the routers, rather than when including the routers, but due to the way that the route creation decorators work, and the way route inclusion works, I think it would be a relatively involved refactor to change this. I assume this is why @tiangolo designed the API the way it is now.

0reactions
dimaqqcommented, Nov 19, 2021

If I use .include_router(router, dependencies=Depends(security)) and then one of the endpoints needs to see the credentials that security decoded from the Authorization header field, how do I go about that?

  1. is there a way to poke at router dependency values somehow?
  2. should I explicitly include that dependency in the special endpoint?
    • will fastapi eval it once and cache the result?
Read more comments on GitHub >

github_iconTop Results From Across the Web

Features in UD v2 - Universal Dependencies
For a number of existing features, add new values that may be or have already been ... Turkish?), which supports interrogativity as a...
Read more >
UD tools - Universal Dependencies
If you would like to have your tool added to this page, please submit a pull ... It supports a number of features,...
Read more >
Introduction - Universal Dependencies
Universal Dependencies (UD) is a project that is developing cross-linguistically consistent treebank annotation for many languages, with the goal of ...
Read more >
Universal Dependencies
Universal Dependencies (UD) is a framework for consistent annotation of grammar (parts of speech, morphological features, and syntactic dependencies) across ...
Read more >
Syntax: General Principles - Universal Dependencies
Function words attach as direct dependents of the most closely related content ... Further support for this analysis comes from the possibility of...
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