Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tadas-subonis/ae4ec242634701b684f023fc8a7bc39f to your computer and use it in GitHub Desktop.
Save tadas-subonis/ae4ec242634701b684f023fc8a7bc39f to your computer and use it in GitHub Desktop.
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