forked from Wo1v3r/LinuxCourseProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client
executable file
·529 lines (458 loc) · 13.5 KB
/
client
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/bin/bash
set -e
OPT=$(getopt -n "client" -o "h,v" -l "help,version,ip:,register:,movies,order:,history:,cancel-ticket:,recoverKey:,userkey:,locations,movieID:,movieName:,theaterID:,city:,country:" -- "$@")
eval set -- "$OPT"
#flags
registerF=0
moviesF=0
orderF=0
historyF=0
cancelF=0
restoreF=0
locationsF=0
movieInfoF=0
#default values
username=""
password=""
userKey=$GLOBAL_USER_KEY
# decoration
format="%3s | %-12s | %-15s | %-50s\n" #format for output
format_orders="%3s | %-30s | %-50s | %-12s\n" #format for output
#server address link
server_address="http://localhost"
# check correct format of credentials i.ex : username:password
function check_creds(){
tempvar="$1"
separators="${tempvar//[^:]}" # checking amount of ':' in credentials string
if [ "${#separators}" == 1 ]; then
#if have exactly 1 ':' delimiter, set key to apikey variable
password="${1#*:}"
username="${1%%:*}"
elif [ "${#separators}" == 0 ]; then
echo "client: credentials do not contain an API component"
exit 1
fi
# check not empty credentials
if [ -z "$password" ] || [ -z "$username" ];then
echo "client : Username and pussword can not be empty!"
exit 1
fi
}
# check correct format of tcket order i.ex : <MOVIE_ID>:<LOCATION_ID>
function perseOrderParameters(){
tempvar="$1"
separators="${tempvar//[^:]}" # checking amount of ':' in request string
if [ "${#separators}" == 1 ]; then
#if have exactly 1 ':' delimiter, set locationID and movieID
location_order_id="${1#*:}"
movie_order_id="${1%%:*}"
elif [ "${#separators}" == 0 ]; then
echo "client: Incorrect pair "$1", correct is : <MOVIE_ID>:<LOCATION_ID> "
exit 1
fi
# check not empty credentials
if [ -z "$location_order_id" ] || [ -z "$movie_order_id" ];then
echo "client : Movie ID and Location ID cannot be empty!"
exit 1
fi
}
#version
function versionShow(){
echo -e "Linux environment programming: \n\nVersion 1.0\n
Authors:\n-----------------------\nLiran Rotenberg\nJohnatan Leon\nIsabelle Meif\nAlexey Silyuk\n"
exit
}
#Synopsis
function help(){
echo "Movie tickets ordering system :
./client -h : this synopsis
./client -v : version with authors names
Change API's ip:
./client --ip <IP> [OPTS]
Register to system:
./client --register username:password
Recover lost userkey:
./client --recoverKey <USERNAME>:<PASSWORD>
View movies :
All movies:
./client --movies
By ID:
./client --movieID <MOVIEID>
By Name (or part of name,prefix):
./client --movieName \"MOVIE NAME\"
View locations:
All without filtering:
./client --locations
By ID:
./client --locationID <ID>
By Country:
./client --locations --country <COUNTRY>, may be used country prefix
By City:
./client --locations --city <CITY> , may be used city prefix
Ordering :
View orders history
./client --history <USER_KEY>
Order new ticket:
./client --order-ticket <MOVIE_ID>:<LOCATION_ID> --userkey <USER_KEY>
Cancel ticket order:
./client --cancel-ticket <TICKET_ID> --userkey <USER_KEY>
"
exit
}
while [ "$1" != "--" ]; do
case "$1" in
-h|--help)
help
;;
-v|--version)
versionShow
;;
--register)
register_credentials="$2"
registerF=1
shift
;;
--recoverKey)
restoreF=1
restore_credentials="$2"
shift
;;
--movies)
moviesF=1
;;
--cancel-ticket)
cancel_movie_id="$2"
cancelF=1
shift
;;
--history)
historyF=1
userKey="$2"
shift
;;
--order)
movie_order_params="$2"
orderF=1
shift
;;
--userkey)
userKey="$2"
shift
;;
--locations)
locationsF=1
;;
--movieID)
movieID="$2"
movieInfoF=1
shift
;;
--movieName)
movieName="$2"
movieInfoF=1
shift
;;
--city)
city="$2"
cityF=1
shift
;;
--theaterID)
theaterID="$2"
theaterIDF=1
shift
;;
--country)
country="$2"
countryF=1
shift
;;
--ip)
server_address=http://"$2"
shift
;;
*)
echo Unknown parameter
exit 1
;;
esac
shift
done
if ! [ -z "$2" ]; then
echo "client: unknown extra arguments : "$2""
exit 1
fi
# Register
if [ "$registerF" == 1 ]; then
if ! [ -z "$register_credentials" ]; then
check_creds "$register_credentials"
data=$(curl -s ""$server_address"/Register/username="$username"&password="$password"")
if [ $(jq -r ".status" <<< "$data") == "success" ]; then
userAuthKey=$(jq -r ".key" <<< "$data")
echo -e "\n--------------------\nRegistration agent : You registered successfully\n--------------------\n Username : "$username"\n Password : "$password"\n UserKey : "$userAuthKey"\n please save your userKey in safe place for ordering tikets\n run next command to add key to work environment:\n export GLOBAL_USER_KEY="$userAuthKey""
else
message="$(jq -r ".message" <<< "$data")"
echo -e "\n--------------------\nRegistration agent : $message\n--------------------\n"
exit 1
fi
else
echo "Incorrect credentials"
exit 1
fi
fi
# Restore user key
if [ "$restoreF" == 1 ];then
restoreF=0
if ! [ -z "$restore_credentials" ]; then
check_creds "$restore_credentials"
responce=$(curl -s ""$server_address"/RecoverKey/username=$username&password=$password")
echo -e "\n--------------------\nKey recovery agent :\n--------------------"
if [ $(jq -r ".status" <<< "$responce") == "success" ]; then
userKey=$(jq -r ".key" <<< "$responce")
echo -e "Your secret key is : "$userKey", save it in safe place !\n"
else
echo -e "Key for "$username":"$password" pair not found in system\n"
exit 1
fi
else
echo "No credentials provided"
exit 1
fi
fi
# Order ticket NOT DONE
if [ "$orderF" == 1 ]; then
if [ -n "$userKey" ];then
perseOrderParameters "$movie_order_params"
response=$(curl -s ""$server_address"/Order/key="$userKey"&movieID="$movie_order_id"&locationID="$location_order_id"" )
message="$(jq .message <<< "$response")"
if [ $(jq -r ".status" <<< "$response") == "success" ];then
orderID=$(jq -r ".OrderID" <<< "$response")
echo $message
#echo "Your order number is : "$orderID", enjoy!"
else
echo $message
exit 1
fi
else
echo "No credentials provided"
exit 1
fi
fi
# Cancel ordered ticket
if [ "$cancelF" == 1 ]; then
if ! [ -z "$userKey" ];then
responce=$(curl -s ""$server_address"/Cancel/key="$userKey"&orderID="$cancel_movie_id"")
cancel_result="$(jq -r ".status" <<< "$responce")"
status_message="$(jq -r ".message" <<< "$responce")"
if [ "$cancel_result" == "success" ]; then
echo "Your ticket number \""$cancel_movie_id"\" been cancelled!"
else
echo "$status_message"
exit 1
fi
else
echo "No credentials provided"
fi
fi
function getMovieNameByID(){
movie_id="$1"
movie_name=$(curl -s ""$server_address"/Movies/ID="$movie_id"" | jq .movies[0].title)
}
function getLocationsByID(){
location_id="$1"
location_country="$(curl -s ""$server_address"/Locations/ID="$location_id"" | jq .locations[0].country)"
location_city="$(curl -s ""$server_address"/Locations/ID="$location_id"" | jq .locations[0].city)"
location_address="$(curl -s ""$server_address"/Locations/ID="$location_id"" | jq .locations[0].address)"
location_name=""$location_country", "$location_city","$location_address""
}
# History view
if [ "$historyF" == 1 ];then
if [ -n "$userKey" ]; then
history_list=$(curl -s ""$server_address"/History/key="$userKey"")
amount=$(curl -s ""$server_address"/History/key="$userKey"" | jq .orders | jq length)
echo -e "\nOrders for user with userkey "$userKey"\n"
if [ "$amount" == 0 ];then
echo "No history"
exit 1
fi
printf "$format_orders" "ID" "Movie Title" "Location" "Status"
printf "$format_orders" "---" "------------" "---------" "--------"
for i in `seq 0 "$amount"`; do
current_order=$(jq .orders["$i"] <<< "$history_list")
order_id=$(jq -r ".id" <<< "$current_order")
if [ "$order_id" != null ];then
movie_id=$(jq -r ".movieID" <<< "$current_order")
getMovieNameByID "$movie_id"
location_id=$(jq -r ".locationID" <<< "$current_order")
getLocationsByID "$location_id"
isCanceled=$(jq -r ".canceled" <<< "$current_order")
if [ "$isCanceled" == "true" ]; then
status="Canceled"
else
status="Active"
fi
printf "$format_orders" "$order_id" "$movie_name" "$location_name" "$status"
fi
done
else
echo "No credentials provided"
fi
fi
function printMovie(){
movie="$1"
format="%3s | %-6s | %-50s\n" #format for output
id="$(jq -r ".id" <<< $movie)"
title="$(jq -r ".title" <<< $movie)"
year="$(jq -r ".year" <<< $movie)"
printf "$format" "$id" "$year" "$title"
}
# Get all movies list
if [ "$moviesF" == 1 ]; then
movies_list="$(curl -s ""$server_address"/Movies/")"
if [ "$(jq -r ".status" <<< "$movies_list")" == "success" ]; then
length="$(curl -s ""$server_address"/Movies/" |jq .movies | jq length)"
format="%3s | %-6s | %-50s\n" #format for output
printf "\nFull Movies list :\n---------------------------\n$format--------------------------------------------------------\n" "ID" "Year" "Movie title"
for i in `seq 0 "$length"`;
do
movie="$(jq -r ".movies[$i]" <<< $movies_list)"
if [ "$movie" != "null" ];then
printMovie "$movie"
fi
done
else
echo "No data found"
exit 1
fi
fi
# get and prints movie by ID
function printMoviesbyID(){
responce=$(curl -s ""$server_address"/Movies/ID="$movieID"")
if [ $(jq -r ".status" <<< "$responce") == "success" ];then
movie="$(jq ".movies" <<< $responce)"
if [ "$movie" != null ]; then
current_movie="$(jq -r ".[0]" <<< $movie)"
id="$(jq -r ".id" <<< $current_movie)"
title="$(jq -r ".title" <<< $current_movie)"
producer="$(jq -r ".producer" <<< $current_movie)"
year="$(jq -r ".year" <<< $current_movie)"
description="$(jq -r ".info" <<< $current_movie)"
link="$(jq -r ".link" <<< $current_movie)"
# print movie information
echo -e "--------------------------\nMovie ID : $id\nTitle : $title\nYear : $year\nProducer : $producer\nLink for more information : $link\nDescription : $description\n--------------------------\n"
fi
else
echo "Movie with ID="$movieID" not found in library"
exit 1
fi
}
function printMoviesList(){
responce=$(curl -s ""$server_address"/Movies/title="$movieName"")
if [ $(jq -r ".status" <<< "$responce") == "success" ];then
movies_list="$(jq ".movies" <<< $responce)"
arraySize="$(curl -s ""$server_address"/Movies/title=$movieName" | jq .movies | jq length)"
if [ "$arraySize" == 1 ];then
movie="$(jq -r ".[0]" <<< $movies_list)"
movieID="$(jq -r ".id" <<< $movie)"
printMoviesbyID
else
for i in `seq 0 $arraySize`;do
current_movie="$(jq -r ".[$i]" <<< $movies_list)"
movieID="$(jq -r ".id" <<< $current_movie)"
if [ "$movieID" != null ];then
printMoviesbyID
fi
done
fi
else
echo "Movie with Name \""$movieName"\" not found in library"
exit 1
fi
}
# Movie info view
if [ "$movieInfoF" == 1 ];then
echo -e "Movies according to your request :\n"
# view movie by ID
if ! [ -z "$movieID" ]; then
printMoviesbyID
fi
# view movie by name
if ! [ -z "$movieName" ]; then
printMoviesList
fi
exit 0
fi
# prints one received location
function printOneLocation(){
location="$1"
id="$(jq -r ".id" <<< $location)"
if [ "$id" != "null" ];then
country="$(jq -r ".country" <<< $location)"
city="$(jq -r ".city" <<< $location)"
address="$(jq -r ".address" <<< $location)"
printf "$format" "$id" "$country" "$city" "$address"
fi
}
function printAllLocations(){
locations=$(curl -s ""$server_address"/Locations/" )
length=$(curl -s ""$server_address"/Locations/" | jq .locations | jq length)
for i in `seq 0 $length`;
do
current_movie="$(jq -r ".locations[$i]" <<< $locations)"
printOneLocation "$current_movie"
done
exit
}
function printLocationByID(){
if [ -n "$theaterID" ];then
location=$(curl -s ""$server_address"/Locations/ID="$theaterID"" | jq .locations[0])
printOneLocation "$location"
else
echo "No movie ID received"
exit 1
fi
exit
}
function printLocationByCity(){
if [ -n "$city" ];then
locations=$(curl -s ""$server_address"/Locations/city="$city"")
length=$(curl -s ""$server_address"/Locations/city="$city"" | jq .locations | jq length)
for i in `seq 0 $length`;do
current_movie="$(jq -r ".locations[$i]" <<< $locations)"
printOneLocation "$current_movie"
done
else
echo "No city value received"
exit 1
fi
exit
}
function printLocationByCountry(){
if [ -n "$country" ];then
locations=$(curl -s ""$server_address"/Locations/country="$country"")
length=$(curl -s ""$server_address"/Locations/country="$country"" |jq .locations | jq length)
for i in `seq 0 $length`;do
current_movie="$(jq -r ".locations[$i]" <<< $locations)"
printOneLocation "$current_movie"
done
else
echo "No city value received"
exit 1
fi
exit
}
# View locations
if [ "$locationsF" == 1 ];then
printf "$format" "ID" "Country" "City" "Address"
echo "--------------------------------------------"
if [ "$theaterIDF" == 1 ];then
printLocationByID
elif [ "$cityF" == 1 ];then
printLocationByCity
elif [ "$countryF" == 1 ];then
printLocationByCountry
else
printAllLocations
fi
exit
fi