mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-11-22 14:03:32 +00:00
Paginate list responses when multi requests are disabled (#1838)
Some checks are pending
CI / Perform Lint Checks (3.15) (push) Waiting to run
CI / Python 3.11 on macos-latest (push) Blocked by required conditions
CI / Python 3.12 on macos-latest (push) Blocked by required conditions
CI / Python 3.13 on macos-latest (push) Blocked by required conditions
CI / Python 3.14 on macos-latest (push) Blocked by required conditions
CI / Python 3.15 on macos-latest (push) Blocked by required conditions
CI / Python 3.11 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.12 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.13 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.14 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.15 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.11 on windows-latest (push) Blocked by required conditions
CI / Python 3.12 on windows-latest (push) Blocked by required conditions
CI / Python 3.13 on windows-latest (push) Blocked by required conditions
CI / Python 3.14 on windows-latest (push) Blocked by required conditions
CI / Python 3.15 on windows-latest (push) Blocked by required conditions
CodeQL Checks / Analyze (python) (push) Waiting to run
Some checks are pending
CI / Perform Lint Checks (3.15) (push) Waiting to run
CI / Python 3.11 on macos-latest (push) Blocked by required conditions
CI / Python 3.12 on macos-latest (push) Blocked by required conditions
CI / Python 3.13 on macos-latest (push) Blocked by required conditions
CI / Python 3.14 on macos-latest (push) Blocked by required conditions
CI / Python 3.15 on macos-latest (push) Blocked by required conditions
CI / Python 3.11 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.12 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.13 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.14 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.15 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.11 on windows-latest (push) Blocked by required conditions
CI / Python 3.12 on windows-latest (push) Blocked by required conditions
CI / Python 3.13 on windows-latest (push) Blocked by required conditions
CI / Python 3.14 on windows-latest (push) Blocked by required conditions
CI / Python 3.15 on windows-latest (push) Blocked by required conditions
CodeQL Checks / Analyze (python) (push) Waiting to run
Drop the separate `step == 1` send loop. With batching off, the requests go through the existing loop at the end of the method, which now paginates. That loop also handles lists requeried after an error in a batch, which were truncated the same way with no timeout involved. --------- Co-authored-by: ZeliardM <140266236+ZeliardM@users.noreply.github.com>
This commit is contained in:
@@ -224,18 +224,9 @@ class SmartProtocol(BaseProtocol):
|
|||||||
# Break the requests down as there can be a size limit
|
# Break the requests down as there can be a size limit
|
||||||
step = self._multi_request_batch_size
|
step = self._multi_request_batch_size
|
||||||
if step == 1:
|
if step == 1:
|
||||||
# If step is 1 do not send request batches
|
# Batching is off, so the loop at the end sends each request on its own
|
||||||
for request in multi_requests:
|
multi_requests = []
|
||||||
method = request["method"]
|
for batch_num, i in enumerate(range(0, len(multi_requests), step)):
|
||||||
req = self.get_smart_request(method, request.get("params"))
|
|
||||||
resp = await self._transport.send(req)
|
|
||||||
self._handle_response_error_code(
|
|
||||||
resp, method, raise_on_error=raise_on_error
|
|
||||||
)
|
|
||||||
multi_result[method] = resp["result"]
|
|
||||||
return multi_result
|
|
||||||
|
|
||||||
for batch_num, i in enumerate(range(0, end, step)):
|
|
||||||
requests_step = multi_requests[i : i + step]
|
requests_step = multi_requests[i : i + step]
|
||||||
|
|
||||||
smart_params = {"requests": requests_step}
|
smart_params = {"requests": requests_step}
|
||||||
@@ -254,8 +245,7 @@ class SmartProtocol(BaseProtocol):
|
|||||||
if self.REQUIRES_MULTIREQUESTS:
|
if self.REQUIRES_MULTIREQUESTS:
|
||||||
raise
|
raise
|
||||||
# P300 does not respond to some batched requests (e.g. both child
|
# P300 does not respond to some batched requests (e.g. both child
|
||||||
# list requests together) so disable batching. Batch size is never
|
# list requests together) so disable batching.
|
||||||
# 1 here as that case sends single requests above.
|
|
||||||
self._multi_request_batch_size = 1
|
self._multi_request_batch_size = 1
|
||||||
raise _RetryableError(
|
raise _RetryableError(
|
||||||
"Timeout during multi request, multi requests disabled"
|
"Timeout during multi request, multi requests disabled"
|
||||||
@@ -328,7 +318,12 @@ class SmartProtocol(BaseProtocol):
|
|||||||
self._handle_response_error_code(
|
self._handle_response_error_code(
|
||||||
resp, method, raise_on_error=raise_on_error
|
resp, method, raise_on_error=raise_on_error
|
||||||
)
|
)
|
||||||
multi_result[method] = resp.get("result")
|
result = resp.get("result")
|
||||||
|
if iterate_list_pages and result:
|
||||||
|
await self._handle_response_lists(
|
||||||
|
result, method, params, retry_count=retry_count
|
||||||
|
)
|
||||||
|
multi_result[method] = result
|
||||||
return multi_result
|
return multi_result
|
||||||
|
|
||||||
async def _execute_query(
|
async def _execute_query(
|
||||||
|
|||||||
@@ -494,8 +494,9 @@ async def test_smart_protocol_lists_single_request(
|
|||||||
|
|
||||||
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
||||||
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
||||||
|
@pytest.mark.parametrize("multi_request_batch_size", [5, 1])
|
||||||
async def test_smart_protocol_lists_multiple_request(
|
async def test_smart_protocol_lists_multiple_request(
|
||||||
mocker: MockerFixture, list_sum: int, batch_size: int
|
mocker: MockerFixture, list_sum: int, batch_size: int, multi_request_batch_size: int
|
||||||
) -> None:
|
) -> None:
|
||||||
child_list = [{"foo": i} for i in range(list_sum)]
|
child_list = [{"foo": i} for i in range(list_sum)]
|
||||||
response = {
|
response = {
|
||||||
@@ -520,6 +521,7 @@ async def test_smart_protocol_lists_multiple_request(
|
|||||||
get_child_fixtures=False,
|
get_child_fixtures=False,
|
||||||
)
|
)
|
||||||
protocol = SmartProtocol(transport=ft)
|
protocol = SmartProtocol(transport=ft)
|
||||||
|
protocol._multi_request_batch_size = multi_request_batch_size
|
||||||
query_spy = mocker.spy(protocol, "_execute_query")
|
query_spy = mocker.spy(protocol, "_execute_query")
|
||||||
resp = await protocol.query(request)
|
resp = await protocol.query(request)
|
||||||
expected_count = 1 + 2 * (
|
expected_count = 1 + 2 * (
|
||||||
@@ -728,3 +730,39 @@ async def test_no_multiple_methods(
|
|||||||
send_spy = mocker.spy(protocol._transport, "send")
|
send_spy = mocker.spy(protocol._transport, "send")
|
||||||
await protocol.query(req)
|
await protocol.query(req)
|
||||||
assert send_spy.call_count == 2
|
assert send_spy.call_count == 2
|
||||||
|
|
||||||
|
|
||||||
|
async def test_smart_protocol_lists_requeried_after_batch_error() -> None:
|
||||||
|
"""Test that a list requeried after an error in its batch is complete."""
|
||||||
|
child_list = {
|
||||||
|
"child_device_list": [{"foo": i} for i in range(30)],
|
||||||
|
"start_index": 0,
|
||||||
|
"sum": 30,
|
||||||
|
}
|
||||||
|
ft = FakeSmartTransport(
|
||||||
|
{"get_child_device_list": child_list},
|
||||||
|
"foobar",
|
||||||
|
component_nego_not_included=True,
|
||||||
|
get_child_fixtures=False,
|
||||||
|
warn_fixture_missing_methods=False,
|
||||||
|
)
|
||||||
|
protocol = SmartProtocol(transport=ft)
|
||||||
|
resp = await protocol.query({"get_foo": None, "get_child_device_list": None})
|
||||||
|
assert resp["get_child_device_list"] == child_list
|
||||||
|
|
||||||
|
|
||||||
|
async def test_force_single_method_leaves_no_empty_batch(
|
||||||
|
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||||
|
) -> None:
|
||||||
|
"""Test that a method sent on its own does not add an empty batch."""
|
||||||
|
dummy_protocol._multi_request_batch_size = 5
|
||||||
|
methods = [f"get_method_{i}" for i in range(5)]
|
||||||
|
responses = [{"method": m, "result": {}, "error_code": 0} for m in methods]
|
||||||
|
send_mock = mocker.patch.object(
|
||||||
|
dummy_protocol._transport,
|
||||||
|
"send",
|
||||||
|
return_value={"result": {"responses": responses}, "error_code": 0},
|
||||||
|
)
|
||||||
|
|
||||||
|
await dummy_protocol.query(dict.fromkeys([*methods, "scanApList"]), retry_count=0)
|
||||||
|
assert send_mock.call_count == 2
|
||||||
|
|||||||
Reference in New Issue
Block a user