Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

fix: add fieldMask for getDevice and listDevices #64

Merged
merged 3 commits into from Feb 5, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions samples/api-client/manager/manager.py
Expand Up @@ -279,7 +279,11 @@ def get_device(service_account_json, project_id, cloud_region, registry_id, devi
client = iot_v1.DeviceManagerClient()
device_path = client.device_path(project_id, cloud_region, registry_id, device_id)

device = client.get_device(request={"name": device_path})
# See full list of device fields: https://cloud.google.com/iot/docs/reference/cloudiot/rest/v1/projects.locations.registries.devices
# Warning! Use snake_case field names.
fieldMask = gp_field_mask.FieldMask(paths=['id', 'name', 'num_id', 'credentials', 'last_heartbeat_time', 'last_event_time', 'last_state_time', 'last_config_ack_time', 'last_config_send_time', 'blocked', 'last_error_time', 'last_error_status', 'config', 'state', 'log_level', 'metadata', 'gateway_config'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest breaking this up into separate lines to avoid linter issues.


device = client.get_device(request={"name": device_path, "field_mask": fieldMask})

print("Id : {}".format(device.id))
print("Name : {}".format(device.name))
Expand Down Expand Up @@ -345,9 +349,13 @@ def list_devices(service_account_json, project_id, cloud_region, registry_id):
client = iot_v1.DeviceManagerClient()
registry_path = client.registry_path(project_id, cloud_region, registry_id)

devices = list(client.list_devices(request={"parent": registry_path}))
# See full list of device fields: https://cloud.google.com/iot/docs/reference/cloudiot/rest/v1/projects.locations.registries.devices
# Warning! Use snake_case field names.
fieldMask = gp_field_mask.FieldMask(paths=['id', 'name', 'num_id', 'credentials', 'last_heartbeat_time', 'last_event_time', 'last_state_time', 'last_config_ack_time', 'last_config_send_time', 'blocked', 'last_error_time', 'last_error_status', 'config', 'state', 'log_level', 'metadata', 'gateway_config'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. It also improves readability (and by extension, maintainability).


devices = list(client.list_devices(request={"parent": registry_path, "field_mask": fieldMask}))
for device in devices:
print("Device: {} : {}".format(device.num_id, device.id))
print(device)

return devices
# [END iot_list_devices]
Expand Down