Skip to content

Commit 78a1768

Browse files
committed
default destinations sdk and cli support
1 parent 0cea292 commit 78a1768

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

planet/cli/destinations.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ async def _create_destination(ctx, data, pretty):
6565
raise ClickException(f"Failed to create destination: {e}")
6666

6767

68+
async def _set_default_destination(ctx, destination_id, pretty):
69+
async with destinations_client(ctx) as cl:
70+
try:
71+
response = await cl.set_default_destination(destination_id)
72+
echo_json(response, pretty)
73+
except Exception as e:
74+
raise ClickException(f"Failed to set default destination: {e}")
75+
76+
77+
async def _unset_default_destination(ctx, pretty):
78+
async with destinations_client(ctx) as cl:
79+
try:
80+
response = await cl.unset_default_destination()
81+
echo_json(response, pretty)
82+
except Exception as e:
83+
raise ClickException(f"Failed to unset default destination: {e}")
84+
85+
86+
async def _get_default_destination(ctx, pretty):
87+
async with destinations_client(ctx) as cl:
88+
try:
89+
response = await cl.get_default_destination()
90+
echo_json(response, pretty)
91+
except Exception as e:
92+
raise ClickException(f"Failed to get default destination: {e}")
93+
94+
6895
@command(destinations, name="list")
6996
@click.option('--archived',
7097
type=bool,
@@ -590,3 +617,57 @@ async def update_s3_compatible(ctx,
590617
data["parameters"]["use_path_style"] = True
591618

592619
await _patch_destination(ctx, destination_id, data, pretty)
620+
621+
622+
@destinations.group()
623+
def default():
624+
"""Commands for interacting with default destinations."""
625+
pass
626+
627+
628+
@command(default, name="set")
629+
@click.argument("destination_id")
630+
async def set_default_destination(ctx, destination_id, pretty):
631+
"""
632+
Set a default destination.
633+
634+
This command sets a specified destination as the default for the organization. Default destinations
635+
are globally available to all members of an organization. An organization can have zero or one default
636+
destination at any time. Ability to set a default destination is restricted to organization
637+
administrators and destination owners.
638+
639+
Example:
640+
641+
planet destinations default set my-destination-id
642+
"""
643+
await _set_default_destination(ctx, destination_id, pretty)
644+
645+
646+
@command(default, name="unset")
647+
async def unset_default_destination(ctx, pretty):
648+
"""
649+
Unset the current default destination.
650+
651+
This command unsets the current default destination. Ability to unset a default destination is restricted
652+
to organization administrators and destination owners. Returns None (HTTP 204, No Content) on success.
653+
654+
Example:
655+
656+
planet destinations default unset
657+
"""
658+
await _unset_default_destination(ctx, pretty)
659+
660+
661+
@command(default, name="get")
662+
async def get_default_destination(ctx, pretty):
663+
"""
664+
Get the current default destination.
665+
666+
This command gets the current default destination for the organization, if one is set. The default
667+
destination is globally available to all members of an organization.
668+
669+
Example:
670+
671+
planet destinations default get
672+
"""
673+
await _get_default_destination(ctx, pretty)

planet/clients/destinations.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
T = TypeVar("T")
2828

29+
DEFAULT_DESTINATION_REF = "pl:destinations/default"
30+
2931

3032
class DestinationsClient(_BaseClient):
3133
"""Asynchronous Destinations API client.
@@ -174,3 +176,77 @@ async def create_destination(self, request: Dict[str, Any]) -> Dict:
174176
else:
175177
dest = response.json()
176178
return dest
179+
180+
async def set_default_destination(self, destination_id: str) -> None:
181+
"""
182+
Set an existing destination as the default destination. Default destinations are globally available
183+
to all members of an organization. An organization can have zero or one default destination at any time.
184+
Ability to set a default destination is restricted to organization administrators and destination owners.
185+
186+
Args:
187+
destination_id (str): The ID of the destination to set as default.
188+
189+
Returns:
190+
dict: A dictionary containing the default destination details.
191+
192+
Raises:
193+
APIError: If the API returns an error response.
194+
ClientError: If there is an issue with the client request.
195+
"""
196+
url = f'{self._base_url}/default'
197+
request = {
198+
"destination_id": destination_id
199+
}
200+
try:
201+
response = await self._session.request(method='PUT',
202+
url=url,
203+
json=request)
204+
except APIError:
205+
raise
206+
except ClientError: # pragma: no cover
207+
raise
208+
else:
209+
return response.json()
210+
211+
async def unset_default_destination(self) -> None:
212+
"""
213+
Unset the current default destination. Ability to unset a default destination is restricted to
214+
organization administrators and destination owners. Returns None (HTTP 204, No Content) on success.
215+
216+
Returns:
217+
None
218+
219+
Raises:
220+
APIError: If the API returns an error response.
221+
ClientError: If there is an issue with the client request.
222+
"""
223+
url = f'{self._base_url}/default'
224+
try:
225+
await self._session.request(method='DELETE', url=url)
226+
except APIError:
227+
raise
228+
except ClientError: # pragma: no cover
229+
raise
230+
231+
async def get_default_destination(self) -> Dict:
232+
"""
233+
Get the current default destination. The default destination is globally available to all members of an
234+
organization.
235+
236+
Returns:
237+
dict: A dictionary containing the default destination details.
238+
239+
Raises:
240+
APIError: If the API returns an error response.
241+
ClientError: If there is an issue with the client request.
242+
"""
243+
url = f'{self._base_url}/default'
244+
try:
245+
response = await self._session.request(method='GET', url=url)
246+
except APIError:
247+
raise
248+
except ClientError: # pragma: no cover
249+
raise
250+
else:
251+
dest = response.json()
252+
return dest

planet/order_request.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,35 @@ def s3_compatible(endpoint: str,
392392
return {'s3_compatible': parameters}
393393

394394

395+
def destination(destination_ref: str, path_prefix: Optional[str] = None) -> dict:
396+
"""Destinations API configuration.
397+
Parameters:
398+
destination_ref: Reference to an existing Destinations API
399+
destination.
400+
path_prefix: Path prefix for deliveries.
401+
"""
402+
cloud_details: Dict[str, Any] = {'ref': destination_ref}
403+
404+
if path_prefix:
405+
cloud_details['path_prefix'] = path_prefix
406+
407+
return {'destination': cloud_details}
408+
409+
410+
def default_destination(path_prefix: Optional[str] = None) -> dict:
411+
"""Default Destinations API configuration.
412+
413+
Parameters:
414+
path_prefix: Path prefix for deliveries.
415+
"""
416+
parameters: Dict[str, Any] = {'ref': 'pl:destinations/default'}
417+
418+
if path_prefix:
419+
parameters['path_prefix'] = path_prefix
420+
421+
return {'destination': parameters}
422+
423+
395424
def _tool(name: str, parameters: dict) -> dict:
396425
"""Create the API spec representation of a tool.
397426

planet/subscription_request.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing_extensions import Literal
2020

2121
from . import geojson, specs
22+
from .clients.destinations import DEFAULT_DESTINATION_REF
2223
from .exceptions import ClientError
2324

2425
NOTIFICATIONS_TOPICS = ('delivery.success',
@@ -528,6 +529,34 @@ def s3_compatible(endpoint: str,
528529
return _delivery('s3_compatible', parameters)
529530

530531

532+
def destination(destination_ref: str, path_prefix: Optional[str] = None) -> dict:
533+
"""Specify a Destinations API destination by its ref.
534+
535+
Parameters:
536+
destination_id: The ID of the destination to deliver to.
537+
"""
538+
parameters: Dict[str, Any] = {"ref": destination_ref}
539+
540+
if path_prefix:
541+
parameters['path_prefix'] = path_prefix
542+
543+
return _delivery('destination', parameters)
544+
545+
546+
def default_destination(path_prefix: Optional[str] = None) -> dict:
547+
"""Specify the organization's default Destinations API destination.
548+
549+
Parameters:
550+
path_prefix: Path prefix for deliveries.
551+
"""
552+
parameters: Dict[str, Any] = {"ref": DEFAULT_DESTINATION_REF}
553+
554+
if path_prefix:
555+
parameters['path_prefix'] = path_prefix
556+
557+
return _delivery('destination', parameters)
558+
559+
531560
def notifications(url: str, topics: List[str]) -> dict:
532561
"""Specify a subscriptions API notification.
533562

0 commit comments

Comments
 (0)