-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
140 lines (108 loc) · 4.59 KB
/
main.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import logging
import os
from datetime import datetime
from typing import Annotated
import jwt
from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request, Response, status # type: ignore
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse # type: ignore
from fastapi.security import OAuth2PasswordBearer # type: ignore
from fastapi_restful.timing import add_timing_middleware
import model
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
model.Base.metadata.create_all(bind=model.engine)
app = FastAPI()
add_timing_middleware(app, record=logger.debug, exclude="untimed")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
jwks_url = "https://www.googleapis.com/service_accounts/v1/jwk/[email protected]"
project_id = str(os.getenv("PROJECT_ID"))
jwk_client = jwt.PyJWKClient(jwks_url)
api = APIRouter(prefix="/api/v1", dependencies=[Depends(oauth2_scheme)])
def get_db():
db = model.SessionLocal()
try:
yield db
finally:
db.close()
async def get_current_user(
token: Annotated[str, Depends(oauth2_scheme)]) -> str:
try:
pub_key = jwk_client.get_signing_key_from_jwt(token).key
payload = jwt.decode(
token,
pub_key,
algorithms=["RS256"],
audience=project_id,
issuer="https://securetoken.google.com/" + project_id,
require=["iss", "aud", "sub", "exp", "iat", "auth_time"])
return payload["user_id"]
except Exception as error:
print("Error decoding token:", error)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
) from None
@api.put("/stool-log")
async def upsert_stool_log(user_id: Annotated[str,
Depends(get_current_user)],
log: model.StoolLogModel,
db: model.Session = Depends(get_db)):
model.upsert_stool_log(db, user_id, log)
@api.put("/food-log")
async def upsert_food_log(user_id: Annotated[str,
Depends(get_current_user)],
log: model.FoodLogModel,
db: model.Session = Depends(get_db)):
model.upsert_food_log(db, user_id, log)
@api.put("/symptom-log")
async def upsert_symptom_log(user_id: Annotated[str,
Depends(get_current_user)],
log: model.SymptomLogModel,
db: model.Session = Depends(get_db)):
model.upsert_symptom_log(db, user_id, log)
@api.get("/meal-list")
async def get_meal_list(
response: Response,
user_id: Annotated[str, Depends(get_current_user)],
search: str = "",
db: model.Session = Depends(get_db),
):
response.headers["Cache-Control"] = "max-age=86400" # 24 hours
return model.get_meal_list(db, search, user_id)
@api.put("/logs")
async def upsert_logs(user_id: Annotated[str, Depends(get_current_user)],
logs: model.LogEntriesModel,
db: model.Session = Depends(get_db)):
model.upsert_logs(db, user_id, logs)
@api.get("/logs")
async def get_logs(user_id: Annotated[str, Depends(get_current_user)],
db: model.Session = Depends(get_db),
since: datetime = datetime.min):
return model.get_logs(db, user_id, since)
@api.get("/ingredient-suggestions")
async def get_ingredient_suggestions(
response: Response,
user_id: Annotated[str, Depends(get_current_user)],
search: str = "",
limit: int = 10,
db: model.Session = Depends(get_db),
):
response.headers["Cache-Control"] = "max-age=86400" # 24 hours
return model.get_ingredient_suggestions(db, user_id, search, limit)
@app.get("/")
async def root():
return {"Hello": "World!"}
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request,
exc: RequestValidationError):
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
logging.error(f"{request}: {exc_str}")
content = {'status_code': 10422, 'message': exc_str, 'data': None}
return JSONResponse(content=content,
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
app.include_router(api)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))