-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestClient.py
270 lines (194 loc) · 8.84 KB
/
TestClient.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
from openapi_client.api import default_api
from openapi_client import api_client
from openapi_client.model.album_dto import AlbumDTO
from openapi_client.model.invoice_line_item_dto import InvoiceLineItemDTO
from openapi_client.model.user_data_dto import UserDataDTO
import uuid
def response_to_dict(data):
dictionaries = []
for entry in data:
dictionaries.append(entry.to_dict())
return dictionaries
def get_user_data():
email_address = ""
password = ""
while not email_address:
email_address = input("email: ")
while not password:
password = input("password: ")
return UserDataDTO(emailAddress=email_address, password=password)
def get_authorized_rest_service(token):
client = api_client.ApiClient()
client.set_default_header("Authorization", token)
client.set_default_header("CartUUID", str(uuid.uuid4()))
return default_api.DefaultApi(client)
# Start
print()
print("Enter your credentials")
end = False
jwt = ""
unauthorized_rest_service = default_api.DefaultApi()
# Login
while not jwt:
user_data = get_user_data()
jwt = unauthorized_rest_service.login(user_data_dto=user_data)
# On successful login -> create authorized_rest_service (with jwt in Headers)
authorized_rest_service = get_authorized_rest_service(jwt)
print()
print("Welcome to our music shop :)")
while not end:
print()
print("<Music Overview>")
print("Available commands: [s] Music search, [c] Display shopping cart, [q] Quit")
command_valid = False
while not command_valid:
command = input("Enter command: ").lower()
# Music Search
if command == "s":
back = False
while not back:
print()
print("<Music Search>")
song_title = input("Enter song title: ")
print("Searching for albums containing a song with title '" + song_title.upper() + "' ...")
response = authorized_rest_service.find_albums_by_song_title_physical(song_title)
albums = response_to_dict(response)
album_count = 1
for album in albums:
print()
print("ALBUM " + str(album_count))
print("Title: " + album.get('title'))
print("Medium: " + album.get('medium_type'))
print("Price: " + str(album.get('price')) + " €")
print("Stock: " + str(album.get('stock')))
print()
print("SONGS OF ALBUM " + str(album_count))
song_count = 1
for song in album['songs']:
print('#' + str(song_count) + ' ' + song['title'])
for artist in song['artists']:
print(artist['name'])
song_count += 1
album_count += 1
print()
print("Available commands: [a] Add album to shopping cart, [s] New music search, [b] Back, [q] Quit")
command_valid = False
while not command_valid:
command = input("Enter command: ").lower()
# New music search
if command == "s":
print("Initiating new music search...")
command_valid = True
# Add album(s) to shopping cart
elif command == "a":
album_number = input("Enter album number: ")
quantity = input("Enter quantity: ")
album = albums[int(album_number) - 1]
req = AlbumDTO(
title=album.get('title'),
mediumType=album.get('medium_type'),
price=album.get('price'),
stock=album.get('stock'),
quantityToAddToCart=quantity,
songs=album.get('songs'),
imageUrl=""
)
print("Adding ALBUM " + album_number + " to shopping cart ...")
authorized_rest_service.add_albums_to_cart(album_dto=req)
# add search result to cart
print()
print("ALBUM " + album_number)
print("Title: " + album.get('title'))
print("Medium: " + album.get('medium_type'))
print("Quantity: " + quantity)
print("Added to cart.")
print("Back to music shop overview ...")
back = True
command_valid = True
# Back to music shop overview or stop client
elif command == "b":
print("Back to music shop overview ...")
back = True
command_valid = True
# Quit
elif command == "q":
back = True
command_valid = True
# Unknown command
else:
print("Unknown command")
command_valid = False
# Shopping Cart
elif command == "c":
back = False
while not back:
print()
print("<Shopping Cart>")
print("Displaying shopping cart items ...")
response = authorized_rest_service.display_shopping_cart()
items = response_to_dict(response['cart_line_items'])
if items:
item_count = 1
for item in items:
print()
print("ITEM " + str(item_count))
print("Title: " + item.get('name'))
print("Medium: " + item.get('medium_type'))
print("Price: " + str(item.get('price')) + " €")
print("Quantity: " + str(item.get('quantity')))
item_count += 1
print()
print("Available commands: [p] Purchase line items, [c] Clear shopping cart, [b] Back, [q] Quit")
command_valid = False
while not command_valid:
command = input("Enter command: ").lower()
# Purchase Line Item(s)
if command == "p":
print("Purchasing items ...")
invoice_line_items = []
for item in items:
invoice_line_item = InvoiceLineItemDTO(
name=item.get('name'),
mediumType=item.get('medium_type'),
quantity=item.get('quantity'),
price=item.get('price'),
returnedQuantity=0
)
invoice_line_items.append(invoice_line_item)
authorized_rest_service.buy_product(invoice_line_item_dto=invoice_line_items)
authorized_rest_service.clear_shopping_cart()
print("Items purchased and shopping cart cleared.")
command_valid = True
# Clear Shopping Cart
elif command == "c":
print("Clearing shopping cart ...")
authorized_rest_service.clear_shopping_cart()
command_valid = True
# Back to music shop overview or stop client
elif command == "b":
back = True
print("Back to music shop overview ...")
command_valid = True
# Quit
elif command == "q":
back = True
command_valid = True
else:
print("Unknown command")
command_valid = False
else:
print("No items in shopping cart found. Back to music shop overview ...")
back = True
command_valid = True
# Quit
elif command == "q":
print("Stopping python test client ...")
end = True
command_valid = True
# Unknown command
else:
print("Unknown command")
command_valid = False
# End
print()
print("Bye! Have a nice day :)")