Skip to content

Commit 32e4eee

Browse files
authored
Merge pull request #1074 from tbarsballe/sub-source-type
Add source_type parameter to list subscriptions
2 parents efcb023 + b57a0c4 commit 32e4eee

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

docs/cli/cli-subscriptions.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ parameter higher, or you can set it to 0 and there will be no limit.
134134
You can get nicer formatting with `--pretty` or pipe it into `jq`, just like the other Planet
135135
CLI’s.
136136

137-
The `list` command also supports filtering by the status of the subscription:
137+
The `list` command supports filtering by the status of the subscription:
138138

139139
```sh
140140
planet subscriptions list --status running
@@ -143,6 +143,15 @@ planet subscriptions list --status running
143143
gives you just the currently active subscriptions. The other available statuses are:
144144
`cancelled`, `preparing`, `pending`, `completed`, `suspended`, and `failed`.
145145

146+
The `list` command also supports filtering by the source type of the subscription:
147+
148+
```sh
149+
planet subscriptions list --source-type catalog
150+
```
151+
152+
only gives you subscriptions with the `catalog` source type. For the full list of available source types,
153+
see [Subscription Source Types](https://developers.planet.com/docs/subscriptions/source/#subscription-source-types).
154+
146155
### Get Subscription
147156

148157
To get the full details on a single subscription you can take the id from your list and use the

planet/cli/subscriptions.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,21 @@ def subscriptions(ctx, base_url):
7878
multiple=True,
7979
default=None,
8080
help="Select subscriptions in one or more states. Default is all.")
81+
@click.option(
82+
'--source-type',
83+
default=None,
84+
help="Filter subscriptions by source type. See documentation for all "
85+
"available types. Default is all.")
8186
@limit
8287
@click.pass_context
8388
@translate_exceptions
8489
@coro
85-
async def list_subscriptions_cmd(ctx, status, limit, pretty):
90+
async def list_subscriptions_cmd(ctx, status, source_type, limit, pretty):
8691
"""Prints a sequence of JSON-encoded Subscription descriptions."""
8792
async with subscriptions_client(ctx) as client:
88-
async for sub in client.list_subscriptions(status=status, limit=limit):
93+
async for sub in client.list_subscriptions(status=status,
94+
source_type=source_type,
95+
limit=limit):
8996
echo_json(sub, pretty)
9097

9198

planet/clients/subscriptions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Planet Subscriptions API Python client."""
22

33
import logging
4-
from typing import AsyncIterator, Optional, Sequence
4+
from typing import AsyncIterator, Optional, Sequence, Dict, Union
55

66
from typing_extensions import Literal
77

@@ -61,6 +61,7 @@ def __init__(self,
6161

6262
async def list_subscriptions(self,
6363
status: Optional[Sequence[str]] = None,
64+
source_type: Optional[str] = None,
6465
limit: int = 100) -> AsyncIterator[dict]:
6566
"""Iterate over list of account subscriptions with optional filtering.
6667
@@ -73,6 +74,7 @@ async def list_subscriptions(self,
7374
status (Set[str]): pass subscriptions with status in this
7475
set, filter out subscriptions with status not in this
7576
set.
77+
source_type (str): Product source type filter, to return only subscriptions with the matching source type
7678
limit (int): limit the number of subscriptions in the
7779
results. When set to 0, no maximum is applied.
7880
TODO: user_id
@@ -89,7 +91,11 @@ class _SubscriptionsPager(Paged):
8991
"""Navigates pages of messages about subscriptions."""
9092
ITEMS_KEY = 'subscriptions'
9193

92-
params = {'status': [val for val in status or {}]}
94+
params: Dict[str, Union[str, Sequence[str]]] = {
95+
'status': [val for val in status or {}]
96+
}
97+
if source_type is not None:
98+
params['source_type'] = source_type # type: ignore
9399

94100
try:
95101
response = await self._session.request(method='GET',

tests/integration/test_subscriptions_api.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,20 @@ def result_pages(status=None, size=40):
6666
M(params__contains={'status': 'preparing'})).mock(
6767
side_effect=[Response(200, json={'subscriptions': []})])
6868

69-
# 4. No status requested. Response is the same as for 1.
69+
# 4. source_type: catalog requested. Response is the same as for 1.
70+
api_mock.route(
71+
M(url=TEST_URL),
72+
M(params__contains={'source_type': 'catalog'})).mock(side_effect=[
73+
Response(200, json=page)
74+
for page in result_pages(status={'running'}, size=40)
75+
])
76+
77+
# 5. source_type: soil_water_content requested. Response has a single empty page.
78+
api_mock.route(M(url=TEST_URL),
79+
M(params__contains={'source_type': 'soil_water_content'})).mock(
80+
side_effect=[Response(200, json={'subscriptions': []})])
81+
82+
# 6. No status or source_type requested. Response is the same as for 1.
7083
api_mock.route(M(url=TEST_URL)).mock(
7184
side_effect=[Response(200, json=page) for page in result_pages(size=40)])
7285

@@ -182,6 +195,24 @@ async def test_list_subscriptions_success(
182195
]) == count
183196

184197

198+
@pytest.mark.parametrize("source_type, count",
199+
[("catalog", 100), ("soil_water_content", 0),
200+
(None, 100)])
201+
@pytest.mark.anyio
202+
@api_mock
203+
async def test_list_subscriptions_source_type_success(
204+
source_type,
205+
count,
206+
):
207+
"""Account subscriptions iterator yields expected descriptions."""
208+
async with Session() as session:
209+
client = SubscriptionsClient(session, base_url=TEST_URL)
210+
assert len([
211+
sub
212+
async for sub in client.list_subscriptions(source_type=source_type)
213+
]) == count
214+
215+
185216
@pytest.mark.anyio
186217
@failing_api_mock
187218
async def test_create_subscription_failure():

tests/integration/test_subscriptions_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def _invoke(extra_args, runner=None, **kwargs):
5353

5454
@pytest.mark.parametrize('options,expected_count',
5555
[(['--status=running'], 100), ([], 100),
56+
(['--source-type=catalog'], 100),
57+
(['--source-type=soil_water_content'], 0),
5658
(['--limit=1', '--status=running'], 1),
5759
(['--limit=2', '--pretty', '--status=running'], 2),
5860
(['--limit=1', '--status=preparing'], 0)])

0 commit comments

Comments
 (0)