Skip to content

Commit 55706d7

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

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-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: 20 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,25 @@ 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(
407+
reqmock: Mocker, client: BrokerClient
408+
) -> None:
409+
"""Test the get_all_subscriptions method."""
410+
reqmock.get(
411+
f"{BaseURL.BROKER_SANDBOX.value}/v1/rebalancing/subscriptions",
412+
text="""{
413+
"subscriptions": [],
414+
"next_page_token": null
415+
}""",
416+
)
417+
response = client.get_all_subscriptions(
418+
filter=GetSubscriptionsRequest(), handle_pagination=PaginationType.NONE
419+
)
420+
421+
assert reqmock.called_once
422+
assert len(response) == 0
423+
424+
406425
def test_get_subscription_by_id(reqmock: Mocker, client: BrokerClient) -> None:
407426
"""Test the get_subscription_by_id method."""
408427
sub_id = UUID("9341be15-8786-4d23-ba1a-fc10ef4f90f4")

0 commit comments

Comments
 (0)