-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_4.py
102 lines (75 loc) · 3.24 KB
/
main_4.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
import random
from fastapi import FastAPI, HTTPException, Response, status
from pydantic import BaseModel
class Post(BaseModel):
title: str
content: str
app = FastAPI()
# much better way to store posts with a unique id is a HASH MAP
my_posts = {
0 : {"title" : "title_0", "content":"content_0"},
1 : {"title" : "title_1", "content":"content_1"},
2 : {"title" : "title_2", "content":"content_2"},
3 : {"title" : "title_3", "content":"content_3"},
4 : {"title" : "title_4", "content":"content_4"},
5 : {"title" : "title_5", "content":"content_5"},
}
# ------------------------------- CHAPTER - 4 : THE CRUD FINALE :- DELETE ; UPDATE ------------------------------------------------------- #
# for deletinf a key value pair in dictionary :-
# del my_posts[id]
# my_posts.pop(id)
@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT )
def delete_post(id:int):
print(my_posts)
# trying to delete with a key that's not present gives => INTERNAL server error
if my_posts.__contains__(id):
my_posts.pop(id)
# del my_posts[id]
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"post with the id requested {id} is not present ; Invalid id ; " )
# for 204, we don't expect ANY return from the API to the client
# check this :-
# 1. get all posts
# 2. put a post
# with put, NOTE :- even if you wanna update only field, you'll need to pass in everything
@app.put("/posts/{id}")
def update_post(updated_post:Post, id: int):
if my_posts.__contains__(id):
my_posts[id] = updated_post.model_dump()
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"post with the id requested {id} is not present ; Invalid id ; " )
return {"data updated ! These are all the posts " : my_posts }
@app.get("/posts") # don't need to add the slash unneccasarily
def get_posts():
return {"data ---> " : my_posts }
def find_post_with_id(id):
if my_posts.__contains__(id) :
return my_posts[id]
# FASTAPI does it's magic validation and checks if whatever is sent is integer or not
@app.get("/posts/{id}")
# we get the id passed, along with the default response
def get_posts(id : int, response : Response ):
post = find_post_with_id( int(id) )
if post is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"post with the id requested {id} is not present ; Invalid id ; " )
return {
"this is main_4.py server => " : id,
"THIS IS THE PATH PARAMETER => " : id,
"THIS IS IT'S TYPE => " : str(type(id)) ,
f"post with id={id}" : post
}
# this is how to have a DEFAULT STATUS CODE
@app.post("/posts" , status_code=status.HTTP_201_CREATED )
def post_posts(post : Post):
print(post)
post_dict = post.model_dump()
print(post_dict)
id = random.randint(1, 1_000)
post_dict[id] = post_dict
return {
"data recieved by API in dictionary form " : post_dict,
"data recieved ; now the list of posts are :- " : my_posts
}