forked from Defelo/PyCrypCli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
244 lines (216 loc) · 9.14 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
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
from typing import List, Tuple
from uuid import uuid4
from websocket import WebSocket, create_connection
from exceptions import *
def uuid() -> str:
return str(uuid4())
class Client:
def __init__(self, server: str):
self.websocket: WebSocket = create_connection(server)
def request(self, command: dict) -> dict:
self.websocket.send(json.dumps(command))
return json.loads(self.websocket.recv())
def microservice(self, ms: str, endpoint: List[str], data: dict) -> dict:
response: dict = self.request({
"ms": ms,
"endpoint": endpoint,
"data": data,
"tag": uuid()
})
if "error" in response or "data" not in response:
raise InvalidServerResponseException(response)
return response["data"]
def register(self, username: str, email: str, password: str) -> str:
response: dict = self.request({
"action": "register",
"name": username,
"mail": email,
"password": password
})
if "error" in response:
error: str = response["error"]
if error.startswith("password invalid (condition:"):
raise WeakPasswordException()
if error == "username already exists":
raise UsernameAlreadyExistsException()
if error == "email invalid":
raise InvalidEmailException()
raise InvalidServerResponseException(response)
if "token" not in response:
raise InvalidServerResponseException(response)
return response["token"]
def login(self, username: str, password: str) -> str:
response: dict = self.request({
"action": "login",
"name": username,
"password": password
})
if "error" in response:
error = response["error"]
if error == "permission denied":
raise InvalidLoginException()
raise InvalidServerResponseException(response)
if "token" not in response:
raise InvalidServerResponseException(response)
return response["token"]
def session(self, token: str):
response: dict = self.request({
"action": "session",
"token": token
})
if "error" in response:
error: str = response["error"]
if error == "invalid token":
raise InvalidSessionTokenException()
raise InvalidServerResponseException(response)
if "token" not in response:
raise InvalidServerResponseException(response)
def info(self) -> dict:
response: dict = self.request({
"action": "info"
})
if "error" in response:
raise InvalidServerResponseException(response)
return response
def get_all_devices(self) -> List[dict]:
response: dict = self.microservice("device", ["device", "all"], {})
if "error" in response or "devices" not in response:
raise InvalidServerResponseException(response)
devices: List[dict] = response["devices"]
return devices
def create_device(self) -> dict:
response: dict = self.microservice("device", ["device", "create"], {})
if "error" in response:
raise InvalidServerResponseException(response)
return response
def change_device_name(self, device_uuid: str, name: str):
response: dict = self.microservice("device", ["device", "change_name"], {
"device_uuid": device_uuid,
"name": name
})
if "error" in response:
raise InvalidServerResponseException(response)
def get_all_files(self, device_uuid: str) -> List[dict]:
response: dict = self.microservice("device", ["file", "all"], {
"device_uuid": device_uuid
})
if "error" in response or "files" not in response:
raise InvalidServerResponseException(response)
files: List[dict] = response["files"]
return files
def create_file(self, device_uuid: str, filename: str, content: str):
response: dict = self.microservice("device", ["file", "create"], {
"device_uuid": device_uuid,
"filename": filename,
"content": content
})
if "error" in response:
raise InvalidServerResponseException(response)
def remove_file(self, device_uuid: str, file_uuid: str):
response: dict = self.microservice("device", ["file", "delete"], {
"device_uuid": device_uuid,
"file_uuid": file_uuid
})
if "error" in response:
raise InvalidServerResponseException(response)
def create_wallet(self) -> Tuple[str, str]:
response: dict = self.microservice("currency", ["create"], {})
if "error" in response:
error: str = response["error"]
if error == "You already own a wallet!":
raise AlreadyOwnAWalletException()
raise InvalidServerResponseException(response)
if "key" not in response or "uuid" not in response:
raise InvalidServerResponseException(response)
return response["uuid"], response["key"]
def get_wallet(self, wallet_uuid: str, key: str) -> dict:
response: dict = self.microservice("currency", ["get"], {
"source_uuid": wallet_uuid,
"key": key
})
if "error" in response:
error: str = response["error"]
if error == "invalid key":
raise InvalidKeyException()
raise InvalidServerResponseException(response)
if "success" not in response:
raise InvalidServerResponseException(response)
return response["success"]
def delete_wallet(self, wallet_uuid: str, key: str):
response: dict = self.microservice("currency", ["delete"], {
"source_uuid": wallet_uuid,
"key": key
})
if "error" in response:
error: str = response["error"]
if error == "invalid key":
raise InvalidKeyException()
raise InvalidServerResponseException(response)
if "ok" not in response:
raise InvalidServerResponseException(response)
def send(self, wallet_uuid: str, key: str, destination: str, amount: int, usage: str):
response: dict = self.microservice("currency", ["send"], {
"source_uuid": wallet_uuid,
"key": key,
"send_amount": amount,
"destination_uuid": destination,
"usage": usage
})
if "error" in response:
error: str = response["error"]
if error == "The source wallet would make debt transaction canceled":
raise SourceWalletTransactionDebtException()
if error.startswith("Your Souce or Destination uuid is invalid"):
raise InvalidWalletException()
if error == "invalid key":
raise InvalidKeyException()
raise InvalidServerResponseException(response)
def create_service(self, device_uuid: str, name: str) -> dict:
response: dict = self.microservice("service", ["create"], {
"name": name,
"device_uuid": device_uuid
})
if "error" in response:
error: str = response["error"]
if error == "you already own a service with this name":
raise AlreadyOwnServiceException()
raise InvalidServerResponseException(response)
return response
def get_services(self, device_uuid: str) -> List[dict]:
response: dict = self.microservice("service", ["list"], {
"device_uuid": device_uuid
})
if "error" in response or "services" not in response:
raise InvalidServerResponseException(response)
return response["services"]
def use_service(self, device_uuid, service_uuid: str, **kwargs):
response: dict = self.microservice("service", ["use"], {
"device_uuid": device_uuid,
"service_uuid": service_uuid,
**kwargs
})
if "error" in response:
error: str = response["error"]
if error == "unknown service":
raise UnknownServiceException()
raise InvalidServerResponseException(response)
return response
def spot(self) -> dict:
response: dict = self.microservice("device", ["device", "spot"], {})
if "error" in response:
raise InvalidServerResponseException(response)
return response
def device_info(self, device_uuid: str) -> dict:
response: dict = self.microservice("device", ["device", "info"], {
"device_uuid": device_uuid
})
if "error" in response:
raise InvalidServerResponseException(response)
return response
def part_owner(self, device_uuid: str) -> bool:
response: dict = self.microservice("service", ["part_owner"], {
"device_uuid": device_uuid
})
if "error" in response or "ok" not in response:
raise InvalidServerResponseException(response)
return response["ok"]