diff --git a/docs/cli/cli-subscriptions.md b/docs/cli/cli-subscriptions.md index 5c822d5da..f8dab7b59 100644 --- a/docs/cli/cli-subscriptions.md +++ b/docs/cli/cli-subscriptions.md @@ -128,7 +128,7 @@ planet subscriptions list ``` outputs the JSON for your first 100 subscriptions. If you'd like more you can set the `--limit` -parameter higher, or you can set it to 0 and there will be no limit. +parameter higher, or you can set it to 0 and there will be no limit. By default the `list` command will request Subscriptions API with a page size of 500 subscriptions, and can be set lower or higher with the `--page-size` parameter. You can get nicer formatting with `--pretty` or pipe it into `jq`, just like the other Planet CLI’s. diff --git a/planet/cli/subscriptions.py b/planet/cli/subscriptions.py index d911ad2df..a50f18ef2 100644 --- a/planet/cli/subscriptions.py +++ b/planet/cli/subscriptions.py @@ -118,6 +118,9 @@ def subscriptions(ctx, base_url): documentation for examples.""") @limit +@click.option('--page-size', + type=click.INT, + help='Number of subscriptions to return per page.') @click.pass_context @translate_exceptions @coro @@ -133,6 +136,7 @@ async def list_subscriptions_cmd(ctx, sort_by, updated, limit, + page_size, pretty): """Prints a sequence of JSON-encoded Subscription descriptions.""" async with subscriptions_client(ctx) as client: @@ -147,7 +151,8 @@ async def list_subscriptions_cmd(ctx, status=status, sort_by=sort_by, updated=updated, - limit=limit): + limit=limit, + page_size=page_size): echo_json(sub, pretty) diff --git a/planet/clients/subscriptions.py b/planet/clients/subscriptions.py index 64d0d031c..ffce14233 100644 --- a/planet/clients/subscriptions.py +++ b/planet/clients/subscriptions.py @@ -1,7 +1,7 @@ """Planet Subscriptions API Python client.""" import logging -from typing import AsyncIterator, Awaitable, Dict, Optional, Sequence, TypeVar, Union +from typing import Any, AsyncIterator, Awaitable, Dict, Optional, Sequence, TypeVar from typing_extensions import Literal @@ -65,19 +65,19 @@ def _call_sync(self, f: Awaitable[T]) -> T: """block on an async function call, using the call_sync method of the session""" return self._session._call_sync(f) - async def list_subscriptions( - self, - status: Optional[Sequence[str]] = None, - limit: int = 100, - created: Optional[str] = None, - end_time: Optional[str] = None, - hosting: Optional[bool] = None, - name__contains: Optional[str] = None, - name: Optional[str] = None, - source_type: Optional[str] = None, - start_time: Optional[str] = None, - sort_by: Optional[str] = None, - updated: Optional[str] = None) -> AsyncIterator[dict]: + async def list_subscriptions(self, + status: Optional[Sequence[str]] = None, + limit: int = 100, + created: Optional[str] = None, + end_time: Optional[str] = None, + hosting: Optional[bool] = None, + name__contains: Optional[str] = None, + name: Optional[str] = None, + source_type: Optional[str] = None, + start_time: Optional[str] = None, + sort_by: Optional[str] = None, + updated: Optional[str] = None, + page_size: int = 500) -> AsyncIterator[dict]: """Iterate over list of account subscriptions with optional filtering. Note: @@ -112,6 +112,7 @@ async def list_subscriptions( updated (str): filter by updated time or interval. limit (int): limit the number of subscriptions in the results. When set to 0, no maximum is applied. + page_size (int): number of subscriptions to return per page. TODO: user_id Datetime args (created, end_time, start_time, updated) can either be a @@ -135,7 +136,7 @@ class _SubscriptionsPager(Paged): """Navigates pages of messages about subscriptions.""" ITEMS_KEY = 'subscriptions' - params: Dict[str, Union[str, Sequence[str], bool]] = {} + params: Dict[str, Any] = {} if created is not None: params['created'] = created if end_time is not None: @@ -157,6 +158,8 @@ class _SubscriptionsPager(Paged): if updated is not None: params['updated'] = updated + params['page_size'] = page_size + try: response = await self._session.request(method='GET', url=self._base_url, diff --git a/planet/sync/subscriptions.py b/planet/sync/subscriptions.py index 64153e2f8..edab3580b 100644 --- a/planet/sync/subscriptions.py +++ b/planet/sync/subscriptions.py @@ -45,7 +45,8 @@ def list_subscriptions(self, source_type: Optional[str] = None, start_time: Optional[str] = None, sort_by: Optional[str] = None, - updated: Optional[str] = None) -> Iterator[dict]: + updated: Optional[str] = None, + page_size: int = 500) -> Iterator[dict]: """Iterate over list of account subscriptions with optional filtering. Note: @@ -80,6 +81,7 @@ def list_subscriptions(self, updated (str): filter by updated time or interval. limit (int): limit the number of subscriptions in the results. When set to 0, no maximum is applied. + page_size (int): number of subscriptions to return per page. TODO: user_id Datetime args (created, end_time, start_time, updated) can either be a @@ -109,7 +111,8 @@ def list_subscriptions(self, source_type, start_time, sort_by, - updated) + updated, + page_size) try: while True: diff --git a/tests/integration/test_subscriptions_api.py b/tests/integration/test_subscriptions_api.py index 290ffcb44..6f17cddaa 100644 --- a/tests/integration/test_subscriptions_api.py +++ b/tests/integration/test_subscriptions_api.py @@ -267,6 +267,17 @@ async def test_list_subscriptions_filtering_and_sorting(): ]) == 2 +@pytest.mark.parametrize("page_size, count", [(50, 100), (100, 100)]) +@pytest.mark.anyio +@api_mock +async def test_list_subscriptions_page_size_success(page_size, count): + async with Session() as session: + client = SubscriptionsClient(session, base_url=TEST_URL) + assert len([ + sub async for sub in client.list_subscriptions(page_size=page_size) + ]) == count + + @pytest.mark.anyio @failing_api_mock async def test_create_subscription_failure():