-
Notifications
You must be signed in to change notification settings - Fork 98
Add destinations api client #1175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
aa6dba8
make source type truly optional for planetary_variable_source
charcey 230c8b0
Merge branch 'main' of github.com:planetlabs/planet-client-python
charcey e8e4f9e
python sync and async cli with tests
charcey 1af648e
add cli, new md
charcey ebcbb1f
more cli tutorial, pass in ref in orders cli on list
charcey dc88d31
back out accidently committed unrelated changes
charcey 544a43d
type edit on cli md
charcey 31cbf82
fix url
charcey 29a57d2
linting
charcey bf5a2fa
lint and add ref to sync list
charcey 362a7de
Apply suggestions from code review
charcey d27901a
Apply suggestions from code review
charcey 68aafaa
Merge branch 'main' of github.com:planetlabs/planet-client-python int…
charcey c439611
update cli args to options, update examples
charcey 6722bea
add cli tests
charcey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
python sync and async cli with tests
- Loading branch information
commit e8e4f9ee4b1358cf8620d7e119b450c83b4319a4
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| # Copyright 2025 Planet Labs PBC. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| # use this file except in compliance with the License. You may obtain a copy of | ||
| # the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations under | ||
| # the License. | ||
|
|
||
| import logging | ||
| from typing import Any, AsyncIterator, Dict, Optional, Union, TypeVar | ||
|
|
||
| from planet.clients.base import _BaseClient | ||
| from planet.exceptions import APIError, ClientError | ||
| from planet.http import Session | ||
| from ..constants import PLANET_BASE_URL | ||
|
|
||
|
|
||
| BASE_URL = f'{PLANET_BASE_URL}/public/destinations/v1/' | ||
|
|
||
| LOGGER = logging.getLogger() | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
| class DestinationsClient(_BaseClient): | ||
| """Asynchronous Destinations API client. | ||
|
|
||
| Example: | ||
| ```python | ||
| >>> import asyncio | ||
| >>> from planet import Session | ||
| >>> | ||
| >>> async def main(): | ||
| ... async with Session() as sess: | ||
| ... cl = sess.client('destinations') | ||
| ... # use client here | ||
| ... | ||
| >>> asyncio.run(main()) | ||
| ``` | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| session: Session, | ||
| base_url: Optional[str] = None) -> None: | ||
| """ | ||
| Parameters: | ||
| session: Open session connected to server. | ||
| base_url: The base URL to use. Defaults to production destinations | ||
| API base url. | ||
| """ | ||
| super().__init__(session, base_url or BASE_URL) | ||
|
|
||
| async def list_destinations(self, | ||
| archived: Optional[bool] = None, | ||
| is_owner: Optional[bool] = None, | ||
| can_write: Optional[bool] = None, | ||
| ) -> Dict: | ||
| """ | ||
| List all destinations. By default, all non-archived destinations in the requesting user's org are returned. | ||
|
|
||
| Args: | ||
| archived (bool): If True, include archived destinations. | ||
| is_owner (bool): If True, include only destinations owned by the requesting user. | ||
| can_write (bool): If True, include only destinations the requesting user can modify. | ||
|
|
||
| Returns: | ||
| dict: A dictionary containing the list of destinations inside the 'destinations' key. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| params: Dict[str, Any] = {} | ||
| if archived is not None: | ||
| params["archived"] = archived | ||
| if is_owner is not None: | ||
| params["is_owner"] = is_owner | ||
| if can_write is not None: | ||
| params["can_write"] = can_write | ||
|
|
||
| try: | ||
| response = await self._session.request(method='GET', | ||
| url=self._base_url, | ||
| params=params) | ||
| except APIError: | ||
| raise | ||
| except ClientError: # pragma: no cover | ||
| raise | ||
| else: | ||
| dest_response = response.json() | ||
| return dest_response | ||
|
|
||
| async def get_destination(self, destination_id: str) -> Dict: | ||
| """ | ||
| Get a specific destination by its ID. | ||
|
|
||
| Args: | ||
| destination_id (str): The ID of the destination to retrieve. | ||
|
|
||
| Returns: | ||
| dict: A dictionary containing the destination details. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| url = f'{self._base_url}/{destination_id}' | ||
| try: | ||
| response = await self._session.request(method='GET', | ||
| url=url) | ||
| except APIError: | ||
| raise | ||
| except ClientError: # pragma: no cover | ||
| raise | ||
| else: | ||
| dest = response.json() | ||
| return dest | ||
|
|
||
| async def patch_destination(self, | ||
| destination_ref: str, | ||
| request: Dict[str, Any]) -> Dict: | ||
| """ | ||
| Update a specific destination by its ref. | ||
|
|
||
| Args: | ||
| destination_ref (str): The ref of the destination to update. | ||
| request (dict): Destination content to update, only attributes to update are required. | ||
|
|
||
| Returns: | ||
| dict: A dictionary containing the updated destination details. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| url = f'{self._base_url}/{destination_ref}' | ||
| try: | ||
| response = await self._session.request(method='PATCH', | ||
| url=url, | ||
| json=request) | ||
| except APIError: | ||
| raise | ||
| except ClientError: # pragma: no cover | ||
| raise | ||
| else: | ||
| dest = response.json() | ||
| return dest | ||
|
|
||
| async def create_destination(self, request: Dict[str, Any]) -> Dict: | ||
| """ | ||
| Create a new destination. | ||
|
|
||
| Args: | ||
| data (dict): Destination content to create, all attributes are required. | ||
charcey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Returns: | ||
| dict: A dictionary containing the created destination details. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| try: | ||
| response = await self._session.request(method='POST', | ||
| url=self._base_url, | ||
| json=request) | ||
| except APIError: | ||
| raise | ||
| except ClientError: # pragma: no cover | ||
| raise | ||
| else: | ||
| dest = response.json() | ||
| return dest | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Copyright 2025 Planet Labs PBC. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| # use this file except in compliance with the License. You may obtain a copy of | ||
| # the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations under | ||
| # the License. | ||
|
|
||
| from typing import Any, Dict, Optional | ||
| from planet.clients.destinations import DestinationsClient | ||
| from planet.http import Session | ||
|
|
||
| class DestinationsAPI: | ||
|
|
||
| _client: DestinationsClient | ||
|
|
||
| def __init__(self, session: Session, base_url: Optional[str] = None): | ||
| """ | ||
| Parameters: | ||
| session: Open session connected to server. | ||
| base_url: The base URL to use. Defaults to production Destinations API | ||
| base url. | ||
| """ | ||
|
|
||
| self._client = DestinationsClient(session, base_url) | ||
|
|
||
|
|
||
| def list_destinations(self, | ||
| archived: Optional[bool] = None, | ||
| is_owner: Optional[bool] = None, | ||
| can_write: Optional[bool] = None, | ||
| ) -> Dict: | ||
| """ | ||
| List all destinations. By default, all non-archived destinations in the requesting user's org are returned. | ||
|
|
||
| Args: | ||
| archived (bool): If True, include archived destinations. | ||
| is_owner (bool): If True, include only destinations owned by the requesting user. | ||
| can_write (bool): If True, include only destinations the requesting user can modify. | ||
|
|
||
| Returns: | ||
| dict: A dictionary containing the list of destinations inside the 'destinations' key. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| return self._client._call_sync( | ||
| self._client.list_destinations(archived, | ||
| is_owner, | ||
| can_write)) | ||
|
|
||
| def get_destination(self, destination_id: str) -> Dict: | ||
| """ | ||
| Get a specific destination by its ID. | ||
|
|
||
| Args: | ||
| destination_id (str): The ID of the destination to retrieve. | ||
|
|
||
| Returns: | ||
| dict: A dictionary containing the destination details. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| return self._client._call_sync( | ||
| self._client.get_destination(destination_id) | ||
| ) | ||
|
|
||
| def patch_destination(self, | ||
| destination_ref: str, | ||
| request: Dict[str, Any]) -> Dict: | ||
| """ | ||
| Update a specific destination by its ref. | ||
|
|
||
| Args: | ||
| destination_ref (str): The ref of the destination to update. | ||
| request (dict): Destination content to update, only attributes to update are required. | ||
|
|
||
| Returns: | ||
| dict: A dictionary containing the updated destination details. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| return self._client._call_sync( | ||
| self._client.patch_destination(destination_ref, request) | ||
| ) | ||
|
|
||
| def create_destination(self, request: Dict[str, Any]) -> Dict: | ||
| """ | ||
| Create a new destination. | ||
|
|
||
| Args: | ||
| data (dict): Destination content to create, all attributes are required. | ||
charcey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Returns: | ||
| dict: A dictionary containing the created destination details. | ||
|
|
||
| Raises: | ||
| APIError: If the API returns an error response. | ||
| ClientError: If there is an issue with the client request. | ||
| """ | ||
| return self._client._call_sync( | ||
| self._client.create_destination(request) | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.