Last active
July 4, 2023 22:00
-
-
Save tadas-subonis/ae4ec242634701b684f023fc8a7bc39f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import Depends | |
from fastapi.dependencies.models import Dependant | |
from fastapi.dependencies.utils import solve_dependencies, get_dependant, get_parameterless_sub_dependant | |
import asyncio | |
def resolve_dependency(cls: Type): | |
dependant = get_dependant(path="", call=cls) | |
dependencies = [ | |
Depends(cls) | |
] | |
for depends in dependencies: | |
dependant.dependencies.insert( | |
0, | |
get_parameterless_sub_dependant(depends=depends, path="something"), | |
) | |
dependency = solve_dependencies( | |
request=Request({"type": "http", "query_string": "", "headers": {}}), | |
dependant=dependant, | |
body={}, | |
) | |
values, errors, background_tasks, response, dependency_cache = asyncio.run(dependency) | |
for key, value in dependency_cache.items(): | |
type, _ = key | |
if type == cls: | |
return value | |
raise Exception("No found dependency") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment