Skip to content

๐Ÿ”— โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ

๐Ÿ’ผ ๐Ÿ‘† ๐Ÿšซ ๐Ÿค™ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ’ฒ ๐Ÿ”— ๐Ÿ”˜ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.

โš–๏ธ ๐Ÿ”— ๐Ÿšซ ๐Ÿ“จ ๐Ÿ’ฒ.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช โšซ๏ธ ๐Ÿ› ๏ธ/โŽ.

๐Ÿ“š ๐Ÿ’ผ, โ†ฉ๏ธ ๐Ÿ“ฃ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”ข โฎ๏ธ Depends, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ list dependencies โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ.

๐Ÿšฎ dependencies โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ

โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ“จ ๐Ÿ“ฆ โŒ dependencies.

โšซ๏ธ ๐Ÿ”œ list Depends():

from fastapi import Depends, FastAPI, Header, HTTPException

app = FastAPI()


async def verify_token(x_token: str = Header()):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def verify_key(x_key: str = Header()):
    if x_key != "fake-super-secret-key":
        raise HTTPException(status_code=400, detail="X-Key header invalid")
    return x_key


@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]

๐Ÿ‘‰ ๐Ÿ”— ๐Ÿ”œ ๐Ÿ› ๏ธ/โŽ ๐ŸŽ ๐ŸŒŒ ๐Ÿ˜ ๐Ÿ”—. โœ‹๏ธ ๐Ÿ‘ซ ๐Ÿ’ฒ (๐Ÿšฅ ๐Ÿ‘ซ ๐Ÿ“จ ๐Ÿ™†) ๐Ÿ† ๐Ÿšซ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.

Tip

๐Ÿ‘จโ€๐ŸŽจ โœ… โ™ป ๐Ÿ”ข ๐Ÿ”ข, & ๐ŸŽฆ ๐Ÿ‘ซ โŒ.

โš™๏ธ ๐Ÿ‘‰ dependencies โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ’ญ ๐Ÿ‘ซ ๐Ÿ› ๏ธ โช โŽ ๐Ÿ‘จโ€๐ŸŽจ/๐Ÿญ โŒ.

โšซ๏ธ ๐Ÿ’ช โ„น โŽ ๐Ÿ˜จ ๐Ÿ†• ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘ˆ ๐Ÿ‘€ โ™ป ๐Ÿ”ข ๐Ÿ‘† ๐Ÿ“Ÿ & ๐Ÿ’ช ๐Ÿ’ญ โšซ๏ธ ๐Ÿ™ƒ.

Info

๐Ÿ‘‰ ๐Ÿ–ผ ๐Ÿ‘ฅ โš™๏ธ ๐Ÿ’ญ ๐Ÿ›ƒ ๐ŸŽš X-Key & X-Token.

โœ‹๏ธ ๐ŸŽฐ ๐Ÿ’ผ, ๐Ÿ•โ” ๐Ÿ› ๏ธ ๐Ÿ’‚โ€โ™‚, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿคš ๐ŸŒ– ๐Ÿ’ฐ โšช๏ธโžก๏ธ โš™๏ธ ๐Ÿ› ๏ธ ๐Ÿ’‚โ€โ™‚ ๐Ÿš™ (โญ ๐Ÿ“ƒ).

๐Ÿ”— โŒ & ๐Ÿ“จ ๐Ÿ’ฒ

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿ”— ๐Ÿ”ข ๐Ÿ‘† โš™๏ธ ๐Ÿ›Ž.

๐Ÿ”— ๐Ÿ“„

๐Ÿ‘ซ ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ“จ ๐Ÿ“„ (๐Ÿ’– ๐ŸŽš) โš–๏ธ ๐ŸŽ ๐ŸŽง-๐Ÿ”—:

from fastapi import Depends, FastAPI, Header, HTTPException

app = FastAPI()


async def verify_token(x_token: str = Header()):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def verify_key(x_key: str = Header()):
    if x_key != "fake-super-secret-key":
        raise HTTPException(status_code=400, detail="X-Key header invalid")
    return x_key


@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]

๐Ÿคš โš 

๐Ÿ‘ซ ๐Ÿ”— ๐Ÿ’ช raise โš , ๐ŸŽ ๐Ÿ˜ ๐Ÿ”—:

from fastapi import Depends, FastAPI, Header, HTTPException

app = FastAPI()


async def verify_token(x_token: str = Header()):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def verify_key(x_key: str = Header()):
    if x_key != "fake-super-secret-key":
        raise HTTPException(status_code=400, detail="X-Key header invalid")
    return x_key


@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]

๐Ÿ“จ ๐Ÿ’ฒ

& ๐Ÿ‘ซ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ’ฒ โš–๏ธ ๐Ÿšซ, ๐Ÿ’ฒ ๐Ÿ† ๐Ÿšซ โš™๏ธ.

, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿค-โš™๏ธ ๐Ÿ˜ ๐Ÿ”— (๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿ’ฒ) ๐Ÿ‘† โช โš™๏ธ ๐Ÿ‘ฑ ๐Ÿ™†, & โœ‹๏ธ ๐Ÿ’ฒ ๐Ÿ† ๐Ÿšซ โš™๏ธ, ๐Ÿ”— ๐Ÿ”œ ๐Ÿ› ๏ธ:

from fastapi import Depends, FastAPI, Header, HTTPException

app = FastAPI()


async def verify_token(x_token: str = Header()):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def verify_key(x_key: str = Header()):
    if x_key != "fake-super-secret-key":
        raise HTTPException(status_code=400, detail="X-Key header invalid")
    return x_key


@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]

๐Ÿ”— ๐Ÿ‘ช โžก ๐Ÿ› ๏ธ

โช, ๐Ÿ•โ” ๐Ÿ‘‚ ๐Ÿ”ƒ โ” ๐Ÿ“Š ๐Ÿฆ ๐Ÿˆธ (๐Ÿฆ ๐Ÿˆธ - ๐Ÿ’— ๐Ÿ“), ๐ŸŽฒ โฎ๏ธ ๐Ÿ’— ๐Ÿ“, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ก โ” ๐Ÿ“ฃ ๐Ÿ‘ dependencies ๐Ÿ”ข ๐Ÿ‘ช โžก ๐Ÿ› ๏ธ.

๐ŸŒ ๐Ÿ”—

โญ ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ‘€ โ” ๐Ÿšฎ ๐Ÿ”— ๐ŸŽ‚ FastAPI ๐Ÿˆธ, ๐Ÿ‘ˆ ๐Ÿ‘ซ โœ” ๐Ÿ”  โžก ๐Ÿ› ๏ธ.