-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
225 lines (174 loc) · 5.48 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
from enum import Enum
from datetime import datetime
from fastapi.responses import JSONResponse
from fastapi.requests import Request
from typing import Union
app = FastAPI()
@app.get("/")
def hello_programmers():
return {"status": 200, "message": "Hello Programmers"}
@app.get("/users")
def hello_programmers(first_name: str, last_name: str):
return {
"status": 200,
"message": "Hello Programmers",
"user": {"first_name": first_name, "last_name": last_name},
}
@app.get("/default-users")
def hello_programmers(
first_name: str = "Default first name",
last_name: str = "Default last name",
age: int = 0,
city: str = "Bangalore",
):
return {
"status": 200,
"message": "Hello Programmers",
"user": {
"first_name": first_name,
"last_name": last_name,
"age": age,
"city": city,
},
}
@app.get("/validate")
def validate_integer_numbers(number: str):
try:
number = float(number) if "." in number else int(number)
return {"message": f"{number} is a valid number of {type(number)}"}
except Exception as error:
raise HTTPException(
status_code=400,
detail=f"{number} is an invalid number of {type(number)} -> {error}",
)
class User(BaseModel):
first_name: str
last_name: str
full_name: str = ""
username: str
age: int
city: str
@app.post("/users/create/")
def create_user(user: User):
user.full_name = f"{user.first_name} {user.last_name}"
return user
class State(BaseModel):
name: str
blocks: int
pincode: int
class City(BaseModel):
name: str
state: State
population: int
pincode: int
@app.post("/cities/create")
def create_city(city: City):
return city
class Fruit(BaseModel):
name: str
colour: str
class Tree(BaseModel):
name: str
branches: int
leaves: int
fruits: List[Fruit]
@app.post("/trees/create")
def create_tree(tree: Tree):
return tree
class FruitName(str, Enum):
default = "fruit"
favourite = "favourite fruit"
healthy = "healthy fruit"
@app.post("/fruits/create")
def create_fruit(name: str, nickname: FruitName):
return {"fruit": {"name": name, "nickname": nickname}}
class PlatformType(str, Enum):
search_engine = "Search Engine"
social_media = "Social Media"
class PlatformInfo(BaseModel):
name: str
type: PlatformType
nickname: str
class Platform(Enum):
instagram = PlatformInfo(
name="Instagram",
type=PlatformType.social_media,
nickname=f"Instagram {PlatformType.social_media.value}",
)
facebook = PlatformInfo(
name="Facebook",
type=PlatformType.social_media,
nickname=f"Facebook {PlatformType.social_media.value}",
)
google = PlatformInfo(
name="Google",
type=PlatformType.search_engine,
nickname=f"Google {PlatformType.search_engine.value}",
)
class PlatformRequest(BaseModel):
platform: str
@app.post("/platforms/create")
def create_platform(platform_request: PlatformRequest):
try:
platform = Platform[platform_request.platform].value.dict()
except KeyError as error:
# Order is status_code, detail -> if you do not specify kwarg names then using
# `raise HTTPException(f'Invalid key {platform_request.platform}', status_code=400)`
# will throw
# `TypeError: HTTPException.__init__() got multiple values for argument 'status_code'`
raise HTTPException(
detail=f"Invalid key {platform_request.platform}", status_code=400
)
return {"platform": platform}
# Install custom exceptions
class CustomFastAPIHttpException(HTTPException):
def __init__(self, status_code: str, detail: str, custom_message: str):
"""
In case we skip -> `super().__init__(status_code=status_code, detail=detail)`
We will see:
`AttributeError: 'CustomFastAPIHttpException' object has no attribute 'status_code'`
"""
super().__init__(status_code=status_code, detail=detail)
self.custom_message = custom_message
@app.exception_handler(CustomFastAPIHttpException)
async def custom_exception_handler(
request: Request, exception: CustomFastAPIHttpException
):
return JSONResponse(
status_code=exception.status_code,
content={
"message": "Something went wrong!",
"custom_message": exception.custom_message,
"detail": exception.detail,
},
)
@app.get("/plans/")
async def get_plans(count: int):
try:
assert count != 0, "Plans count should be > 0"
return {
"plans": [
{
"name": f"Plan {plan_no}",
"created_at": datetime.now(),
"updated_at": datetime.now(),
}
for plan_no in range(1, count + 1)
]
}
except Exception as error:
raise CustomFastAPIHttpException(
status_code=400,
detail=f"There is some issue while processing the request -> {error}",
custom_message="Please check, this might be due to wrong input or code BUG.",
)
class Item(BaseModel):
name: str
nickname: Union[str, None] = None
price: Union[int, float] = 0
@app.post("/items/")
async def create_item(item: Item):
return {"item": item}