-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
172 lines (141 loc) · 5.95 KB
/
Copy pathclient.py
File metadata and controls
172 lines (141 loc) · 5.95 KB
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
import httpx
API_URL = "http://localhost:8000"
# Client Functions
def login(email: str, password: str) -> str:
response = httpx.post(f"{API_URL}/api/users/token", data={"username": email, "password": password})
if response.status_code == 200:
token = response.json()["access_token"]
print("Login successful.")
return token
else:
raise Exception(f"Login failed: {response.text}")
def create_user(name: str, email: str, contact: str, password: str):
response = httpx.post(f"{API_URL}/api/users/", json={"name": name, "email": email, "contact": contact, "password": password})
if response.status_code == 200:
print("User created:", response.json())
else:
print("Error creating user:", response.text)
def get_users(token: str):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.get(f"{API_URL}/api/users/", headers=headers)
if response.status_code == 200:
print("Users:", response.json())
else:
print("Error fetching users:", response.text)
def get_user(user_id: int, token: str):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.get(f"{API_URL}/api/users/{user_id}", headers=headers)
if response.status_code == 200:
print("User:", response.json())
else:
print("Error fetching user:", response.text)
def update_user(user_id: int, token: str, name: str = None, email: str = None, contact: str = None):
data = {}
if name:
data["name"] = name
if email:
data["email"] = email
if contact:
data["contact"] = contact
headers = {"Authorization": f"Bearer {token}"}
response = httpx.put(f"{API_URL}/api/users/{user_id}", json=data, headers=headers)
if response.status_code == 200:
print("User updated:", response.json())
else:
print("Error updating user:", response.text)
def delete_user(user_id: int, token: str):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.delete(f"{API_URL}/api/users/{user_id}", headers=headers)
if response.status_code == 200:
print("User deleted")
else:
print("Error deleting user:", response.text)
def add_comment(comment: str, token:str):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.post(f"{API_URL}/api/comments/", json={"comment": comment}, headers=headers)
if response.status_code == 200:
print("Comment added:", response.json())
else:
print("Error adding comment:", response.text)
def get_comments(token: str, skip: int=0, limit: int = 5):
headers = {"Authorization": f"Bearer {token}"}
#response = httpx.get(f"{API_URL}/comment/", headers=headers)
response = httpx.get(f"{API_URL}/api/comments/?skip={skip}&limit={limit}")
if response.status_code == 200:
print("Comments:", response.json())
else:
print("Error fetching comments:", response.text)
def add_reply(reply: str, parent_id: int, token:str):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.post(f"{API_URL}/api/comments/", json={"comment": reply, "parent_id": parent_id}, headers=headers)
if response.status_code == 200:
print("Comment added:", response.json())
else:
print("Error adding comment:", response.text)
def get_comment(token: str, comment_id: int):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.get(f"{API_URL}/api/comments/{comment_id}", headers=headers)
if response.status_code == 200:
print("Comments:", response.json())
else:
print("Error fetching comments:", response.text)
def delete_comment(comment_id: int, token: str):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.delete(f"{API_URL}/api/comments/{comment_id}", headers=headers)
if response.status_code == 200:
print("Comment deleted")
else:
print("Error deleting comment:", response.text)
def get_comments_by_userid(token: str, user_id: int, skip: int=0, limit: int = 10):
headers = {"Authorization": f"Bearer {token}"}
response = httpx.get(f"{API_URL}/api/comments/user/{user_id}", headers=headers)
if response.status_code == 200:
print("Comments:", response.json())
else:
print("Error fetching comments:", response.text)
# Example Usage
if __name__ == "__main__":
#uncomment to create initial user
#create_user("Jay", "Jay@example.com","12345","pass1234")
#token = login("Jay@example.com", "pass1234")
#create_user("Jas", "Jas@example.com","67890","pass6789")
#token = login("Jas@example.com", "pass6789")
#create_user("Bob", "bob@example.com","67890", "pass6789")
token = login("bob@example.com", "pass6789")
get_users(token)
#add_comment("Bwahaha!", token)
#add_comment("Hello!", token)
#add_comment("How are you!", token)
#add_comment("Konnichiwa!", token)
#add_comment("Ohayou!", token)
#add_comment("Kamusta!", token)
#add_comment("comment6!", token)
#add_comment("comment7!", token)
#add_comment("comment8!", token)
#add_comment("comment9!", token)
#add_comment("comment10!", token)
#add_comment("comment11!", token)
#add_comment("comment12!", token)
#add_comment("comment13!", token)
#add_comment("comment14!", token)
#add_comment("comment15!", token)
#add_reply("reply1", 1, token)
#add_reply("reply2", 3, token)
#add_reply("reply3", 3, token)
#token = login("Jas@example.com", "pass6789")
#add_reply("reply4", 3, token)
#get_comments(token)
#get_comment(token,1)
get_comments(token)
#delete_comment(9, token)
print("-----------")
get_comment(token, 3)
print("-----------")
get_comments_by_userid(token, 1)
#add_reply("Wazup!",1,token)
#add_reply("Afternoon!",1,token)
#get_comment(1, token)
#get_user(1, token)
#update_user(1,token, name="jay")
#delete_user(2, token)
#get_users(token)