15
15
"""Sample that implements device registration for the Google Assistant API."""
16
16
17
17
import json
18
+ import logging
18
19
import os
19
20
20
21
import click
@@ -40,6 +41,27 @@ def failed_request_exception(message, r):
40
41
r .text ))
41
42
42
43
44
+ # Prints out a device model in the terminal by parsing dict
45
+ def pretty_print_model (devicemodel ):
46
+ PRETTY_PRINT_MODEL = """Device Model Id: %(deviceModelId)s
47
+ Project Id: %(projectId)s
48
+ Device Type: %(deviceType)s"""
49
+ logging .info (PRETTY_PRINT_MODEL % devicemodel )
50
+ for trait in devicemodel ['traits' ]:
51
+ logging .info (' Trait %s' % trait )
52
+ logging .info ('' ) # Newline
53
+
54
+
55
+ # Prints out a device instance in the terminal by parsing dict
56
+ def pretty_print_device (device ):
57
+ logging .info ('Device Instance Id: %s' % device ['id' ])
58
+ if 'nickname' in device :
59
+ logging .info (' Nickname: %s' % device ['nickname' ])
60
+ if 'modelId' in device :
61
+ logging .info (' Model: %s' % device ['modelId' ])
62
+ logging .info ('' ) # Newline
63
+
64
+
43
65
@click .group ()
44
66
@click .option ('--project' ,
45
67
help = 'Enter the Google Developer Project ID that you want to '
@@ -56,6 +78,8 @@ def failed_request_exception(message, r):
56
78
'tool will look for this file in the current directory (by '
57
79
'searching for a file named after the client_id stored in the '
58
80
'credentials file).' )
81
+ @click .option ('--verbose' , flag_value = True ,
82
+ help = 'Shows detailed JSON response' )
59
83
@click .option ('--api-endpoint' , default = 'embeddedassistant.googleapis.com' ,
60
84
show_default = True ,
61
85
help = 'Hostname for the Google Assistant API. Do not use this '
@@ -70,7 +94,7 @@ def failed_request_exception(message, r):
70
94
'API. You can use this flag if the credentials were generated '
71
95
'in a location that is different than the default.' )
72
96
@click .pass_context
73
- def cli (ctx , project , client_secret , api_endpoint , credentials ):
97
+ def cli (ctx , project , client_secret , verbose , api_endpoint , credentials ):
74
98
try :
75
99
with open (credentials , 'r' ) as f :
76
100
c = google .oauth2 .credentials .Credentials (token = None ,
@@ -90,16 +114,18 @@ def cli(ctx, project, client_secret, api_endpoint, credentials):
90
114
project = secret ['installed' ]['project_id' ]
91
115
except Exception as e :
92
116
raise click .ClickException ('Error loading client secret: %s.\n '
93
- 'Run the register tool'
117
+ 'Run the register tool '
94
118
'with --client-secret '
95
119
'or --project option.\n '
96
- 'Or copy the %s file'
120
+ 'Or copy the %s file '
97
121
'in the current directory.'
98
122
% (e , client_secret ))
99
123
ctx .obj ['SESSION' ] = google .auth .transport .requests .AuthorizedSession (c )
100
124
ctx .obj ['API_URL' ] = ('https://%s/v1alpha2/projects/%s'
101
125
% (api_endpoint , project ))
102
126
ctx .obj ['PROJECT_ID' ] = project
127
+ logging .basicConfig (format = '' ,
128
+ level = logging .DEBUG if verbose else logging .INFO )
103
129
104
130
105
131
@cli .command ()
@@ -204,18 +230,20 @@ def register_model(ctx, model, type, trait,
204
230
payload .setdefault ('manifest' , {})['productName' ] = product_name
205
231
if description :
206
232
payload .setdefault ('manifest' , {})['deviceDescription' ] = description
233
+ logging .debug (json .dumps (payload ))
207
234
r = session .get (model_url )
235
+ logging .debug (r .text )
208
236
if r .status_code == 200 :
209
- click .echo ('updating existing device model: %s' % model )
237
+ click .echo ('Updating existing device model: %s' % model )
210
238
r = session .put (model_url , data = json .dumps (payload ))
211
239
elif r .status_code in (400 , 404 ):
212
- click .echo ('creating new device model' )
240
+ click .echo ('Creating new device model' )
213
241
r = session .post (model_base_url , data = json .dumps (payload ))
214
242
else :
215
- raise failed_request_exception ('failed to check existing model ' , r )
243
+ raise failed_request_exception ('Unknown error occurred ' , r )
216
244
if r .status_code != 200 :
217
- raise failed_request_exception ('failed to register model' , r )
218
- click .echo (r . text )
245
+ raise failed_request_exception ('Failed to register model' , r )
246
+ click .echo ('Model %s successfully registered' % model )
219
247
220
248
221
249
@cli .command ('register-device' )
@@ -248,19 +276,21 @@ def register_device(ctx, device, model, nickname):
248
276
if nickname :
249
277
payload ['nickname' ] = nickname
250
278
279
+ logging .debug (json .dumps (payload ))
251
280
r = session .get (device_url )
252
281
if r .status_code == 200 :
253
- click .echo ('updating existing device: %s' % device )
282
+ click .echo ('Updating existing device: %s' % device )
254
283
session .delete (device_url )
255
284
r = session .post (device_base_url , data = json .dumps (payload ))
256
285
elif r .status_code in (400 , 404 ):
257
- click .echo ('creating new device' )
286
+ click .echo ('Creating new device' )
258
287
r = session .post (device_base_url , data = json .dumps (payload ))
259
288
else :
260
- raise failed_request_exception ('failed to check existing device' , r )
289
+ raise failed_request_exception ('Failed to check existing device' , r )
261
290
if r .status_code != 200 :
262
- raise failed_request_exception ('failed to register device' , r )
263
- click .echo (r .text )
291
+ raise failed_request_exception ('Failed to register device' , r )
292
+ click .echo ('Device instance %s successfully registered' % device )
293
+ logging .debug (r .text )
264
294
265
295
266
296
@cli .command ()
@@ -275,11 +305,18 @@ def get(ctx, resource, id):
275
305
instance.
276
306
"""
277
307
session = ctx .obj ['SESSION' ]
308
+
278
309
url = '/' .join ([ctx .obj ['API_URL' ], resource , id ])
279
310
r = session .get (url )
280
311
if r .status_code != 200 :
281
- raise failed_request_exception ('failed to get resource' , r )
282
- click .echo (r .text )
312
+ raise failed_request_exception ('Failed to get resource' , r )
313
+
314
+ response = json .loads (r .text )
315
+ if resource == 'deviceModels' :
316
+ pretty_print_model (response )
317
+ elif resource == 'devices' :
318
+ pretty_print_device (response )
319
+ logging .debug (r .text )
283
320
284
321
285
322
@cli .command ()
@@ -292,11 +329,20 @@ def list(ctx, resource):
292
329
devicetool's --project flag.
293
330
"""
294
331
session = ctx .obj ['SESSION' ]
332
+
295
333
url = '/' .join ([ctx .obj ['API_URL' ], resource ])
296
334
r = session .get (url )
297
335
if r .status_code != 200 :
298
- raise failed_request_exception ('failed to list resources' , r )
299
- click .echo (r .text )
336
+ raise failed_request_exception ('Failed to list resources' , r )
337
+
338
+ response = json .loads (r .text )
339
+ logging .debug (r .text )
340
+ if resource == 'deviceModels' :
341
+ for devicemodel in response ['deviceModels' ]:
342
+ pretty_print_model (devicemodel )
343
+ elif resource == 'devices' :
344
+ for device in response ['devices' ]:
345
+ pretty_print_device (device )
300
346
301
347
302
348
def main ():
0 commit comments