Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 998e969

Browse files
committed
Migrate devices.get, add error handling assertion
1 parent 3de743d commit 998e969

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

seamapi/devices.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,12 @@ def get(
129129
params["device_id"] = to_device_id(device)
130130
if name:
131131
params["name"] = name
132-
res = requests.get(
133-
f"{self.seam.api_url}/devices/get",
134-
headers={"Authorization": f"Bearer {self.seam.api_key}"},
135-
params=params,
132+
res = self.seam.make_request(
133+
"GET",
134+
"/devices/get",
135+
params=params
136136
)
137-
if not res.ok:
138-
raise Exception(res.text)
139-
json_device = res.json()["device"]
137+
json_device = res["device"]
140138
return Device.from_dict(json_device)
141139

142140
def update(
@@ -178,7 +176,7 @@ def update(
178176
"properties": properties,
179177
"location": location,
180178
}
181-
179+
182180
res = requests.post(
183181
f"{self.seam.api_url}/devices/update",
184182
headers={"Authorization": f"Bearer {self.seam.api_key}"},
@@ -188,7 +186,7 @@ def update(
188186
raise Exception(res.text)
189187

190188
return True
191-
189+
192190
def delete(self, device: Union[DeviceId, Device]) -> bool:
193191
"""Deletes a device.
194192
@@ -219,4 +217,4 @@ def delete(self, device: Union[DeviceId, Device]) -> bool:
219217
if not res.ok:
220218
raise Exception(res.text)
221219

222-
return None
220+
return None

tests/devices/test_devices.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from seamapi import Seam
2+
from seamapi.types import SeamAPIException
23
from tests.fixtures.run_august_factory import run_august_factory
34
from seamapi.utils.deep_attr_dict import DeepAttrDict
45

@@ -33,4 +34,13 @@ def test_devices(seam: Seam):
3334
assert some_updated_lock.properties.name == "Updated lock"
3435

3536
seam.devices.delete(device=(some_updated_lock))
36-
assert len(seam.devices.list()) == len(devices) - 1
37+
assert len(seam.devices.list()) == len(devices) - 1
38+
39+
# Test custom exception
40+
try:
41+
seam.devices.get(name="foo")
42+
assert False
43+
except SeamAPIException as error:
44+
assert error.status_code == 404
45+
assert type(error.request_id) == str
46+
assert error.metadata["type"] == "device_not_found"

0 commit comments

Comments
 (0)