-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic11.py
More file actions
60 lines (41 loc) 路 984 Bytes
/
Copy pathtopic11.py
File metadata and controls
60 lines (41 loc) 路 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Dependency Injection
# . What is Depends()
# . Reusable logic
# . Auth example intro
# . Interview Questions
from fastapi import FastAPI, Depends, Header, HTTPException
app = FastAPI()
# def common_logic():
# return{
# "message":"Common Logic Executed"
# }
# @app.get("/home")
# def home(data=Depends(common_logic)):
# return data
# Reusable Logic -
# def get_current_user():
# return{
# "user":"Anupam Sharma"
# }
# @app.get("/profile")
# def profile(user = Depends(get_current_user)):
# return user
# @app.get("/dashboard")
# def dashboard(user = Depends(get_current_user)):
# return user
# Auth example intro-
def verify_token(token: str = Header(None)):
if token != "mysecrettoken":
raise HTTPException(
status_code=401,
detail = "Unauthorized access denied"
)
return {
"user":"Authorized User"
}
@app.get("/secure_data")
def secure_data(user = Depends(verify_token)):
return{
"message": "Secure Data Accessed",
"user":user
}