Skip to content

Commit 703f26f

Browse files
committed
http: api: add expand to list devices
1 parent f9cc124 commit 703f26f

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/enapter/cli/http/api/device_list_command.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,41 @@ def register(parent: cli.Subparsers) -> None:
1818
help="Maximum number of devices to list",
1919
default=-1,
2020
)
21+
parser.add_argument(
22+
"-m",
23+
"--manifest",
24+
action="store_true",
25+
help="Expand device manifest information",
26+
)
27+
parser.add_argument(
28+
"-p",
29+
"--properties",
30+
action="store_true",
31+
help="Expand device properties information",
32+
)
33+
parser.add_argument(
34+
"-c",
35+
"--connectivity",
36+
action="store_true",
37+
help="Expand device connectivity information",
38+
)
39+
parser.add_argument(
40+
"--communication",
41+
action="store_true",
42+
help="Expand device communication information",
43+
)
2144

2245
@staticmethod
2346
async def run(args: argparse.Namespace) -> None:
2447
if args.limit == 0:
2548
return
2649
async with http.api.Client(http.api.Config.from_env()) as client:
27-
async with client.devices.list() as stream:
50+
async with client.devices.list(
51+
expand_manifest=args.manifest,
52+
expand_properties=args.properties,
53+
expand_connectivity=args.connectivity,
54+
expand_communication=args.communication,
55+
) as stream:
2856
count = 0
2957
async for device in stream:
3058
print(json.dumps(device.to_dto()))

src/enapter/http/api/devices/client.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, AsyncGenerator
1+
from typing import AsyncGenerator
22

33
import httpx
44

@@ -36,19 +36,32 @@ async def get(
3636
"connectivity": expand_connectivity,
3737
"communication": expand_communication,
3838
}
39-
params = {"expand": ",".join(k for k, v in expand.items() if v)}
40-
response = await self._client.get(url, params=params)
39+
expand_string = ",".join(k for k, v in expand.items() if v)
40+
response = await self._client.get(url, params={"expand": expand_string})
4141
api.check_error(response)
4242
return Device.from_dto(response.json()["device"])
4343

4444
@async_.generator
45-
async def list(self) -> AsyncGenerator[Device, None]:
45+
async def list(
46+
self,
47+
expand_manifest: bool = False,
48+
expand_properties: bool = False,
49+
expand_connectivity: bool = False,
50+
expand_communication: bool = False,
51+
) -> AsyncGenerator[Device, None]:
4652
url = "v3/devices"
53+
expand = {
54+
"manifest": expand_manifest,
55+
"properties": expand_properties,
56+
"connectivity": expand_connectivity,
57+
"communication": expand_communication,
58+
}
59+
expand_string = ",".join(k for k, v in expand.items() if v)
4760
limit = 50
4861
offset = 0
4962
while True:
5063
response = await self._client.get(
51-
url, params={"limit": limit, "offset": offset}
64+
url, params={"expand": expand_string, "limit": limit, "offset": offset}
5265
)
5366
api.check_error(response)
5467
payload = response.json()

0 commit comments

Comments
 (0)