Skip to content

Commit dd72894

Browse files
committed
Support bulk subs creation in client and add --bulk to CLI to invoke it
1 parent fe61ffd commit dd72894

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

planet/cli/subscriptions.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ async def list_subscriptions_cmd(ctx,
151151

152152
@subscriptions.command(name="create") # type: ignore
153153
@click.argument("request", type=types.JSON())
154+
@click.option(
155+
"--bulk",
156+
is_flag=True,
157+
help="Bulk create many subscriptions using a feature collection reference.",
158+
)
154159
@click.option(
155160
"--hosting",
156161
type=click.Choice([
@@ -198,9 +203,14 @@ async def create_subscription_cmd(ctx, request, pretty, **kwargs):
198203
hosting_info = sentinel_hub(collection_id, create_configuration)
199204
request["hosting"] = hosting_info
200205

201-
async with subscriptions_client(ctx) as client:
202-
sub = await client.create_subscription(request)
203-
echo_json(sub, pretty)
206+
if kwargs.get("bulk"):
207+
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
210+
else:
211+
async with subscriptions_client(ctx) as client:
212+
sub = await client.create_subscription(request)
213+
echo_json(sub, pretty)
204214

205215

206216
@subscriptions.command(name='cancel') # type: ignore

planet/clients/subscriptions.py

+32-1
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 Any, AsyncIterator, Awaitable, Dict, Optional, Sequence, TypeVar
4+
from typing import Any, AsyncIterator, Awaitable, Dict, Optional, Sequence, TypeVar, List
55

66
from typing_extensions import Literal
77

@@ -203,6 +203,37 @@ 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:
207+
"""
208+
Create multiple subscriptions in bulk. Currently, the list of requests can only contain one item.
209+
210+
Args:
211+
requests (List[dict]): A list of dictionaries where each dictionary
212+
represents a subscription to be created.
213+
214+
Raises:
215+
APIError: If the API returns an error response.
216+
ClientError: If there is an issue with the client request.
217+
218+
Returns:
219+
None: The bulk create endpoint returns an empty body, so no value
220+
is returned.
221+
"""
222+
try:
223+
url = f'{self._base_url}/bulk'
224+
_ = await self._session.request(method='POST',
225+
url=url,
226+
json={'subscriptions': requests})
227+
# Forward APIError. We don't strictly need this clause, but it
228+
# makes our intent clear.
229+
except APIError:
230+
raise
231+
except ClientError: # pragma: no cover
232+
raise
233+
else:
234+
# The bulk create endpoint returns an empty body so don't attempt to parse it
235+
return
236+
206237
async def cancel_subscription(self, subscription_id: str) -> None:
207238
"""Cancel a Subscription.
208239

planet/sync/subscriptions.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Planet Subscriptions API Python client."""
22

3-
from typing import Any, Dict, Iterator, Optional, Sequence, Union
3+
from typing import Any, Dict, Iterator, Optional, Sequence, Union, List
44

55
from typing_extensions import Literal
66

@@ -136,6 +136,22 @@ 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:
140+
"""Bulk create subscriptions.
141+
142+
Args:
143+
request (List[dict]): list of descriptions of a bulk creation.
144+
145+
Returns:
146+
None
147+
148+
Raises:
149+
APIError: on an API server error.
150+
ClientError: on a client error.
151+
"""
152+
return self._client._call_sync(
153+
self._client.bulk_create_subscriptions(requests))
154+
139155
def cancel_subscription(self, subscription_id: str) -> None:
140156
"""Cancel a Subscription.
141157

0 commit comments

Comments
 (0)