Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions planet/clients/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/test_data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down