Skip to content
Merged
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
8 changes: 6 additions & 2 deletions imap_data_access/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class IMAPDataAccessError(Exception):
pass


_RETRY_ADAPTER = requests.adapters.HTTPAdapter(max_retries=3)


@contextlib.contextmanager
def _make_request(request: requests.PreparedRequest):
"""Get the response from a URL request using the requests library.
Expand All @@ -44,6 +47,7 @@ def _make_request(request: requests.PreparedRequest):
)
try:
with requests.Session() as session:
session.mount("https://", _RETRY_ADAPTER)
response = session.send(request)
response.raise_for_status()
yield response
Expand All @@ -52,8 +56,8 @@ def _make_request(request: requests.PreparedRequest):
error_msg = f"{e.response.status_code} {e.response.reason}: {e.response.text}"
raise IMAPDataAccessError(error_msg) from e
except requests.exceptions.RequestException as e:
error_msg = f"{e.response.status_code} {e.response.reason}: {e.response.text}"
raise IMAPDataAccessError(error_msg) from e
# Handle cases where response may not exist (connection errors, timeouts, etc.)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice - Ive definitely seen that happen before and thought it was due to something else

raise IMAPDataAccessError(str(e)) from e


def _get_base_url() -> str:
Expand Down
9 changes: 3 additions & 6 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,12 @@ def test_request_errors(mock_send_request):
with pytest.raises(imap_data_access.io.IMAPDataAccessError, match="404 Not Found"):
imap_data_access.download(test_science_path)

# Set up the mock to raise a RequestException
mock_response.status_code = 400
mock_response.reason = "Request failed"
mock_response.text = ""
# Set up the mock to raise a RequestException with a response
mock_send_request.side_effect = requests.exceptions.RequestException(
response=mock_response
"connection error"
)
with pytest.raises(
imap_data_access.io.IMAPDataAccessError, match="400 Request failed"
imap_data_access.io.IMAPDataAccessError, match="connection error"
):
imap_data_access.download(test_science_path)

Expand Down