This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopping_list_test_client.py
executable file
·203 lines (163 loc) · 6.64 KB
/
shopping_list_test_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
#!/usr/bin/env python
"""Shopping list gRPC test client."""
import click
import grpc
import sys
import time
import shopping_list_pb2
import shopping_list_pb2_grpc
def read_certificates(certificates):
"""Read certificates from file. """
with open(certificates, 'rb') as f:
certificates = f.read()
return certificates
def create_shopping_list(stub, name, description=None):
"""Convenience function for RPC to create a shopping list."""
shopping_list = shopping_list_pb2.ShoppingList(
name=name,
description=description
)
request = shopping_list_pb2.CreateShoppingListRequest(
shopping_list=shopping_list
)
response = stub.CreateShoppingList(request)
return response
def delete_shopping_list(stub, shopping_list):
"""Convenience function for RPC to delete a shopping list."""
request = shopping_list_pb2.DeleteShoppingListRequest(
shopping_list_id=shopping_list.id
)
response = stub.DeleteShoppingList(request)
return response
def list_shopping_lists(stub, filter=None):
"""Convenience function for RPC to list shopping lists."""
request = shopping_list_pb2.ListShoppingListsStreamRequest(
filter=filter
)
return stub.ListShoppingListsStream(request)
def add_item(stub, shopping_list, name, description=None):
"""Convenience function for RPC to add an item to a shopping list."""
item = shopping_list_pb2.Item(
name=name,
description=description
)
request = shopping_list_pb2.AddItemRequest(
shopping_list_id=shopping_list.id,
item=item
)
response = stub.AddItem(request)
return response.item
def remove_item(stub, shopping_list, item):
"""Convenience function for RPC to remove an item from a shopping list."""
request = shopping_list_pb2.RemoveItemRequest(
shopping_list_id=shopping_list.id,
item_id=item.id
)
response = stub.RemoveItem(request)
return response
def set_item_checked(stub, shopping_list, item, checked):
"""Convenience function for RPC to set whether an item is checked."""
request = shopping_list_pb2.SetItemCheckedRequest(
shopping_list_id=shopping_list.id,
item_id=item.id,
checked=checked
)
response = stub.SetItemChecked(request)
return response
def list_items(stub, shopping_list, filter=None):
"""Convenience function for RPC to list items in a shopping list."""
request = shopping_list_pb2.ListItemsStreamRequest(
shopping_list_id=shopping_list.id,
filter=filter
)
return stub.ListItemsStream(request)
def pretty_print_item(item):
"""Pretty-print an item, including its checked status."""
print(' [{}] {}'.format(
'x' if item.checked else ' ',
item.name))
@click.command()
@click.option('--host', default='localhost', help='Host to connect to')
@click.option('--port', default=8443, help='Port on host to connect to')
@click.option('--use-tls/--no-use-tls', default=True, help='Use transport layer security (TLS)')
@click.option('--certificates', default='server.crt', help='Certificate file')
def main(host, port, use_tls, certificates):
if use_tls:
try:
certificates = read_certificates(certificates)
except IOError as e:
print('Failed to read certificate: {}'.format(e))
sys.exit(1)
credentials = grpc.ssl_channel_credentials(root_certificates=certificates)
channel = grpc.secure_channel('{}:{}'.format(host, port), credentials)
else:
channel = grpc.insecure_channel('{}:{}'.format(host, port))
stub = shopping_list_pb2_grpc.ShoppingListServiceStub(channel)
try:
# Print existing shopping lists
print('Existing shopping lists: ')
for shopping_list in list_shopping_lists(stub):
num_items = len(list(list_items(stub, shopping_list)))
print(' {} ({} items)'.format(shopping_list.name, num_items))
else:
print(' (none)')
# Create a new shopping list
name = 'Groceries-' + str(int(time.time()))
print('Creating shopping list "{}"'.format(name))
groceries = create_shopping_list(stub, name, 'Items for dinner and snacks')
# Add items to shopping list
print('Adding items...')
bread = add_item(stub, groceries, 'Bread')
butter = add_item(stub, groceries, 'Butter')
cheese = add_item(stub, groceries, 'Cheese')
tofu = add_item(stub, groceries, 'Tofu')
# Check some items
print('Checking some items...')
set_item_checked(stub, groceries, bread, True)
set_item_checked(stub, groceries, butter, True)
set_item_checked(stub, groceries, butter, False)
set_item_checked(stub, groceries, cheese, True)
# List items in the shopping list
print('Items:')
for item in list_items(stub, groceries, filter=None):
pretty_print_item(item)
# Remove some items
print('Removing some items...')
remove_item(stub, groceries, bread)
remove_item(stub, groceries, tofu)
# List items in the shopping list
print('Items:')
for item in list_items(stub, groceries, filter=None):
pretty_print_item(item)
# Create a new shopping list and add some items
name = 'Pet Supplies-' + str(int(time.time()))
print('Creating shopping list "{}"'.format(name))
pet_supplies = create_shopping_list(stub, name)
print('Adding items...')
add_item(stub, pet_supplies, 'Poop bags')
dog_food = add_item(stub, pet_supplies, 'Dog food')
set_item_checked(stub, pet_supplies, dog_food, False)
set_item_checked(stub, pet_supplies, dog_food, True)
print('Items:')
for item in list_items(stub, pet_supplies, filter=None):
pretty_print_item(item)
# Delete shopping lists
print('Deleting shopping lists...')
delete_shopping_list(stub, groceries)
delete_shopping_list(stub, pet_supplies)
# Print existing shopping lists
print('Existing shopping lists: ')
for shopping_list in list_shopping_lists(stub):
num_items = len(list(list_items(stub, shopping_list)))
print(' {} ({} items)'.format(shopping_list.name, num_items))
else:
print(' (none)')
print('Done')
except grpc.RpcError as rpc_error_call:
# Exception object is also a grpc.Call.
# See https://github.com/grpc/grpc/issues/9270#issuecomment-398796613
code = rpc_error_call.code()
details = rpc_error_call.details()
print(code, details)
if __name__ == '__main__':
main()