Skip to content

Commit 5b5bc7e

Browse files
authored
Enable redirect following and avoid treating 302s as errors (#685)
* Enable redirect following and avoid treating 302s as errors * Remove debugging, reformat * Change download test to fail if we don't follow redirects * Use raise_for_status only if we have an error response This works around httpx's preference to raise in the redirect case.
1 parent 1f555a2 commit 5b5bc7e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

planet/http.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ def _convert_and_raise(cls, error):
9393

9494
@classmethod
9595
def _raise_for_status(cls, response):
96-
try:
97-
response.raise_for_status()
98-
except httpx.HTTPStatusError as e:
99-
e.response.read()
100-
cls._convert_and_raise(e)
96+
if response.is_error:
97+
try:
98+
response.raise_for_status()
99+
except httpx.HTTPStatusError as e:
100+
e.response.read()
101+
cls._convert_and_raise(e)
101102

102103
return
103104

@@ -241,7 +242,9 @@ def __init__(self, auth: AuthType = None):
241242

242243
LOGGER.info(f'Session read timeout set to {READ_TIMEOUT}.')
243244
timeout = httpx.Timeout(10.0, read=READ_TIMEOUT)
244-
self._client = httpx.AsyncClient(auth=auth, timeout=timeout)
245+
self._client = httpx.AsyncClient(auth=auth,
246+
timeout=timeout,
247+
follow_redirects=True)
245248

246249
self._client.headers.update({'User-Agent': self._get_user_agent()})
247250
self._client.headers.update({'X-Planet-App': 'python-sdk'})
@@ -265,11 +268,12 @@ async def alog_response(*args, **kwargs):
265268

266269
@classmethod
267270
async def _raise_for_status(cls, response):
268-
try:
269-
response.raise_for_status()
270-
except httpx.HTTPStatusError as e:
271-
await e.response.aread()
272-
cls._convert_and_raise(e)
271+
if response.is_error:
272+
try:
273+
response.raise_for_status()
274+
except httpx.HTTPStatusError as e:
275+
await e.response.aread()
276+
cls._convert_and_raise(e)
273277

274278
return
275279

tests/integration/test_orders_api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
TEST_URL = 'http://www.MockNotRealURL.com/api/path'
3434
TEST_BULK_CANCEL_URL = f'{TEST_URL}/bulk/orders/v2/cancel'
3535
TEST_DOWNLOAD_URL = f'{TEST_URL}/download'
36+
TEST_DOWNLOAD_ACTUAL_URL = f'{TEST_URL}/download_actual'
3637
TEST_ORDERS_URL = f'{TEST_URL}/orders/v2'
3738
TEST_STATS_URL = f'{TEST_URL}/stats/orders/v2'
3839

@@ -540,8 +541,16 @@ async def test_download_asset_md(tmpdir, session):
540541
'Content-Type': 'application/json',
541542
'Content-Disposition': 'attachment; filename="metadata.json"'
542543
}
544+
545+
mock_redirect = httpx.Response(HTTPStatus.FOUND,
546+
headers={
547+
'Location': TEST_DOWNLOAD_ACTUAL_URL,
548+
'Content-Length': '0'
549+
})
543550
mock_resp = httpx.Response(HTTPStatus.OK, json=md_json, headers=md_headers)
544-
respx.get(dl_url).return_value = mock_resp
551+
552+
respx.get(dl_url).return_value = mock_redirect
553+
respx.get(TEST_DOWNLOAD_ACTUAL_URL).return_value = mock_resp
545554

546555
cl = OrdersClient(session, base_url=TEST_URL)
547556
filename = await cl.download_asset(dl_url, directory=str(tmpdir))

0 commit comments

Comments
 (0)