-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (44 loc) · 1.33 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class UserData(BaseModel):
id: int
email: str
first_name: str
last_name: str
avatar: str
class SupportData(BaseModel):
url: str
text: str
class ResponseModel(BaseModel):
data: UserData
support: SupportData
@app.get("/api/users/{user_id}", response_model=ResponseModel)
def get_user(user_id: int):
# Mock data for demonstration purposes
users = {
2: {
"id": 2,
"email": "[email protected]",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg",
}
}
support_info = {
"url": "https://contentcaddy.io?utm_source=reqres&utm_medium=json&utm_campaign=referral",
"text": "Tired of writing endless social media content? Let Content Caddy generate it for you.",
}
user = users.get(user_id)
if not user:
# todo improve exception
raise HTTPException(status_code=404, detail="User not found")
return {
"data": user,
"support": support_info,
}
# To run this app, use the following command in your terminal:
# uvicorn filename:app --reload
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)