|
1 | | -from fastapi import FastAPI, HTTPException, Security |
| 1 | +from fastapi import FastAPI, Security |
2 | 2 | from fastapi.middleware.cors import CORSMiddleware |
3 | | -from database.query import query_get, query_put, query_update |
4 | 3 | from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer |
5 | 4 | from fastapi.responses import JSONResponse |
6 | 5 | from fastapi.encoders import jsonable_encoder |
7 | | -from user import Auth, SignInRequestModel, SignUpRequestModel, UserAuthResponseModel, UserUpdateRequestModel, UserResponseModel, register_user, signin_user, update_user, get_all_users, get_user_by_id |
| 6 | +from user import ( |
| 7 | + Auth, |
| 8 | + SignInRequestModel, |
| 9 | + SignUpRequestModel, |
| 10 | + UserAuthResponseModel, |
| 11 | + UserUpdateRequestModel, |
| 12 | + UserResponseModel, |
| 13 | + register_user, |
| 14 | + signin_user, |
| 15 | + update_user, |
| 16 | + get_all_users, |
| 17 | + get_user_by_id, |
| 18 | +) |
8 | 19 |
|
9 | 20 | app = FastAPI() |
10 | 21 |
|
|
14 | 25 | "http://localhost:3000", |
15 | 26 | "http://localhost:3001", |
16 | 27 | "http://localhost:4000", |
17 | | - "http://localhost:19006" |
| 28 | + "http://localhost:19006", |
18 | 29 | ] |
19 | 30 | app.add_middleware( |
20 | 31 | CORSMiddleware, |
|
28 | 39 | auth_handler = Auth() |
29 | 40 |
|
30 | 41 |
|
| 42 | +""" |
31 | 43 | ############################### |
32 | 44 | ########## Auth APIs ########## |
33 | 45 | ############################### |
| 46 | +""" |
| 47 | + |
34 | 48 |
|
35 | | -@app.post('/v1/signup', response_model=UserAuthResponseModel) |
| 49 | +@app.post("/v1/signup", response_model=UserAuthResponseModel) |
36 | 50 | def signup_api(user_details: SignUpRequestModel): |
37 | 51 | """ |
38 | 52 | This sign-up API allow you to register your account, and return access token. |
39 | 53 | """ |
40 | 54 | user = register_user(user_details) |
41 | 55 | access_token = auth_handler.encode_token(user_details.email) |
42 | 56 | refresh_token = auth_handler.encode_refresh_token(user_details.email) |
43 | | - return JSONResponse(status_code=200, content={'token': {'access_token': access_token, 'refresh_token': refresh_token}, 'user': user}) |
| 57 | + return JSONResponse( |
| 58 | + status_code=200, |
| 59 | + content={ |
| 60 | + "token": {"access_token": access_token, "refresh_token": refresh_token}, |
| 61 | + "user": user, |
| 62 | + }, |
| 63 | + ) |
44 | 64 |
|
45 | 65 |
|
46 | | -@app.post('/v1/signin', response_model=UserAuthResponseModel) |
| 66 | +@app.post("/v1/signin", response_model=UserAuthResponseModel) |
47 | 67 | def signin_api(user_details: SignInRequestModel): |
48 | 68 | """ |
49 | 69 | This sign-in API allow you to obtain your access token. |
50 | 70 | """ |
51 | 71 | user = signin_user(user_details.email, user_details.password) |
52 | | - access_token = auth_handler.encode_token(user['email']) |
53 | | - refresh_token = auth_handler.encode_refresh_token(user['email']) |
54 | | - return JSONResponse(status_code=200, content={'token': {'access_token': access_token, 'refresh_token': refresh_token}, 'user': user}) |
55 | | - |
56 | | - |
57 | | -@app.get('/v1/refresh-token') |
| 72 | + access_token = auth_handler.encode_token(user["email"]) |
| 73 | + refresh_token = auth_handler.encode_refresh_token(user["email"]) |
| 74 | + return JSONResponse( |
| 75 | + status_code=200, |
| 76 | + content={ |
| 77 | + "token": {"access_token": access_token, "refresh_token": refresh_token}, |
| 78 | + "user": user, |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +@app.get("/v1/refresh-token") |
58 | 84 | def refresh_token_api(refresh_token: str): |
59 | 85 | """ |
60 | 86 | This refresh-token API allow you to obtain new access token. |
61 | 87 | """ |
62 | 88 | new_token = auth_handler.refresh_token(refresh_token) |
63 | | - return {'access_token': new_token} |
| 89 | + return {"access_token": new_token} |
64 | 90 |
|
65 | 91 |
|
| 92 | +""" |
66 | 93 | ################################ |
67 | 94 | ########## Users APIs ########## |
68 | 95 | ################################ |
| 96 | +""" |
| 97 | + |
69 | 98 |
|
70 | 99 | @app.get("/v1/users", response_model=list[UserResponseModel]) |
71 | 100 | def get_all_users_api(credentials: HTTPAuthorizationCredentials = Security(security)): |
72 | 101 | """ |
73 | 102 | This users get API allow you to fetch all user data. |
74 | 103 | """ |
75 | 104 | token = credentials.credentials |
76 | | - if (auth_handler.decode_token(token)): |
| 105 | + if auth_handler.decode_token(token): |
77 | 106 | user = get_all_users() |
78 | 107 | return JSONResponse(status_code=200, content=jsonable_encoder(user)) |
79 | | - return JSONResponse(status_code=401, content={'error': 'Faild to authorize'}) |
| 108 | + return JSONResponse(status_code=401, content={"error": "Faild to authorize"}) |
80 | 109 |
|
81 | 110 |
|
82 | 111 | @app.get("/v1/user/{user_id}", response_model=UserResponseModel) |
83 | | -def get_user_api(user_id: int, credentials: HTTPAuthorizationCredentials = Security(security)): |
| 112 | +def get_user_api( |
| 113 | + user_id: int, credentials: HTTPAuthorizationCredentials = Security(security) |
| 114 | +): |
84 | 115 | """ |
85 | 116 | This user API allow you to fetch specific user data. |
86 | 117 | """ |
87 | 118 | token = credentials.credentials |
88 | | - if (auth_handler.decode_token(token)): |
| 119 | + if auth_handler.decode_token(token): |
89 | 120 | user = get_user_by_id(user_id) |
90 | 121 | return JSONResponse(status_code=200, content=jsonable_encoder(user)) |
91 | | - return JSONResponse(status_code=401, content={'error': 'Faild to authorize'}) |
| 122 | + return JSONResponse(status_code=401, content={"error": "Faild to authorize"}) |
92 | 123 |
|
93 | 124 |
|
94 | 125 | @app.post("/v1/user/update", response_model=UserResponseModel) |
95 | | -def update_user_api(user_details: UserUpdateRequestModel, credentials: HTTPAuthorizationCredentials = Security(security)): |
| 126 | +def update_user_api( |
| 127 | + user_details: UserUpdateRequestModel, |
| 128 | + credentials: HTTPAuthorizationCredentials = Security(security), |
| 129 | +): |
96 | 130 | """ |
97 | 131 | This user update API allow you to update user data. |
98 | 132 | """ |
99 | 133 | token = credentials.credentials |
100 | | - if (auth_handler.decode_token(token)): |
| 134 | + if auth_handler.decode_token(token): |
101 | 135 | user = update_user(user_details) |
102 | 136 | return JSONResponse(status_code=200, content=jsonable_encoder(user)) |
103 | | - return JSONResponse(status_code=401, content={'error': 'Faild to authorize'}) |
| 137 | + return JSONResponse(status_code=401, content={"error": "Faild to authorize"}) |
104 | 138 |
|
105 | 139 |
|
| 140 | +""" |
106 | 141 | ############################### |
107 | 142 | ########## Test APIs ########## |
108 | 143 | ############################### |
| 144 | +""" |
| 145 | + |
109 | 146 |
|
110 | | -@app.get('/secret') |
| 147 | +@app.get("/secret") |
111 | 148 | def secret_data_api(credentials: HTTPAuthorizationCredentials = Security(security)): |
112 | 149 | """ |
113 | 150 | This secret API is just for testing. Need access token to access this API. |
114 | 151 | """ |
115 | 152 | token = credentials.credentials |
116 | | - if (auth_handler.decode_token(token)): |
117 | | - return 'Top Secret data only authorized users can access this info' |
| 153 | + if auth_handler.decode_token(token): |
| 154 | + return "Top Secret data only authorized users can access this info" |
118 | 155 |
|
119 | 156 |
|
120 | | -@app.get('/not-secret') |
| 157 | +@app.get("/not-secret") |
121 | 158 | def not_secret_data_api(): |
122 | 159 | """ |
123 | 160 | This not-secret API is just for testing. |
124 | 161 | """ |
125 | | - return 'Not secret data' |
| 162 | + return "Not secret data" |
0 commit comments