वैश्विक Dependencies¶
🌐 एआई और मनुष्यों द्वारा किया गया अनुवाद
यह अनुवाद मनुष्यों के मार्गदर्शन में एआई द्वारा किया गया है। 🤝
इसमें मूल अर्थ को गलत समझने या अप्राकृतिक लगने आदि जैसी गलतियाँ हो सकती हैं। 🤖
आप हमें एआई LLM को बेहतर मार्गदर्शन करने में मदद करके इस अनुवाद को बेहतर बना सकते हैं।
कुछ प्रकार के applications के लिए आप पूरे application में dependencies जोड़ना चाह सकते हैं।
जिस तरह आप path operation decorators में dependencies जोड़ सकते हैं, उसी तरह आप उन्हें FastAPI application में भी जोड़ सकते हैं।
उस स्थिति में, वे application की सभी path operations पर लागू होंगी:
from typing import Annotated
from fastapi import Depends, FastAPI, Header, HTTPException
async def verify_token(x_token: Annotated[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: Annotated[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"}]
🤓 Other versions and variants
Tip
Prefer to use the Annotated version if possible.
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"}]
और path operation decorators में dependencies जोड़ने वाले section की सभी बातें अभी भी लागू होती हैं, लेकिन इस मामले में, app की सभी path operations पर।
path operations के समूहों के लिए Dependencies¶
बाद में, जब आप बड़े applications को संरचित करने के तरीके के बारे में पढ़ेंगे (बड़े Applications - कई Files), संभवतः कई files के साथ, तो आप सीखेंगे कि path operations के एक समूह के लिए एक ही dependencies parameter कैसे declare किया जाए।