diff --git a/planet/clients/data.py b/planet/clients/data.py index 83552cb5..e558de27 100644 --- a/planet/clients/data.py +++ b/planet/clients/data.py @@ -599,7 +599,7 @@ async def wait_asset( # loop without end if max_attempts is zero # otherwise, loop until num_attempts reaches max_attempts num_attempts = 0 - while not max_attempts or num_attempts < max_attempts: + while True: t = time.time() try: @@ -615,6 +615,10 @@ async def wait_asset( if current_status == 'active': break + if max_attempts and num_attempts >= max_attempts: + raise exceptions.ClientError( + f'Maximum number of attempts ({max_attempts}) reached.') + sleep_time = max(delay - (time.time() - t), 0) LOGGER.debug(f'sleeping {sleep_time}s') await asyncio.sleep(sleep_time) @@ -630,10 +634,6 @@ async def wait_asset( response = await self._session.request(method='GET', url=asset_url) asset = response.json() - if max_attempts and num_attempts >= max_attempts: - raise exceptions.ClientError( - f'Maximum number of attempts ({max_attempts}) reached.') - return asset async def download_asset(self, diff --git a/tests/integration/test_data_api.py b/tests/integration/test_data_api.py index e717de74..23259216 100644 --- a/tests/integration/test_data_api.py +++ b/tests/integration/test_data_api.py @@ -1379,6 +1379,44 @@ async def test_wait_asset_max_attempts(session): await cl.wait_asset(basic_udm2_asset, delay=0, max_attempts=1) +@respx.mock +@pytest.mark.anyio +async def test_wait_asset_active_on_last_attempt(session): + asset_url = f'{TEST_URL}/asset' + + basic_udm2_asset = { + "_links": { + "_self": asset_url, + "activate": "ACTIVATEURL", + "type": "https://api.planet.com/data/v1/asset-types/basic_udm2" + }, + "_permissions": ["download"], + "md5_digest": None, + "status": 'activating', + "type": "basic_udm2" + } + + basic_udm2_asset_active = copy.deepcopy(basic_udm2_asset) + basic_udm2_asset_active['status'] = 'active' + + route = respx.get(asset_url) + route.side_effect = [ + httpx.Response(HTTPStatus.OK, json=basic_udm2_asset), + httpx.Response(HTTPStatus.OK, json=basic_udm2_asset_active) + ] + + statuses = [] + cl = DataClient(session, base_url=TEST_URL) + asset = await cl.wait_asset(basic_udm2_asset, + delay=0, + max_attempts=2, + callback=statuses.append) + + assert asset == basic_udm2_asset_active + assert statuses == ['activating', 'activating', 'active'] + assert route.call_count == 2 + + @respx.mock @pytest.mark.anyio @pytest.mark.parametrize("exists, overwrite",