-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
516 lines (403 loc) · 13 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#Python
from uuid import UUID
from datetime import date, datetime
from typing import Optional, List
import json
#Pydantic
from pydantic import EmailStr
from pydantic import BaseModel
from pydantic import Field
#FastAPI
from fastapi import FastAPI, status, Body
from pydantic.types import SecretStr
from fastapi import HTTPException
from fastapi.param_functions import Form, Path
app = FastAPI()
#Models
class UserBase(BaseModel):
user_id: UUID = Field(...)
email: EmailStr = Field (...)
class Passwords(BaseModel):
password: str = Field (..., min_length=8, max_length=64)
class User(UserBase):
first_name: str = Field (
...,
min_length= 1,
max_length=50
)
last_name: str = Field (
...,
min_length= 1,
max_length=50
)
birth_date: Optional[date] = Field(default=None)
class tweet(BaseModel):
tweet_id: UUID = Field (...)
content: str = Field (
...,
min_length=1,
max_length=256
)
created_at : datetime = Field(default=datetime.now())
updated_at : Optional[datetime] = Field(default=None)
by: User = Field(...)
class UserLogin(UserBase, Passwords):
pass
class UserRegister(User, Passwords):
pass
class LoginOut(BaseModel):
email: EmailStr = Field(...)
message: str = Field(default=None)
class DeleteUser(BaseModel):
user_id: UUID = Field (...)
message: str = Field (default= "User Deleted")
#Path Operations
## Users
### Register a user
@app.post(
path="/signup",
response_model= User,
status_code= status.HTTP_201_CREATED,
summary= "Register a User",
tags= ["Users"]
)
def signup(
user: UserRegister = Body(...)
):
"""
Signup
This path operation register a user in the app
Parameters:
- Request body parameter:
- user: UserRegister
Returns a JSON with basic user's information:
- user_id: UUID
- email: Emailstr
- first_name: str
- last_name: str
- birth_date: str
"""
with open ("users.json", "r+", encoding="utf-8") as f:
results:list = json.loads(f.read())
user_dict = user.dict()
user_dict["user_id"] = str(user_dict["user_id"])
user_dict["birth_date"] = str(user_dict["birth_date"])
results.append(user_dict)
f.seek(0)
f.write(json.dumps(results))
return user
### Login a user
@app.post(
path="/login",
response_model= LoginOut,
status_code= status.HTTP_200_OK,
summary= "Login a User",
tags= ["Users"]
)
def login(
email: EmailStr = Form(...),
password: str = Form(...)
):
"""
Login a user
This path Operations login a user in the app based on users.json file wich contains user's data,
it also verifies if the information is the same to do a login succesfully
Parameters:
- Request Body parameter:
- email : EmailStr
- password: str
Returns a message "Done!" if the authentication is ok, or returns "Unsuccesfully authentication!" if it has no coincidence, both cases returns the user's email as a response
"""
with open("users.json", "r+", encoding="utf-8") as f:
datos = list(json.loads(f.read()))
for user in datos:
if email == user["email"] and password == user["password"]:
return LoginOut(email=email, message="Done!")
return LoginOut(email=email, message= "Unsuccesfully authentication!")
### Show all user
@app.get(
path="/users",
response_model= List[User],
status_code= status.HTTP_200_OK,
summary= "Show all Users",
tags= ["Users"]
)
def show_all_users():
"""
Show all users
This path operation shows all users registered in the app
Parameters:
- None
Returns a json list with all users in the app, with the following keys
- user_id: UUID
- email: Emailstr
- first_name: str
- last_name: str
- birth_date: str
"""
with open ("users.json", "r", encoding="utf-8") as f:
results = json.loads(f.read())
return results
### Show a user
@app.get(
path="/users/{user_id}",
response_model= User,
status_code= status.HTTP_200_OK,
summary= "Show a User",
tags= ["Users"]
)
def show_a_user(
user_id: str = Path (
...,
min_length=1,
example= "3fa85f64-5717-4562-b3fc-2c963f66afa6"
)
):
"""
Show a user by ID
This path operation shows a user by ID
Parameters:
- Request body parameter:
- **user_id**: str
Returns a json with the user's information: first_name, last_name, email, user_id, date_of_birth
"""
with open ("users.json", "r", encoding="utf-8") as f:
results = list(json.loads(f.read()))
for user in results:
if user_id == user["user_id"]:
user = dict(user)
return user
if user_id != user["user_id"]:
raise HTTPException(
status_code= status.HTTP_404_NOT_FOUND,
detail= "This user does not exist!")
### Delete a user
@app.delete(
path="/users/{user_id}/delete",
response_model= DeleteUser,
status_code= status.HTTP_202_ACCEPTED,
summary= "Delete a User",
tags= ["Users"]
)
def delete_a_user(
user_id: str = Path(
...
)
):
"""
Delete a user
This path operation deletes a user
Parameters:
- Request Body Parameters:
- user_id: str
Returns a json saying the user provided was succesfully deleted, if the user does not exists, the json will show it up to you
"""
with open ("users.json", "r+", encoding="utf-8") as f:
results = list(json.loads(f.read()))
for user in results:
if user["user_id"] == user_id:
results.remove(user)
with open("users.json", "w", encoding="utf-8") as f:
f.seek(0)
f.write(json.dumps(results))
return user
if 1==1:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="¡This user_id doesn't exist!"
)
### Update a user
@app.put(
path="/users/{user_id}/update",
response_model= User,
status_code= status.HTTP_200_OK,
summary= "Update a User",
tags= ["Users"]
)
def update_a_user(
user_id: UUID = Path(...),
user: User = Body (...)
):
"""
Update a User
This path operation updates user's information, even the user ID
Parameters:
- Response Body parameters:
- user_id: UUID -> user's id
- user: User -> User object wich contains first_name, last_name, date_of_birth, used_id and email
Returns a Json with User Json information with the new data given, sometimes its necesary do the execute twice to work
"""
user_id = str(user_id)
user_dict = user.dict()
user_dict["user_id"] = str(user_dict["user_id"])
user_dict["birth_date"] = str(user_dict["birth_date"])
with open("users.json", "r+", encoding="utf-8") as f:
results = list(json.loads(f.read()))
for user in results:
if user["user_id"] == user_id:
results[results.index(user)] = user_dict
with open("users.json", "w", encoding="utf-8") as f:
f.seek(0)
f.write(json.dumps(results))
return user
## Tweets
### Show all tweets
@app.get(
path="/",
response_model= List[tweet],
status_code= status.HTTP_200_OK,
summary= "Show all tweets",
tags= ["Tweets"]
)
def home():
"""
Show all tweets
This path operation shows all tweets posted in the app
Parameters:
- Request Body parameter:
- tweet: tweet
Returns a json file with all tweets in the app, with following keys
- tweet_id: UUID
- content: str
- created_at : datetime
- updated_at : Optional[datetime]
- by: User
"""
with open ("tweets.json", "r", encoding="utf-8") as f:
results = json.loads(f.read())
return results
### Post a Tweet
@app.post(
path="/post",
response_model= tweet,
status_code= status.HTTP_201_CREATED,
summary= "Post a Tweet",
tags= ["Tweets"]
)
def post_a_tweet(tweet: tweet = Body(...)):
"""
Post a Tweet
This path operation post a tweet in the app
Parameters:
- Request body parameter:
- tweet: Tweet
Returns a JSON with basic tweet information:
- tweet_id: UUID
- content: str
- created_at : datetime
- updated_at : Optional[datetime]
- by: User
"""
with open ("tweets.json", "r+", encoding="utf-8") as f:
results:list = json.loads(f.read())
tweet_dict = tweet.dict()
tweet_dict["tweet_id"] = str(tweet_dict["tweet_id"])
tweet_dict["created_at"] = str(tweet_dict["created_at"])
tweet_dict["updated_at"] = str(tweet_dict["updated_at"])
tweet_dict["by"]["user_id"] = str (tweet_dict["by"]["user_id"])
tweet_dict["by"]["birth_date"] = str (tweet_dict["by"]["birth_date"])
results.append(tweet_dict)
f.seek(0)
f.write(json.dumps(results))
return tweet
### Show a Tweet
@app.get(
path="/tweets/{tweet_id}",
response_model= tweet,
status_code= status.HTTP_200_OK,
summary= "Show a Tweet",
tags= ["Tweets"]
)
def show_a_tweet(tweet_id:str=Path(...)):
"""
Show a tweet by ID
This path operation shows a tweet by ID
Parameters:
- Request body parameter:
- **user_id**: str
Returns a json with the user's information: first_name, last_name, email, user_id, date_of_birth
"""
with open ("tweets.json", "r+", encoding="utf-8") as f:
# tweet_dict = dict(tweet_id)
# tweet_dict["tweet_id"] = str(tweet_dict["tweet_id"])
# tweet_dict["created_at"] = str(tweet_dict["created_at"])
# tweet_dict["updated_at"] = str(tweet_dict["updated_at"])
# tweet_dict["by"]["user_id"] = str (tweet_dict["by"]["user_id"])
# tweet_dict["by"]["birth_date"] = str (tweet_dict["by"]["birth_date"])
results = list(json.loads(f.read()))
for tw in results:
if tw["tweet_id"] == tweet_id:
tw = dict(tw)
return tw
if tw["tweet_id"] != tweet_id:
raise HTTPException(
status_code= status.HTTP_404_NOT_FOUND,
detail= "This tweet does not exist!")
### Delete a tweet
@app.delete(
path="/tweets/{tweet_id}/delete",
response_model= tweet,
status_code= status.HTTP_200_OK,
summary= "Delete a Tweet",
tags= ["Tweets"]
)
def delete_a_tweet(tweet_id:str=Path(...)):
"""
Delete a tweet
This path operation deletes a tweet
Parameters:
- Request Body Parameters:
- tweet_id: str
Returns a json saying the tweet provided was succesfully deleted, if the user does not exists, the json will show it up to you
"""
with open ("tweets.json", "r+", encoding="utf-8") as f:
results = list(json.loads(f.read()))
for tweet in results:
if tweet["tweet_id"] == tweet_id:
results.remove(tweet)
with open("tweets.json", "w", encoding="utf-8") as f:
f.seek(0)
f.write(json.dumps(results))
return tweet
if 1==1:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="¡This tweet doesn't exist!"
)
### Update a tweet
@app.put(
path="/tweets/{tweet_id}/update",
response_model= tweet,
status_code= status.HTTP_200_OK,
summary= "Update a Tweet",
tags= ["Tweets"]
)
def Update_a_tweet(
tweet_id:str=Path(...),
tweet:tweet=Body(...)
):
"""
Update a Tweet
This path operation updates tweet's information, even the user ID
Parameters:
- Response Body parameters:
- tweet_id: str -> Tweet's ID
- tweet: tweet -> tweet object wich contains first_name, last_name, date_of_birth, used_id and email
Returns a Json with User Json information with the new data given, sometimes its necesary do the execute twice to work
"""
tweet_id = str(tweet_id)
tweet_dict = tweet.dict()
tweet_dict["tweet_id"] = str(tweet_dict["tweet_id"])
tweet_dict["created_at"] = str(tweet_dict["created_at"])
tweet_dict["updated_at"] = str(tweet_dict["updated_at"])
tweet_dict["by"]["user_id"] = str (tweet_dict["by"]["user_id"])
tweet_dict["by"]["birth_date"] = str (tweet_dict["by"]["birth_date"])
with open("tweets.json", "r+", encoding="utf-8") as f:
results = list(json.loads(f.read()))
for tweet in results:
if tweet["tweet_id"] == tweet_id:
results[results.index(tweet)] = tweet_dict
with open("tweets.json", "w", encoding="utf-8") as f:
f.seek(0)
f.write(json.dumps(results))
return tweet