Код: Выделить всё
from typing import Annotated
from fastapi import Depends, FastAPI
app = FastAPI()
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: Annotated[bool, Depends(checker)]):
return {"fixed_content_in_query": fixed_content_included}
Код: Выделить всё
def config_checker(value):
checker = FixedContentQueryChecker(value)
def f(func):
@functools.wraps(func)
async def wrap_func(*args, fixed_content_included: Annotated[bool, Depends(checker)], **kwargs):
return await func(*args, fixed_content_included, **kwargs)
return wrap_func
return f
@app.get("/query-bar-checker/")
@config_checker(value="bar")
async def read_query_check_bar(fixed_content_included: Annotated[bool, Depends(??)]):
return {"fixed_content_in_query": fixed_content_included}
@app.get("/query-foo-checker/")
@config_checker(value="foo")
async def read_query_check_foo(fixed_content_included: Annotated[bool, Depends(??)]):
return {"fixed_content_in_query": fixed_content_included}
Но если я укажите что-либо в функции Depends() в определении маршрута, декоратор не сможет переопределить его, чтобы можно было использовать параметризованную функцию.
Как мне действовать?
Подробнее здесь: https://stackoverflow.com/questions/793 ... pendencies
Мобильная версия