[QUESTION] Parametrizing a class dependency on a sub-dependency
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
When dealing with sub-dependencies, the documentation introduces e.g.
oauth2_scheme = OAuth2PasswordBearer(...)
async def get_current_user(token: str = Depends(oauth2_scheme)):
...
Chained dependencies / sub-dependencies can be achieved with free functions.
The documentation also introduces parametrized dependencies in the form of callable instances, e.g.
class FixedContentQueryChecker:
def __init__(self, fixed_content: str):
self.fixed_content = fixed_content
def __call__(self, q: str = ""):
if q:
return self.fixed_content in q
return False
checker = FixedContentQueryChecker("bar")
@app.get("/query-checker/")
async def read_query_check(fixed_content_included: bool = Depends(checker)):
return {"fixed_content_in_query": fixed_content_included}
Now is there a way I can write a CurrentUserFetcher
class, parametrized on an OAuth2
instance?
This would take the following form:
oauth2_scheme = OAuth2PasswordBearer(...)
get_current_user = CurrentUserFetcher(oauth2_scheme)
@app.get("/foo/")
async def get_foo(current_user: User = Depends(get_current_user)):
...
Thank you
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:16 (5 by maintainers)
Top Results From Across the Web
Advanced Dependencies - FastAPI
All the dependencies we have seen are a fixed function or class. ... the parameters of the instance that we can use to...
Read more >How to add dependency of one parameter type on another in ...
In other words, I would like to parameterize the element type in the returned list without explicitly making it I or J or...
Read more >Dependency injection of parameters that depends on each other
Suppose your Body and Motor classes need the Car instance as a parameter. Instead of having the Car depend on Body and Motor...
Read more >The Simplest Guide to FastAPI Dependency Injection using ...
Learn how to use FastAPI Dependency Injection using Depends keywords to handle dependencies in the form of dict, classes, global dependency.
Read more >Dependency injection using parameters | F# for fun and profit
Six approaches to dependency injection, Part 2. ... In this post, we'll look at “dependency parameterization” as a way of ... No problem!...
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
Maybe something like this?
Take care with User get get_by_email update, the are operations examples and are not implemented in the example code.
Hmm, you can probably do something like: