Skip to content
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

Save download to order name #1009

Open
wants to merge 2 commits into
base: maint-2.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion planet/clients/orders.py
Original file line number Diff line number Diff line change
@@ -286,6 +286,7 @@ async def download_asset(self,
async def download_order(self,
order_id: str,
directory: Path = Path('.'),
save_to_order_name: bool = False,
overwrite: bool = False,
progress_bar: bool = False) -> List[Path]:
"""Download all assets in an order.
@@ -294,6 +295,8 @@ async def download_order(self,
order_id: The ID of the order.
directory: Base directory for file download. This directory must
already exist.
save_to_order_name: Save data to directory named after the given
order name.
overwrite: Overwrite files if they already exist.
progress_bar: Show progress bar during download.

@@ -317,10 +320,17 @@ async def download_order(self,
info = self._get_download_info(order)
LOGGER.info(f'downloading {len(info)} assets from order {order_id}')

if save_to_order_name:
order_path = Path(directory, order['name'])
for i in info:
i['directory'] = Path('')
else:
order_path = directory

filenames = [
await self.download_asset(i['location'],
filename=i['filename'],
directory=directory / i['directory'],
directory=order_path / i['directory'],
overwrite=overwrite,
progress_bar=progress_bar) for i in info
]
22 changes: 22 additions & 0 deletions tests/integration/test_orders_api.py
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@
TEST_DOWNLOAD_ACTUAL_URL = f'{TEST_URL}/download_actual'
TEST_ORDERS_URL = f'{TEST_URL}/orders/v2'
TEST_STATS_URL = f'{TEST_URL}/stats/orders/v2'
TEST_ORDER_NAME = 'my-order-name'

LOGGER = logging.getLogger(__name__)

@@ -64,6 +65,7 @@ def f():
'location': dl_url, 'name': 'file.json'
},
]
order_description['name'] = TEST_ORDER_NAME

get_url = f'{TEST_ORDERS_URL}/{oid}'
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
@@ -853,3 +855,23 @@ async def test_download_order_overwrite_false_nonexisting_data(
# Check that the was data downloaded and has the correct contents
with open(Path(tmpdir, 'file.json')) as f:
assert json.load(f) == downloaded_content


@respx.mock
@pytest.mark.anyio
async def test_download_order_save_to_order_name(
tmpdir, oid, session, create_download_mock, downloaded_content):
"""
Test if download_order() downloads data to a directory named after the
given order name.
"""

create_download_mock()
cl = OrdersClient(session, base_url=TEST_URL)
filenames = await cl.download_order(oid, save_to_order_name=True,
directory=str(tmpdir))

assert filenames == [Path(tmpdir, TEST_ORDER_NAME, 'file.json')]
# Check that the was data downloaded and has the correct contents
with open(Path(tmpdir, TEST_ORDER_NAME, 'file.json')) as f:
assert json.load(f) == downloaded_content