Skip to content

๐Ÿง ๐Ÿ”—

๐Ÿ”— ๐Ÿ”—

๐ŸŒ ๐Ÿ”— ๐Ÿ‘ฅ โœ”๏ธ ๐Ÿ‘€ ๐Ÿ”ง ๐Ÿ”ข โš–๏ธ ๐ŸŽ“.

โœ‹๏ธ ๐Ÿ“ค ๐Ÿ’ช ๐Ÿ’ผ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’š ๐Ÿ’ช โš’ ๐Ÿ”ข ๐Ÿ”› ๐Ÿ”—, ๐Ÿต โœ”๏ธ ๐Ÿ“ฃ ๐Ÿ“š ๐ŸŽ ๐Ÿ”ข โš–๏ธ ๐ŸŽ“.

โžก๏ธ ๐ŸŒˆ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿ’š โœ”๏ธ ๐Ÿ”— ๐Ÿ‘ˆ โœ… ๐Ÿšฅ ๐Ÿ”ข ๐Ÿ”ข 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

๐ŸŒ ๐Ÿ‘‰ ๐Ÿ’ช ๐Ÿ˜‘ ๐ŸŽญ. & โšซ๏ธ ๐Ÿ’ช ๐Ÿšซ ๐Ÿ“ถ ๐Ÿ†‘ โ” โšซ๏ธ โš .

๐Ÿ‘ซ ๐Ÿ–ผ ๐Ÿ˜ซ ๐Ÿ™…, โœ‹๏ธ ๐ŸŽฆ โ” โšซ๏ธ ๐ŸŒ ๐Ÿ‘ท.

๐Ÿ“ƒ ๐Ÿ”ƒ ๐Ÿ’‚โ€โ™‚, ๐Ÿ“ค ๐Ÿš™ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ› ๏ธ ๐Ÿ‘‰ ๐ŸŽ ๐ŸŒŒ.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿค” ๐ŸŒ ๐Ÿ‘‰, ๐Ÿ‘† โช ๐Ÿ’ญ โ” ๐Ÿ‘ˆ ๐Ÿš™ ๐Ÿงฐ ๐Ÿ’‚โ€โ™‚ ๐Ÿ‘ท ๐Ÿ”˜.