-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
80 lines (60 loc) · 2.86 KB
/
client.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
import json
import os
import requests
class Client:
def __init__(self, url: str, username: str, password: str):
self.url = url
self.body = {
"username": username,
"password": password
}
self.user_url = "/user/"
self.todo_url = "/todo/"
self.files_url = "/files/"
response = requests.post(self.url + self.user_url, data=self.body)
if response.status_code != 201 and response.status_code != 200:
print(response.json())
else:
token = "Bearer " + response.json()['access_token']
self.headers = {"Authorization": token}
def get_todo(self):
response = requests.get(self.url + self.todo_url, headers=self.headers)
print(json.dumps(response.json(), indent = 1))
def post_todo(self, description: str):
response = requests.post(self.url + self.todo_url, data={'description': description}, headers=self.headers)
print(response.json())
def put_todo(self, task_id: int, description: str):
response = requests.put(self.url+self.todo_url+str(task_id), data={'description': description}, headers=self.headers)
print(response.json())
def delete_todo(self, task_id: int):
response = requests.delete(self.url+self.todo_url+str(task_id), headers=self.headers)
print(response.json())
"""
--------------------------------------------------------------------------------------------------------------------
-------------------------------------------------- File Storage ----------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
"""
def post_files(self, file_path:str):
with open(file_path, "rb") as file:
response = requests.post(self.url + self.files_url, files={'file': file}, headers=self.headers)
print(response.json())
def get_files(self):
response = requests.get(self.url + self.files_url, headers=self.headers)
print(json.dumps(response.json(), indent=1))
def delete_file(self, filename: str):
response = requests.delete(self.url + self.files_url + filename, headers=self.headers)
print(response.json())
def download_file(self, filename: str):
response = requests.get(self.url + self.files_url + filename, headers=self.headers, stream=True)
if response.status_code != 200:
print(response.json())
else:
files_path = "./files/"
if not os.path.exists(files_path):
os.mkdir(files_path)
path = files_path + self.body["username"]+"/"
if not os.path.exists(path):
os.mkdir(path)
f = open(path + str(filename), 'wb')
f.write(response.content)
f.close()