๐ง ๐¶
๐ ๐¶
๐ ๐ ๐ฅ โ๏ธ ๐ ๐ง ๐ข โ๏ธ ๐.
โ๏ธ ๐ค ๐ช ๐ผ ๐โ ๐ ๐ ๐ช โ ๐ข ๐ ๐, ๐ต โ๏ธ ๐ฃ ๐ ๐ ๐ข โ๏ธ ๐.
โก๏ธ ๐ ๐ ๐ฅ ๐ โ๏ธ ๐ ๐ โ
๐ฅ ๐ข ๐ข q
๐ ๐ง ๐.
โ๏ธ ๐ฅ ๐ ๐ช ๐ ๐ ๐ง ๐.
"๐ง๐ฒ" ๐¶
๐ ๐ค ๐ โ ๐ ๐ "๐ง๐ฒ".
๐ซ ๐ โซ๏ธ (โ โช ๐ง๐ฒ), โ๏ธ ๐ ๐ ๐.
๐, ๐ฅ ๐ฃ ๐ฉโ๐ฌ __call__
:
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: bool = Depends(checker)):
return {"fixed_content_in_query": fixed_content_included}
๐ ๐ผ, ๐ __call__
โซ๏ธโ FastAPI ๐ โ๏ธ โ
๐ ๐ข & ๐ง-๐, & ๐ โซ๏ธโ ๐ ๐ค ๐ถโโ๏ธ ๐ฒ ๐ข ๐ โก ๐ ๏ธ ๐ข โช.
๐ ๐¶
& ๐, ๐ฅ ๐ช โ๏ธ __init__
๐ฃ ๐ข ๐ ๐ ๐ฅ ๐ช โ๏ธ "๐" ๐:
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: bool = Depends(checker)):
return {"fixed_content_in_query": fixed_content_included}
๐ ๐ผ, FastAPI ๐ ๐ซ โฑ ๐ โ๏ธ ๐
๐ __init__
, ๐ฅ ๐ โ๏ธ โซ๏ธ ๐ ๐ ๐.
โ ๐¶
๐ฅ ๐ช โ ๐ ๐ ๐ โฎ๏ธ:
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: bool = Depends(checker)):
return {"fixed_content_in_query": fixed_content_included}
& ๐ ๐ ๐ฅ ๐ช "๐" ๐ ๐, ๐ ๐ โ๏ธ "bar"
๐ โซ๏ธ, ๐ข checker.fixed_content
.
โ๏ธ ๐ ๐¶
โคด๏ธ, ๐ฅ ๐ช โ๏ธ ๐ checker
Depends(checker)
, โฉ๏ธ Depends(FixedContentQueryChecker)
, โฉ๏ธ ๐ ๐, checker
, ๐ซ ๐ โซ๏ธ.
& ๐โ โ ๐, FastAPI ๐ ๐ค ๐ checker
๐:
checker(q="somequery")
...& ๐ถโโ๏ธ โซ๏ธโ ๐ ๐จ ๐ฒ ๐ ๐ โก ๐ ๏ธ ๐ข ๐ข fixed_content_included
:
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: bool = Depends(checker)):
return {"fixed_content_in_query": fixed_content_included}
Tip
๐ ๐ ๐ช ๐ ๐ญ. & โซ๏ธ ๐ช ๐ซ ๐ถ ๐ โ โซ๏ธ โ .
๐ซ ๐ผ ๐ซ ๐ , โ๏ธ ๐ฆ โ โซ๏ธ ๐ ๐ท.
๐ ๐ ๐โโ, ๐ค ๐ ๐ข ๐ ๐ ๏ธ ๐ ๐ ๐.
๐ฅ ๐ ๐ค ๐ ๐, ๐ โช ๐ญ โ ๐ ๐ ๐งฐ ๐โโ ๐ท ๐.