Skip to content

Commit 4b861d6

Browse files
authored
Image list (#9)
* pydantic: fix deprecated method I was getting this message: client.py:198: PydanticDeprecatedSince20: The `parse_obj` method is deprecated; use `model_validate` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/ * images: add get_images method
1 parent 9fc99fb commit 4b861d6

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

nanokvm/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
GetHardwareRsp,
2929
GetHdmiStateRsp,
3030
GetHidModeRsp,
31+
GetImagesRsp,
3132
GetInfoRsp,
3233
GetMdnsStateRsp,
3334
GetMemoryLimitRsp,
@@ -194,7 +195,7 @@ async def _api_request_json(
194195
raw_response = await response.json(content_type=None)
195196
_LOGGER.debug("Raw JSON response data: %s", raw_response)
196197
# Parse the outer ApiResponse structure
197-
api_response = ApiResponse[response_model].parse_obj(raw_response) # type: ignore
198+
api_response = ApiResponse[response_model].model_validate(raw_response) # type: ignore
198199
except (json.JSONDecodeError, ValidationError) as err:
199200
raise NanoKVMInvalidResponseError(
200201
f"Invalid JSON response received: {err}"
@@ -397,6 +398,14 @@ async def get_tailscale_status(self) -> GetTailscaleStatusRsp:
397398
response_model=GetTailscaleStatusRsp,
398399
)
399400

401+
async def get_images(self) -> GetImagesRsp:
402+
"""Get the list of available image files."""
403+
return await self._api_request_json(
404+
hdrs.METH_GET,
405+
"/storage/image",
406+
response_model=GetImagesRsp,
407+
)
408+
400409
async def get_mounted_image(self) -> GetMountedImageRsp:
401410
"""Get the currently mounted image file."""
402411
return await self._api_request_json(

tests/test_client.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,82 @@
11
from aiohttp import ClientSession
2+
from aioresponses import aioresponses
23

3-
from nanokvm.client import NanoKVMClient
4+
from nanokvm.client import NanoKVMApiError, NanoKVMClient
5+
from nanokvm.models import ApiResponseCode
46

57

68
async def test_client() -> None:
79
"""Test the NanoKVMClient."""
810
async with ClientSession() as session:
911
client = NanoKVMClient("http://localhost:8888/api/", session)
1012
assert client is not None
13+
14+
15+
async def test_get_images_success() -> None:
16+
"""Test get_images with a successful response."""
17+
async with ClientSession() as session:
18+
client = NanoKVMClient(
19+
"http://localhost:8888/api/", session, token="test-token"
20+
)
21+
22+
with aioresponses() as m:
23+
m.get(
24+
"http://localhost:8888/api/storage/image",
25+
payload={
26+
"code": 0,
27+
"msg": "success",
28+
"data": {
29+
"files": [
30+
"/data/alpine-standard-3.23.2-x86_64.iso",
31+
"/data/cs10-js.iso",
32+
]
33+
},
34+
},
35+
)
36+
37+
response = await client.get_images()
38+
39+
assert response is not None
40+
assert len(response.files) == 2
41+
assert "/data/alpine-standard-3.23.2-x86_64.iso" in response.files
42+
assert "/data/cs10-js.iso" in response.files
43+
44+
45+
async def test_get_images_empty() -> None:
46+
"""Test get_images with an empty list."""
47+
async with ClientSession() as session:
48+
client = NanoKVMClient(
49+
"http://localhost:8888/api/", session, token="test-token"
50+
)
51+
52+
with aioresponses() as m:
53+
m.get(
54+
"http://localhost:8888/api/storage/image",
55+
payload={"code": 0, "msg": "success", "data": {"files": []}},
56+
)
57+
58+
response = await client.get_images()
59+
60+
assert response is not None
61+
assert len(response.files) == 0
62+
63+
64+
async def test_get_images_api_error() -> None:
65+
"""Test get_images with an API error response."""
66+
async with ClientSession() as session:
67+
client = NanoKVMClient(
68+
"http://localhost:8888/api/", session, token="test-token"
69+
)
70+
71+
with aioresponses() as m:
72+
m.get(
73+
"http://localhost:8888/api/storage/image",
74+
payload={"code": -1, "msg": "failed to list images", "data": None},
75+
)
76+
77+
try:
78+
await client.get_images()
79+
raise AssertionError("Expected NanoKVMApiError to be raised")
80+
except NanoKVMApiError as e:
81+
assert e.code == ApiResponseCode.FAILURE
82+
assert "failed to list images" in e.msg

0 commit comments

Comments
 (0)