Skip to content

Commit 9c61876

Browse files
Merge pull request #485 from planetlabs/robustify_OrdersClient
Better error handling in OrdersClient
2 parents 468d060 + 320f366 commit 9c61876

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

planet/clients/orders.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,13 @@ async def download_order(self,
291291
state.
292292
"""
293293
order = await self.get_order(order_id)
294-
295294
order_state = order['state']
296295
if not OrderStates.is_final(order_state):
297296
raise exceptions.ClientError(
298297
'Order cannot be downloaded because the order state '
299298
f'({order_state}) is not a final state. '
300299
'Consider using wait functionality before '
301300
'attempting to download.')
302-
303301
locations = self._get_order_locations(order)
304302
LOGGER.info(
305303
f'downloading {len(locations)} assets from order {order_id}')
@@ -316,8 +314,14 @@ async def download_order(self,
316314
@staticmethod
317315
def _get_order_locations(order):
318316
links = order['_links']
319-
results = links.get('results', None)
320-
return list(r['location'] for r in results if r)
317+
results = links.get('results', [])
318+
try:
319+
return list(r['location'] for r in results if r)
320+
except TypeError:
321+
LOGGER.warning(
322+
'order does not have any locations, will not download any ' +
323+
'files.')
324+
return []
321325

322326
async def wait(self,
323327
order_id: str,

tests/integration/test_orders_api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,37 @@ async def test_download_order_state(tmpdir, order_description, oid, session):
656656
await cl.download_order(oid, directory=str(tmpdir))
657657

658658

659+
@respx.mock
660+
@pytest.mark.asyncio
661+
async def test_download_order_without_order_links(tmpdir,
662+
order_description,
663+
oid,
664+
session):
665+
dl_url1 = TEST_DOWNLOAD_URL + '/1?token=IAmAToken'
666+
667+
order_description['state'] = 'success'
668+
order_description['_links']['results'] = None
669+
670+
get_url = f'{TEST_ORDERS_URL}/{oid}'
671+
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
672+
respx.get(get_url).return_value = mock_resp
673+
674+
mock_resp1 = httpx.Response(HTTPStatus.OK,
675+
json={'key': 'value'},
676+
headers={
677+
'Content-Type':
678+
'application/json',
679+
'Content-Disposition':
680+
'attachment; filename="m1.json"'
681+
})
682+
respx.get(dl_url1).return_value = mock_resp1
683+
684+
client = OrdersClient(session, base_url=TEST_URL)
685+
686+
filenames = await client.download_order(oid, directory=str(tmpdir))
687+
assert len(filenames) == 0
688+
689+
659690
@respx.mock
660691
@pytest.mark.asyncio
661692
async def test_download_order_overwrite_true_preexisting_data(

0 commit comments

Comments
 (0)