Skip to content

Commit b484498

Browse files
committed
Fix pagination of empty response for PaginationType.NONE
1 parent a8ee1a0 commit b484498

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

alpaca/common/rest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ def _return_paginated_result(
322322
"""
323323
if handle_pagination == PaginationType.NONE:
324324
# user wants no pagination, so just do a single page
325-
return next(iterator)
325+
try:
326+
return next(iterator)
327+
except StopIteration:
328+
return []
326329
elif handle_pagination == PaginationType.FULL:
327330
# the iterator returns "pages", so we use chain to flatten them all into 1 list
328331
return list(chain.from_iterable(iterator))

tests/broker/broker_client/test_rebalancing_routes.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
GetSubscriptionsRequest,
1515
UpdatePortfolioRequest,
1616
)
17-
from alpaca.common.enums import BaseURL
17+
from alpaca.common.enums import BaseURL, PaginationType
1818

1919

2020
def test_create_portfolio(reqmock: Mocker, client: BrokerClient) -> None:
@@ -403,6 +403,21 @@ def test_get_all_subscriptions(reqmock: Mocker, client: BrokerClient) -> None:
403403
assert isinstance(response[0], Subscription)
404404

405405

406+
def test_get_all_subscriptions_empty_no_pagination(reqmock: Mocker, client: BrokerClient) -> None:
407+
"""Test the get_all_subscriptions method."""
408+
reqmock.get(
409+
f"{BaseURL.BROKER_SANDBOX.value}/v1/rebalancing/subscriptions",
410+
text="""{
411+
"subscriptions": [],
412+
"next_page_token": null
413+
}""",
414+
)
415+
response = client.get_all_subscriptions(filter=GetSubscriptionsRequest(), handle_pagination=PaginationType.NONE)
416+
417+
assert reqmock.called_once
418+
assert len(response) == 0
419+
420+
406421
def test_get_subscription_by_id(reqmock: Mocker, client: BrokerClient) -> None:
407422
"""Test the get_subscription_by_id method."""
408423
sub_id = UUID("9341be15-8786-4d23-ba1a-fc10ef4f90f4")

0 commit comments

Comments
 (0)