From 5924584f87d2534852570c9b2ef86de8dfb31bc2 Mon Sep 17 00:00:00 2001 From: Kevin Lacaille Date: Mon, 31 Jul 2023 13:43:06 -0700 Subject: [PATCH 1/2] Added new keyword which allows to save data to order name. Overwrites info, dont like that. --- planet/clients/orders.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/planet/clients/orders.py b/planet/clients/orders.py index 3ff0c059..10ea87f8 100644 --- a/planet/clients/orders.py +++ b/planet/clients/orders.py @@ -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 ] From 15b0e489a5780bd1f612c13eaad828bc215bbece Mon Sep 17 00:00:00 2001 From: Kevin Lacaille Date: Mon, 31 Jul 2023 13:43:18 -0700 Subject: [PATCH 2/2] Test out new keyword. --- tests/integration/test_orders_api.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/integration/test_orders_api.py b/tests/integration/test_orders_api.py index 0c164c53..6a9347f4 100644 --- a/tests/integration/test_orders_api.py +++ b/tests/integration/test_orders_api.py @@ -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