Skip to content

Commit

Permalink
Add source_type parameter to list subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed Oct 30, 2024
1 parent efcb023 commit 5cd706e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
11 changes: 9 additions & 2 deletions planet/cli/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,21 @@ def subscriptions(ctx, base_url):
multiple=True,
default=None,
help="Select subscriptions in one or more states. Default is all.")
@click.option(
'--source-type',
default=None,
help="Filter subscriptions by source type. See documentation for all "
"available types. Default is all.")
@limit
@click.pass_context
@translate_exceptions
@coro
async def list_subscriptions_cmd(ctx, status, limit, pretty):
async def list_subscriptions_cmd(ctx, status, source_type, limit, pretty):
"""Prints a sequence of JSON-encoded Subscription descriptions."""
async with subscriptions_client(ctx) as client:
async for sub in client.list_subscriptions(status=status, limit=limit):
async for sub in client.list_subscriptions(status=status,
source_type=source_type,
limit=limit):
echo_json(sub, pretty)


Expand Down
6 changes: 5 additions & 1 deletion planet/clients/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(self,

async def list_subscriptions(self,
status: Optional[Sequence[str]] = None,
source_type: Optional[str] = None,
limit: int = 100) -> AsyncIterator[dict]:
"""Iterate over list of account subscriptions with optional filtering.
Expand All @@ -73,6 +74,7 @@ async def list_subscriptions(self,
status (Set[str]): pass subscriptions with status in this
set, filter out subscriptions with status not in this
set.
source_type (str): Product source type filter, to return only subscriptions with the matching source type
limit (int): limit the number of subscriptions in the
results. When set to 0, no maximum is applied.
TODO: user_id
Expand All @@ -89,7 +91,9 @@ class _SubscriptionsPager(Paged):
"""Navigates pages of messages about subscriptions."""
ITEMS_KEY = 'subscriptions'

params = {'status': [val for val in status or {}]}
params = {'status': [val for val in status or {}]} # type: ignore
if source_type is not None:
params['source_type'] = source_type

try:
response = await self._session.request(method='GET',
Expand Down
33 changes: 32 additions & 1 deletion tests/integration/test_subscriptions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,20 @@ def result_pages(status=None, size=40):
M(params__contains={'status': 'preparing'})).mock(
side_effect=[Response(200, json={'subscriptions': []})])

# 4. No status requested. Response is the same as for 1.
# 4. source_type: catalog requested. Response is the same as for 1.
api_mock.route(
M(url=TEST_URL),
M(params__contains={'source_type': 'catalog'})).mock(side_effect=[
Response(200, json=page)
for page in result_pages(status={'running'}, size=40)
])

# 5. source_type: soil_water_content requested. Response has a single empty page.
api_mock.route(M(url=TEST_URL),
M(params__contains={'source_type': 'soil_water_content'})).mock(
side_effect=[Response(200, json={'subscriptions': []})])

# 6. No status or source_type requested. Response is the same as for 1.
api_mock.route(M(url=TEST_URL)).mock(
side_effect=[Response(200, json=page) for page in result_pages(size=40)])

Expand Down Expand Up @@ -182,6 +195,24 @@ async def test_list_subscriptions_success(
]) == count


@pytest.mark.parametrize("source_type, count",
[("catalog", 100), ("soil_water_content", 0),
(None, 100)])
@pytest.mark.anyio
@api_mock
async def test_list_subscriptions_source_type_success(
source_type,
count,
):
"""Account subscriptions iterator yields expected descriptions."""
async with Session() as session:
client = SubscriptionsClient(session, base_url=TEST_URL)
assert len([
sub
async for sub in client.list_subscriptions(source_type=source_type)
]) == count


@pytest.mark.anyio
@failing_api_mock
async def test_create_subscription_failure():
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_subscriptions_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def _invoke(extra_args, runner=None, **kwargs):

@pytest.mark.parametrize('options,expected_count',
[(['--status=running'], 100), ([], 100),
(['--source-type=catalog'], 100),
(['--source-type=soil_water_content'], 0),
(['--limit=1', '--status=running'], 1),
(['--limit=2', '--pretty', '--status=running'], 2),
(['--limit=1', '--status=preparing'], 0)])
Expand Down

0 comments on commit 5cd706e

Please sign in to comment.