Skip to content

๐ŸŒ ๐Ÿ”—

๐Ÿ†Ž ๐Ÿˆธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿšฎ ๐Ÿ”— ๐ŸŽ‚ ๐Ÿˆธ.

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ dependencies โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ‘ซ FastAPI ๐Ÿˆธ.

๐Ÿ‘ˆ ๐Ÿ’ผ, ๐Ÿ‘ซ ๐Ÿ”œ โœ” ๐ŸŒ โžก ๐Ÿ› ๏ธ ๐Ÿˆธ:

from fastapi import Depends, FastAPI, Header, HTTPException


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 = FastAPI(dependencies=[Depends(verify_token), Depends(verify_key)])


@app.get("/items/")
async def read_items():
    return [{"item": "Portal Gun"}, {"item": "Plumbus"}]


@app.get("/users/")
async def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]

& ๐ŸŒ ๐Ÿ’ญ ๐Ÿ“„ ๐Ÿ”ƒ โŽ dependencies โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ โœ”, โœ‹๏ธ ๐Ÿ‘‰ ๐Ÿ’ผ, ๐ŸŒ โžก ๐Ÿ› ๏ธ ๐Ÿ“ฑ.

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

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