-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
140 lines (115 loc) · 3.63 KB
/
service.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
from fastapi.responses import JSONResponse
from concurrent.futures import ThreadPoolExecutor
from config import API_EPS
import utils
import json
import random
# =====> MAIN FUNCTIONS
def auth_user(username, password):
response = verify_user(username, password)
if response.status_code == 200:
# verified
return response
if response.status_code == 400:
# incorrect password
return response
if response.status_code == 201:
# new username, create user
return create_user(username, password)
# some other response
return response
def load_user_data(username):
with ThreadPoolExecutor() as executor:
# Execute the functions concurrently
storage_usage_future = executor.submit(get_storage_usage, username)
bandwidth_usage_future = executor.submit(get_bandwidth_usage, username)
images_links_future = executor.submit(get_images_links, username)
# Get results from the futures
storage_usage = storage_usage_future.result()
bandwidth_usage = bandwidth_usage_future.result()
images_links = images_links_future.result()
random.shuffle(images_links)
return JSONResponse({
"username": username,
"storage_usage": storage_usage,
"bandwidth_usage": bandwidth_usage,
"images_links": images_links,
})
def delete_image(link):
url = API_EPS.get("storage-account-service/delete_image")
data = {
"link": link,
}
return utils.send_request(url, data)
def upload_image(username, file_base64):
url = API_EPS.get("storage-account-service/upload_image")
data = {
"username": username,
"file_base64": file_base64,
}
return utils.send_request(url, data)
# =====> HELPER FUNCTIONS
def get_images_links(username):
url = API_EPS.get("storage-account-service/get_images_links")
data = {"username": username}
response = utils.send_request(url, data)
if response.status_code == 200:
images_links = json.loads(response.body).get("images_links")
return images_links
return None
def get_storage_usage(username):
url = API_EPS.get("storage-monitor-service/get_storage_usage")
data = {"username": username}
response = utils.send_request(url, data)
if response.status_code == 200:
storage_usage = json.loads(response.body).get("storage_usage")
return storage_usage
return None
def get_bandwidth_usage(username):
url = API_EPS.get("usage-monitor-service/get_bandwidth_usage")
data = {"username": username}
response = utils.send_request(url, data)
if response.status_code == 200:
bandwidth_usage = json.loads(response.body).get("bandwidth_usage")
return bandwidth_usage
return None
def verify_user(username, password):
url = API_EPS["auth-service/verify_user"]
data = {
"username": username,
"password": password,
}
return utils.send_request(url, data)
def create_user_in_auth_service(username, password):
url = API_EPS["auth-service/create_user"]
data = {
"username": username,
"password": password,
}
response = utils.send_request(url, data)
return response
def create_user_in_storage_account_service(username, password):
url = API_EPS["storage-account-service/create_user"]
data = {
"username": username,
}
response = utils.send_request(url, data)
return response
def create_user(username, password):
with ThreadPoolExecutor() as executor:
# Execute the functions concurrently
creater1_future = executor.submit(
create_user_in_auth_service,
username, password
)
creater2_future = executor.submit(
create_user_in_storage_account_service,
username, password
)
# Get results from the futures
creater1_response = creater1_future.result()
creater2_response = creater2_future.result()
# Return any failed response
if creater1_response.status_code == 200:
return creater2_response
return creater1_response