Skip to content

Enable redirect following and avoid treating 302s as errors #685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2022

Conversation

sgillies
Copy link
Contributor

@sgillies sgillies commented Sep 1, 2022

Towards resolving #678.

Without these changes, an attempt to download an order with 0.23 produces

Error:

With these changes, I'm able to download an order.

planet/http.py Outdated
@@ -241,7 +243,7 @@ def __init__(self, auth: AuthType = None):

LOGGER.info(f'Session read timeout set to {READ_TIMEOUT}.')
timeout = httpx.Timeout(10.0, read=READ_TIMEOUT)
self._client = httpx.AsyncClient(auth=auth, timeout=timeout)
self._client = httpx.AsyncClient(auth=auth, timeout=timeout, follow_redirects=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed since httpx 0.20.

planet/http.py Outdated
except httpx.HTTPStatusError as e:
e.response.read()
cls._convert_and_raise(e)
if response.status_code >= 400:
Copy link
Contributor Author

@sgillies sgillies Sep 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we do this we will trip on the fact that httpx.Response(302).raise_for_status() raises an exception in version 0.23, even when a redirect could be followed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good news is that httpx.Response(302).is_error is False.

@sgillies sgillies marked this pull request as ready for review September 6, 2022 23:49
@@ -33,6 +33,7 @@
TEST_URL = 'http://www.MockNotRealURL.com/api/path'
TEST_BULK_CANCEL_URL = f'{TEST_URL}/bulk/orders/v2/cancel'
TEST_DOWNLOAD_URL = f'{TEST_URL}/download'
TEST_DOWNLOAD_ACTUAL_URL = f'{TEST_URL}/download_actual'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are probably better names and URLs for this. In the actual API, it's link.planet.com, but we shouldn't depend on anything but the location header.

respx.get(dl_url).return_value = mock_resp

respx.get(dl_url).return_value = mock_redirect
respx.get(TEST_DOWNLOAD_ACTUAL_URL).return_value = mock_resp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreiberkyle @mkshah605 @kevinlacaille @cholmes the orders API redirects when downloading. I'm simulating that here. Default behavior of httpx (no redirect following) will cause this test to fail, which is what the bug reporter was seeing.

mock_redirect = httpx.Response(HTTPStatus.FOUND,
headers={
'Location': TEST_DOWNLOAD_ACTUAL_URL,
'Content-Length': '0'
Copy link
Contributor Author

@sgillies sgillies Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreiberkyle there was an exception in the model module if the redirect response didn't have a content-length header. This feels like something we should fix; our API redirect response has an empty body and we should be able to traverse to the header location without checking the body or its length.

@sgillies sgillies requested a review from cholmes September 7, 2022 00:25
@sgillies sgillies added the bug label Sep 7, 2022
@sgillies sgillies self-assigned this Sep 7, 2022
This works around httpx's preference to raise in the redirect case.
except httpx.HTTPStatusError as e:
await e.response.aread()
cls._convert_and_raise(e)
if response.is_error:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #685 (comment). This is probably the same thing as response.status_code >= 400 but syntactically a little neater, maybe?

@sgillies sgillies merged commit 5b5bc7e into main Sep 7, 2022
@sgillies sgillies deleted the follow-redirects-again branch September 7, 2022 15:28
@cholmes cholmes added this to the 2.0a3: Iterative Alpha Release milestone Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants