FastAPI教程 高級依賴

2021-11-08 09:56 更新

參數(shù)化依賴

我們看到的所有依賴項都是一個固定的函數(shù)或類。

但是在某些情況下,您希望能夠在依賴項上設置參數(shù),而不必聲明許多不同的函數(shù)或類。

假設我們想要一個依賴項來檢查查詢參數(shù)是否q包含某些固定內(nèi)容。

但是我們希望能夠參數(shù)化該固定內(nèi)容。

“可調(diào)用”實例

在 Python 中,有一種方法可以使類的實例成為“可調(diào)用的”。

不是類本身(已經(jīng)是可調(diào)用的),而是該類的實例。

為此,我們聲明了一個方法__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將用于檢查附加參數(shù)和子依賴項的內(nèi)容,并且稍后將調(diào)用此內(nèi)容將值傳遞給您的路徑操作函數(shù)中的參數(shù)。

參數(shù)化實例

現(xiàn)在,我們可以使用__init__來聲明我們可以用來“參數(shù)化”依賴項的實例的參數(shù):

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}

在這種情況下,F(xiàn)astAPI永遠不會觸及或關心__init__,我們將直接在我們的代碼中使用它。

創(chuàng)建實例

我們可以使用以下命令創(chuàng)建此類的實例:

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}

這樣我們就可以“參數(shù)化”我們的依賴項,現(xiàn)在"bar"在它內(nèi)部,作為屬性checker.fixed_content。

使用實例作為依賴

然后,我們可以checker在Depends(checker), 而不是 中使用 this Depends(FixedContentQueryChecker),因為依賴項是實例checker,而不是類本身。

在解決依賴時,F(xiàn)astAPI會這樣調(diào)用checker:

checker(q="somequery")

...并將在我們的路徑操作函數(shù)中作為依賴值返回的任何內(nèi)容作為參數(shù)傳遞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}

提示

這一切似乎都是人為的??赡苓€不是很清楚它有什么用處。

這些示例故意簡單,但展示了它是如何工作的。

在有關安全性的章節(jié)中,有以相同方式實現(xiàn)的實用程序函數(shù)。

如果您理解了所有這些,您就已經(jīng)知道這些用于安全性的實用工具在底層是如何工作的。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號