[FEATURE] Add universal dependency support
See original GitHub issueIt 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:
- Created 4 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top 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 >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
You can achieve this for the full app now via:
app_router = APIRouter()
app_router
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.
If I use
.include_router(router, dependencies=Depends(security))
and then one of the endpoints needs to see the credentials thatsecurity
decoded from theAuthorization
header field, how do I go about that?