-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
440 lines (373 loc) · 17.9 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""
Aplicações distribuídas - Projeto 4 - client.py
Grupo: 77
Alunos: José Almeida - 55373, Augusto Gouveia - 55371
"""
### Imports
import sys
import getopt
import requests
import os
import color_utils as cu
from pprint import pprint
import urllib3
urllib3.disable_warnings(urllib3.exceptions.SecurityWarning)
### Variáveis Globais
CERT_PATH = "/mnt/d/_/Projects/FCUL/AD/dist-sys/v4/certs/"
AUTH_ARGUMENTS = {"verify":CERT_PATH + 'root.pem', "cert":(CERT_PATH + 'cli.crt',CERT_PATH + 'cli.key')}
# Mensagens de Erro
serverCommands = ("CREATE", "READ", "DELETE", "UPDATE")
PARAMETER_ERROR = "Verifique os parâmetros.\nUtilização: client.py [-c] <IP/Hostname> <Porto>"
UNKNOWN_COMMAND_ERROR = "UNKNOWN COMMAND, TYPE 'HELP'"
MISSING_ARGUMENTS_ERROR = "MISSING ARGUMENTS"
EXCESSIVE_ARGUMENTS_ERROR = "TOO MANY ARGUMENTS"
INVALID_ARGUMENTS_ERROR = "INVALID ARGUMENTS"
GENERAL_CONNECTION_ERROR = cu.colorWrite("CONNECTION ERROR - Make sure that you're logged in and try again.", 'red')
CONNECTION_REFUSED_ERROR = cu.colorWrite("CONNECTION REFUSED", 'red')
HELP_MESSAGE = f"""{cu.colorWrite('Comandos Disponíveis', 'green')}:
{cu.colorWrite('Servidor', 'blue')}:
- CREATE
- UTILIZADOR <nome> <senha>
- ALBUM <id_spotify>
- ARTISTA <id_spotify>
- <id_user> <id_album> <avaliacao>
- READ
- UTILIZADOR <id_user>
- ALBUM <id_album>
- ARTISTA <id_artista>
- ALBUM <id_album>
- ALL <UTILIZADORES | ARTISTAS | ALBUNS>
- ALL ALBUNS_A <id_artista>
- ALL ALBUNS_U <id_user>
- ALL ALBUNS <avaliacao>
- DELETE
- UTILIZADOR <id_user>
- ALBUM <id_album>
- ARTISTA <id_artista>
- ALBUM <id_album>
- ALL <UTILIZADORES | ARTISTAS | ALBUNS>
- ALL ALBUNS_A <id_artista>
- ALL ALBUNS_U <id_user>
- ALL ALBUNS <avaliacao>
- UPDATE
- UTILIZADOR <id_user> <senha>
- ALBUM <id_album> <avaliacao> <id_user>
{cu.colorWrite('Cliente', 'blue')}:
- CLEAR
- HELP
- EXIT"""
def argumentChecker(userInput):
"""
Verifica a integridade e validade dos argumentos relativos aos comandos
suportados pelo servidor (CREATE, READ, DELETE, UPDATE).
Requires: userInput é uma lista representante dos vários dados introduzidos
pelo utilizador.
Ensures: Devolve um booleano representando a validade dos argumentos introduzidos
respetivamente ao comando invocado.
"""
# O número de argumentos tem que excluir o nome do comando
command = userInput[0]
arguments = userInput[1:]
argLimitsCreate = {"UTILIZADOR": 3,
"ARTISTA": 2,
"ALBUM": 2
}
argLimitsReadDelete = {"UTILIZADOR": 2,
"ARTISTA": 2,
"ALBUM": 2
}
argLimitsReadDeleteAll = {"UTILIZADORES": 2,
"ARTISTAS": 2,
"ALBUNS_A": 3,
"ALBUNS_U": 3,
"ALBUNS": 3}
# A partir desta linha:
# Verifica-se a validade de cada argumento para cada comando (nas listas acima
# definida), devolvendo False e emitindo um erro de argumentos inválidos caso
# os tipos de dados dos argumentos introduzidos pelo utilizador não corresponderem
# com os tipos de dados esperados para os argumentos do comando em causa.
if command == "CREATE":
try:
option = arguments[0]
if not option.isnumeric():
max_arg = argLimitsCreate.get(option)
if len(arguments) < max_arg:
# Emitir erro de argumentos insuficientes e retornar False caso
# a quantidade de argumentos presentes nos dados introduzidos pelo
# utilizador for menor que a quantidadede argumentos esperada para
# o comando em causa.
print(MISSING_ARGUMENTS_ERROR)
return False
elif len(arguments) > max_arg:
# Emitir erro de argumentos em demasia e retornar False caso a
# quantidade de argumentos presentes nos dados introduzidos pelo
# utilizador não for maior que a quantidade de argumentos esperada
# para o comando em causa.
print(EXCESSIVE_ARGUMENTS_ERROR)
return False
else:
if len(arguments) < 3:
print(MISSING_ARGUMENTS_ERROR)
return False
elif len(arguments) > 3:
print(EXCESSIVE_ARGUMENTS_ERROR)
return False
id_user = int(arguments[0])
id_album = int(arguments[1])
return True
except: # Se o programa cai numa exceção, significa que os argumentos dados são inválidos
print(INVALID_ARGUMENTS_ERROR)
return False
elif command == "READ" or command == "DELETE":
try:
option = arguments[0]
sub_option = arguments[1]
if option != "ALL":
max_arg = argLimitsReadDelete.get(option)
else:
max_arg = argLimitsReadDeleteAll.get(sub_option)
if sub_option != "ALBUNS":
if len(arguments) < max_arg:
# Emitir erro de argumentos insuficientes e retornar False caso
# a quantidade de argumentos presentes nos dados introduzidos pelo
# utilizador for menor que a quantidadede argumentos esperada para
# o comando em causa.
print(MISSING_ARGUMENTS_ERROR)
return False
elif len(arguments) > max_arg:
# Emitir erro de argumentos em demasia e retornar False caso a
# quantidade de argumentos presentes nos dados introduzidos pelo
# utilizador não for maior que a quantidade de argumentos esperada
# para o comando em causa.
print(EXCESSIVE_ARGUMENTS_ERROR)
return False
else:
if len(arguments) > 3:
print(EXCESSIVE_ARGUMENTS_ERROR)
return False
if option in ("UTILIZADOR", "ARTISTA", "ALBUM"):
id = int(arguments[1])
elif option == "ALL":
sub_option = arguments[1]
if sub_option in ("ALBUNS_A", "ALBUNS_U"):
id = int(arguments[2])
elif sub_option in ("ALBUNS", "AVALIACOES", "ARTISTAS", "UTILIZADORES"):
return True
else:
return False
else:
return False
return True
except: # Se o programa cai numa exceção, significa que os argumentos dados são inválidos
print(INVALID_ARGUMENTS_ERROR)
return False
elif command == "UPDATE":
try:
option = arguments[0]
if option == "ALBUM":
if len(arguments) < 4:
print(MISSING_ARGUMENTS_ERROR)
return False
elif len(arguments) > 4:
print(EXCESSIVE_ARGUMENTS_ERROR)
return False
id_album = int(arguments[1])
id_user = int(arguments[3])
return True
elif option == "UTILIZADOR":
if len(arguments) < 3:
print(MISSING_ARGUMENTS_ERROR)
return False
elif len(arguments) > 3:
print(EXCESSIVE_ARGUMENTS_ERROR)
return False
id_user = int(arguments[1])
return True
print(INVALID_ARGUMENTS_ERROR)
return False
except: # Se o programa cai numa exceção, significa que os argumentos dados são inválidos
print(INVALID_ARGUMENTS_ERROR)
return False
# Programa Principal
# Verificação e validação de argumentos iniciais
#
try:
opts, args = getopt.getopt(sys.argv[1:], "c")
HOST = str(args[0])
PORT = int(args[1])
if len(args) != 2:
raise PARAMETER_ERROR
except Exception as e:
# Emitir erro de parâmetros e terminar o processo
# caso a validade dos argumentos introduzidos não se
# verifique.
print(PARAMETER_ERROR)
sys.exit(1)
halt = False
try:
avaliacoes = {"M": 1,
"m": 2,
"S": 3,
"B": 4,
"MB": 5}
queryUrlReadDelete = {"UTILIZADOR": f"https://{HOST}:{PORT}/utilizadores/",
"ALBUM": f"https://{HOST}:{PORT}/albuns/",
"ARTISTA": f"https://{HOST}:{PORT}/artistas/"}
queryUrlReadDeleteAll = {"UTILIZADORES": f"https://{HOST}:{PORT}/utilizadores",
"ALBUNS": f"https://{HOST}:{PORT}/albuns",
"ARTISTAS": f"https://{HOST}:{PORT}/artistas",
"ALBUNS_A": f"https://{HOST}:{PORT}/albuns/artistas/",
"ALBUNS_U": f"https://{HOST}:{PORT}/albuns/utilizadores/",
"AVALIACOES": f"https://{HOST}:{PORT}/albuns/avaliacoes"}
while not halt:
try:
# Processamento dos dados introduzidos pelo utilizador
if not len([opt for opt in opts if "-c" in opt]):
handle = f"client ▶ {cu.colorWrite(HOST, 'green')}: "
else:
handle = f"client ▶ {HOST}:"
userInput = input(handle).split()
# Passar ao próximo ciclo caso o utilizador não introduza nada
if not userInput:
continue
command = userInput[0].upper()
arguments = userInput[1:]
if command == "EXIT":
# Sair do ciclo caso o utilizador invoque o comando EXIT
halt = True
continue
elif command == "HELP":
print(HELP_MESSAGE)
elif command == "CLEAR":
# Limpar o histórico da consola.
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
elif command in serverCommands:
if argumentChecker(userInput):
# Uso da função argumentChecker para verificar a integridade de validade
# dos argumentos fornecidos pelo utilizador em relação ao comando que o mesmo
# invocou
# Formular o pedido antes de ser enviado ao servidor, contendo o comando
# e os respetivos argumentos na posição correta.
if command == "CREATE":
option = arguments[0].upper()
if option == "UTILIZADOR":
utilizador = {"nome": arguments[1],
"senha": arguments[2]}
# efetuar request
r = requests.post(f"https://{HOST}:{PORT}/utilizadores", json=utilizador, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
elif option == "ARTISTA" or option == "ALBUM":
query = {"id_spotify": arguments[1]}
# efetuar request
if option == "ARTISTA":
r = requests.post(f"https://{HOST}:{PORT}/artistas", json=query, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else:
r = requests.post(f"https://{HOST}:{PORT}/albuns", json=query, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else:
avaliacao = {"id_user": int(arguments[0]),
"id_album": int(arguments[1]),
"id_avaliacao": avaliacoes.get(arguments[2])}
r = requests.post(f"https://{HOST}:{PORT}/albuns/avaliacoes", **AUTH_ARGUMENTS, json=avaliacao)
print(r.status_code)
print("***")
# efetuar request
elif command == "READ" or command == "DELETE":
option = arguments[0]
if option in ("UTILIZADOR", "ARTISTA", "ALBUM"):
id = arguments[1]
# efetuar request
if command == "READ":
r = requests.get(queryUrlReadDelete.get(option) + id, **AUTH_ARGUMENTS)
print(r.status_code)
pprint(r.json())
print("***")
else:
r = requests.delete(queryUrlReadDelete.get(option) + id, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else:
sub_option = arguments[1]
if sub_option in ("UTILIZADORES", "ARTISTAS", "AVALIACOES"):
if command == "READ":
r = requests.get(queryUrlReadDeleteAll.get(sub_option), **AUTH_ARGUMENTS)
print(r.status_code)
pprint(r.json())
print("***")
else:
r = requests.delete(queryUrlReadDeleteAll.get(sub_option), **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
elif sub_option == "ALBUNS":
if len(arguments) == 3:
avaliacao = avaliacoes.get(arguments[2])
if command == "READ":
r = requests.get(queryUrlReadDeleteAll.get(sub_option) + f"/avaliacoes/{avaliacao}", **AUTH_ARGUMENTS)
print(r.status_code)
pprint(r.json())
print("***")
else:
r = requests.delete(queryUrlReadDeleteAll.get(sub_option) + f"/avaliacoes/{avaliacao}", **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else:
if command == "READ":
r = requests.get(queryUrlReadDeleteAll.get(sub_option), **AUTH_ARGUMENTS)
print(r.status_code)
pprint(r.json())
print("***")
else:
r = requests.delete(queryUrlReadDeleteAll.get(sub_option), **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else:
id = arguments[2]
if command == "READ":
r = requests.get(queryUrlReadDeleteAll.get(sub_option) + id, **AUTH_ARGUMENTS)
print(r.status_code)
pprint(r.json())
print("***")
else:
r = requests.delete(queryUrlReadDeleteAll.get(sub_option) + id, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
# efetuar request
else: # UPDATE
option = arguments[0]
if option == "ALBUM":
avaliacao = {"id_album": int(arguments[1]),
"id_avaliacao": avaliacoes.get(arguments[2]),
"id_user": int(arguments[3])}
# efetuar request
r = requests.put(f"https://{HOST}:{PORT}/albuns/avaliacoes", json=avaliacao, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else: # UTILIZADOR
id_user = int(arguments[1])
utilizador = {"senha": arguments[2]}
# efetuar request
r = requests.put(f"https://{HOST}:{PORT}/utilizadores/{id_user}", json=utilizador, **AUTH_ARGUMENTS)
print(r.status_code)
print("***")
else:
# Emitir erro de comando desconhecido caso este não se encontre na lista
# de comandos suportados pelo servidor e cliente.
print(UNKNOWN_COMMAND_ERROR)
except KeyboardInterrupt as e:
# Lidar com KeyboardInterrupt
print("\nReceived SIGINT, stopping.\n")
sys.exit(1)
except ConnectionRefusedError:
# Emitir erro de conexão recusada caso seja impossível contactar o servidor.
print(CONNECTION_REFUSED_ERROR)
except Exception as e:
# Emitir erro geral de conexão caso hajam quaisqueres complicações no processo
# de envio do pedido ao servidor e receção da resposta.
print(GENERAL_CONNECTION_ERROR)