Skip to content

Commit 45fc6a8

Browse files
committed
tools: update cloud API response handling
Update cloud API response handling due to schema updates. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 260ad79 commit 45fc6a8

2 files changed

Lines changed: 38 additions & 29 deletions

File tree

src/infuse_iot/tools/cloud.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
__copyright__ = "Copyright 2024, Embeint Holdings Pty Ltd"
77

88
import sys
9-
from http import HTTPStatus
10-
from json import loads
119
from typing import Any
1210

1311
from tabulate import tabulate
1412

13+
import infuse_iot.api_client.models as models
1514
from infuse_iot.api_client import Client
1615
from infuse_iot.api_client.api.board import (
1716
create_board,
@@ -72,7 +71,7 @@ def list(self, client: Client):
7271
org_list = []
7372

7473
orgs = get_all_organisations.sync(client=client)
75-
if isinstance(orgs, Error) or orgs is None:
74+
if isinstance(orgs, models.Error) or orgs is None:
7675
sys.exit(f"Organisation query failed {orgs}")
7776
for o in orgs:
7877
org_list.append([o.name, o.id])
@@ -87,15 +86,15 @@ def list(self, client: Client):
8786
def create(self, client: Client):
8887
rsp = create_organisation.sync_detailed(
8988
client=client,
90-
body=NewOrganisation(self.args.name),
89+
body=models.NewOrganisation(self.args.name),
9190
)
9291

93-
if rsp.status_code == HTTPStatus.CREATED:
94-
assert rsp.parsed is not None
95-
print(f"Created organisation {rsp.parsed.name} with ID {rsp.parsed.id}")
92+
if rsp.parsed is None:
93+
print(f"<{rsp.status_code}>: {rsp.content.decode('utf-8')}")
94+
elif isinstance(rsp.parsed, models.Error):
95+
print(f"<{rsp.status_code}>: {rsp.parsed.message}")
9696
else:
97-
c = loads(rsp.content.decode("utf-8"))
98-
print(f"<{rsp.status_code}>: {c['message']}")
97+
print(f"Created organisation {rsp.parsed.name} with ID {rsp.parsed.id}")
9998

10099

101100
class Boards(CloudSubCommand):
@@ -124,11 +123,11 @@ def list(self, client: Client):
124123
board_list = []
125124

126125
orgs = get_all_organisations.sync(client=client)
127-
if isinstance(orgs, Error) or orgs is None:
126+
if isinstance(orgs, models.Error) or orgs is None:
128127
sys.exit(f"Organisation query failed {orgs}")
129128
for org in orgs:
130129
boards = get_boards.sync(client=client, organisation_id=org.id)
131-
if isinstance(boards, Error) or boards is None:
130+
if isinstance(boards, models.Error) or boards is None:
132131
sys.exit(f"Boards query failed {boards}")
133132

134133
for b in boards:
@@ -144,19 +143,20 @@ def list(self, client: Client):
144143
def create(self, client: Client):
145144
rsp = create_board.sync_detailed(
146145
client=client,
147-
body=NewBoard(
146+
body=models.NewBoard(
148147
name=self.args.name,
149148
description=self.args.desc,
150149
soc=self.args.soc,
151150
organisation_id=self.args.org,
152151
),
153152
)
154-
if rsp.status_code == HTTPStatus.CREATED:
155-
assert rsp.parsed is not None
156-
print(f"Created board {rsp.parsed.name} with ID {rsp.parsed.id}")
153+
154+
if rsp.parsed is None:
155+
print(f"<{rsp.status_code}>: {rsp.content.decode('utf-8')}")
156+
elif isinstance(rsp.parsed, models.Error):
157+
print(f"<{rsp.status_code}>: {rsp.parsed.message}")
157158
else:
158-
c = loads(rsp.content.decode("utf-8"))
159-
print(f"<{rsp.status_code}>: {c['message']}")
159+
print(f"Created board {rsp.parsed.name} with ID {rsp.parsed.id}")
160160

161161

162162
class Device(CloudSubCommand):
@@ -185,6 +185,8 @@ def info(self, client: Client):
185185
info = get_device_by_device_id.sync(client=client, device_id=id_str)
186186
if info is None:
187187
sys.exit(f"No device with Infuse-IoT ID {id_str} found")
188+
elif isinstance(info, models.Error):
189+
sys.exit(f"<{info.code}>: {info.message}")
188190
metadata: list[tuple[str, Any]] = []
189191
if info.metadata:
190192
metadata = [(f"Metadata.{k}", v) for k, v in info.metadata.additional_properties.items()]
@@ -198,13 +200,16 @@ def info(self, client: Client):
198200
table: list[tuple[str, Any]] = [
199201
("UUID", info.id),
200202
("MCU ID", info.mcu_id),
201-
("Organisation", f"{info.organisation_id} ({org.name if org else 'Unknown'})"),
202-
("Board", f"{info.board_id} ({board.name if board else 'Unknown'})"),
203+
(
204+
"Organisation",
205+
f"{info.organisation_id} ({org.name if isinstance(org, models.Organisation) else 'Unknown'})",
206+
),
207+
("Board", f"{info.board_id} ({board.name if isinstance(board, models.Board) else 'Unknown'})"),
203208
("Created", info.created_at),
204209
("Updated", info.updated_at),
205210
*metadata,
206211
]
207-
if state is not None:
212+
if isinstance(state, models.DeviceState):
208213
v = state.application_version
209214

210215
table += [
@@ -215,7 +220,7 @@ def info(self, client: Client):
215220
table += [("Application ID", f"0x{state.application_id:08x}")]
216221
if v:
217222
table += [("Version", f"{v.major}.{v.minor}.{v.revision}+{v.build_num:08x}")]
218-
if route is not None:
223+
if isinstance(route, models.UplinkRoute):
219224
table += [
220225
("~~~Latest Route~~~", ""),
221226
("Interface", route.interface.upper()),
@@ -263,7 +268,7 @@ def kv_state(self, client: Client):
263268
id_str = f"{id_int:016x}"
264269

265270
kv_state = get_device_kv_entries_by_device_id.sync(client=client, device_id=id_str)
266-
if kv_state is None:
271+
if not isinstance(kv_state, list):
267272
print(f"Unable to query KV state for {id_str}")
268273
return
269274

@@ -307,12 +312,14 @@ def run(self):
307312
def list(self, client: Client):
308313
files = get_coap_files.sync(client=client)
309314

310-
if isinstance(files, COAPFilesList):
315+
if files is None:
316+
print("Failed to retrieve file list (No response)")
317+
elif isinstance(files, models.Error):
318+
print(f"<{files.code}>: {files.message}")
319+
else:
311320
sorted_list: list[str] = sorted(files.filenames)
312321
print("CoAP Files:")
313322
print("\t" + "\n\t".join(sorted_list))
314-
else:
315-
print(f"Failed to retrieve file list {files}")
316323

317324

318325
class SubCommand(InfuseCommand):

src/infuse_iot/tools/provision.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ def run(self):
143143
response = get_device_by_soc_and_mcu_id.sync_detailed(
144144
client=client, soc=interface.soc_name, mcu_id=hardware_id_str
145145
)
146-
if response.status_code == HTTPStatus.OK:
146+
if isinstance(response.parsed, Device):
147147
# Device found, fall through
148-
assert isinstance(response.parsed, Device)
149148
assert isinstance(response.parsed.device_id, str)
150149
self._org = response.parsed.organisation_id
151150
self._board = response.parsed.board_id
@@ -166,7 +165,11 @@ def run(self):
166165
response = get_device_by_soc_and_mcu_id.sync_detailed(
167166
client=client, soc=interface.soc_name, mcu_id=hardware_id_str
168167
)
169-
if response.status_code != HTTPStatus.OK:
168+
if isinstance(response.parsed, Error):
169+
err = "Failed to query device after creation:\n"
170+
err += f"\t<{response.status_code}> {response.parsed.message}"
171+
sys.exit(err)
172+
elif response.parsed is None:
170173
err = "Failed to query device after creation:\n"
171174
err += f"\t<{response.status_code}> {response.content.decode('utf-8')}"
172175
sys.exit(err)
@@ -175,7 +178,6 @@ def run(self):
175178
err += f"\t<{response.status_code}> {response.content.decode('utf-8')}"
176179
sys.exit(err)
177180

178-
assert response.parsed is not None
179181
assert isinstance(response.parsed.device_id, str)
180182
# Compare current flash contents to desired flash contents
181183
cloud_id = int(response.parsed.device_id, 16)

0 commit comments

Comments
 (0)