Skip to content

Commit d962a6b

Browse files
committed
Update to parse referenced list of subs, and add an integration test
1 parent 65d5eac commit d962a6b

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

planet/cli/subscriptions.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ async def create_subscription_cmd(ctx, request, pretty, **kwargs):
205205

206206
if kwargs.get("bulk"):
207207
async with subscriptions_client(ctx) as client:
208-
_ = await client.bulk_create_subscriptions([request])
209-
# Bulk create returns no response, so we don't echo anything
208+
links = await client.bulk_create_subscriptions([request])
209+
# Bulk create returns just a link to an endpoint to list created subscriptions.
210+
echo_json(links, pretty)
210211
else:
211212
async with subscriptions_client(ctx) as client:
212213
sub = await client.create_subscription(request)

planet/clients/subscriptions.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ async def create_subscription(self, request: dict) -> dict:
203203
sub = resp.json()
204204
return sub
205205

206-
async def bulk_create_subscriptions(self, requests: List[dict]) -> None:
206+
async def bulk_create_subscriptions(self, requests: List[dict]) -> Dict:
207207
"""
208208
Create multiple subscriptions in bulk. Currently, the list of requests can only contain one item.
209209
@@ -216,20 +216,20 @@ async def bulk_create_subscriptions(self, requests: List[dict]) -> None:
216216
ClientError: If there is an issue with the client request.
217217
218218
Returns:
219-
None: The bulk create endpoint returns an empty body, so no value
220-
is returned.
219+
The response including a _links key to the list endpoint for use finding the created subscriptions.
221220
"""
222221
try:
223222
url = f'{self._base_url}/bulk'
224-
_ = await self._session.request(method='POST',
225-
url=url,
226-
json={'subscriptions': requests})
223+
resp = await self._session.request(
224+
method='POST', url=url, json={'subscriptions': requests})
227225
# Forward APIError. We don't strictly need this clause, but it
228226
# makes our intent clear.
229227
except APIError:
230228
raise
231229
except ClientError: # pragma: no cover
232230
raise
231+
else:
232+
return resp.json()
233233

234234
async def cancel_subscription(self, subscription_id: str) -> None:
235235
"""Cancel a Subscription.

planet/sync/subscriptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def create_subscription(self, request: Dict) -> Dict:
136136
return self._client._call_sync(
137137
self._client.create_subscription(request))
138138

139-
def bulk_create_subscriptions(self, requests: List[Dict]) -> None:
139+
def bulk_create_subscriptions(self, requests: List[Dict]) -> Dict:
140140
"""Bulk create subscriptions.
141141
142142
Args:

tests/integration/test_subscriptions_api.py

+23
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ def modify_response(request):
122122
create_mock.route(M(url=TEST_URL),
123123
method='POST').mock(side_effect=modify_response)
124124

125+
bulk_create_mock = respx.mock()
126+
bulk_create_mock.route(
127+
M(url=f'{TEST_URL}/bulk'), method='POST'
128+
).mock(return_value=Response(
129+
200,
130+
json={
131+
'_links': {
132+
'list': f'{TEST_URL}/subscriptions/v1?created={datetime.now().isoformat()}/&geom_ref=pl:features:test_features&name=test-sub'
133+
}
134+
}))
135+
125136
update_mock = respx.mock()
126137
update_mock.route(M(url=f'{TEST_URL}/test'),
127138
method='PUT').mock(side_effect=modify_response)
@@ -334,6 +345,18 @@ async def test_create_subscription_success():
334345
assert sub['name'] == 'test'
335346

336347

348+
@pytest.mark.anyio
349+
@bulk_create_mock
350+
async def test_bulk_create_subscription_success():
351+
"""Bulk subscription is created, description has the expected items."""
352+
async with Session() as session:
353+
client = SubscriptionsClient(session, base_url=TEST_URL)
354+
resp = await client.bulk_create_subscriptions([{
355+
'name': 'test', 'delivery': 'yes, please', 'source': 'test'
356+
}])
357+
assert '/subscriptions/v1?' in resp['_links']['list']
358+
359+
337360
@create_mock
338361
def test_create_subscription_success_sync():
339362
"""Subscription is created, description has the expected items."""

0 commit comments

Comments
 (0)