Skip to content

Commit e8c0543

Browse files
authored
Implement get_coprocessor_version (#166)
* Expose `/node/coprocessor/version` * Lint
1 parent a76affc commit e8c0543

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

python_otbr_api/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,23 @@ async def get_extended_address(self) -> bytes:
271271
return bytes.fromhex(await response.json())
272272
except ValueError as exc:
273273
raise OTBRError("unexpected API response") from exc
274+
275+
async def get_coprocessor_version(self) -> str:
276+
"""Get the coprocessor firmware version.
277+
278+
Raises if the http status is not 200 or if the response is invalid.
279+
"""
280+
281+
response = await self._session.get(
282+
f"{self._url}/node/coprocessor/version",
283+
headers={"Accept": "application/json"},
284+
timeout=aiohttp.ClientTimeout(total=self._timeout),
285+
)
286+
287+
if response.status != HTTPStatus.OK:
288+
raise OTBRError(f"unexpected http status {response.status}")
289+
290+
try:
291+
return await response.json()
292+
except ValueError as exc:
293+
raise OTBRError("unexpected API response") from exc

tests/test_init.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,20 @@ async def test_get_extended_address(aioclient_mock: AiohttpClientMocker) -> None
446446
assert await otbr.get_extended_address() == bytes.fromhex(mock_response)
447447

448448

449+
async def test_get_coprocessor_version(aioclient_mock: AiohttpClientMocker) -> None:
450+
"""Test get_coprocessor_version."""
451+
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
452+
453+
mock_response = (
454+
"OPENTHREAD/thread-reference-20200818-1740-g33cc75ed3;"
455+
" NRF52840; Jun 2 2022 14:25:49"
456+
)
457+
458+
aioclient_mock.get(f"{BASE_URL}/node/coprocessor/version", json=mock_response)
459+
460+
assert await otbr.get_coprocessor_version() == mock_response
461+
462+
449463
async def test_set_enabled_201(aioclient_mock: AiohttpClientMocker) -> None:
450464
"""Test set_enabled."""
451465
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
@@ -639,3 +653,15 @@ async def test_get_extended_address_invalid(aioclient_mock: AiohttpClientMocker)
639653
aioclient_mock.get(f"{BASE_URL}/node/ext-address", text="unexpected")
640654
with pytest.raises(python_otbr_api.OTBRError):
641655
await otbr.get_extended_address()
656+
657+
658+
async def test_get_coprocessor_version_invalid(aioclient_mock: AiohttpClientMocker):
659+
"""Test get_coprocessor_version with error."""
660+
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
661+
662+
aioclient_mock.get(
663+
f"{BASE_URL}/node/coprocessor/version", status=HTTPStatus.NOT_FOUND
664+
)
665+
666+
with pytest.raises(python_otbr_api.OTBRError):
667+
await otbr.get_coprocessor_version()

0 commit comments

Comments
 (0)