diff --git a/alpaca/common/rest.py b/alpaca/common/rest.py index a42662fb..eede4925 100644 --- a/alpaca/common/rest.py +++ b/alpaca/common/rest.py @@ -322,7 +322,10 @@ def _return_paginated_result( """ if handle_pagination == PaginationType.NONE: # user wants no pagination, so just do a single page - return next(iterator) + try: + return next(iterator) + except StopIteration: + return [] elif handle_pagination == PaginationType.FULL: # the iterator returns "pages", so we use chain to flatten them all into 1 list return list(chain.from_iterable(iterator)) diff --git a/tests/broker/broker_client/test_rebalancing_routes.py b/tests/broker/broker_client/test_rebalancing_routes.py index af9da783..a4314dd3 100644 --- a/tests/broker/broker_client/test_rebalancing_routes.py +++ b/tests/broker/broker_client/test_rebalancing_routes.py @@ -14,7 +14,7 @@ GetSubscriptionsRequest, UpdatePortfolioRequest, ) -from alpaca.common.enums import BaseURL +from alpaca.common.enums import BaseURL, PaginationType def test_create_portfolio(reqmock: Mocker, client: BrokerClient) -> None: @@ -403,6 +403,25 @@ def test_get_all_subscriptions(reqmock: Mocker, client: BrokerClient) -> None: assert isinstance(response[0], Subscription) +def test_get_all_subscriptions_empty_no_pagination( + reqmock: Mocker, client: BrokerClient +) -> None: + """Test the get_all_subscriptions method.""" + reqmock.get( + f"{BaseURL.BROKER_SANDBOX.value}/v1/rebalancing/subscriptions", + text="""{ + "subscriptions": [], + "next_page_token": null + }""", + ) + response = client.get_all_subscriptions( + filter=GetSubscriptionsRequest(), handle_pagination=PaginationType.NONE + ) + + assert reqmock.called_once + assert len(response) == 0 + + def test_get_subscription_by_id(reqmock: Mocker, client: BrokerClient) -> None: """Test the get_subscription_by_id method.""" sub_id = UUID("9341be15-8786-4d23-ba1a-fc10ef4f90f4")