File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,6 +24,9 @@ class IMAPDataAccessError(Exception):
2424 pass
2525
2626
27+ _RETRY_ADAPTER = requests .adapters .HTTPAdapter (max_retries = 3 )
28+
29+
2730@contextlib .contextmanager
2831def _make_request (request : requests .PreparedRequest ):
2932 """Get the response from a URL request using the requests library.
@@ -44,6 +47,7 @@ def _make_request(request: requests.PreparedRequest):
4447 )
4548 try :
4649 with requests .Session () as session :
50+ session .mount ("https://" , _RETRY_ADAPTER )
4751 response = session .send (request )
4852 response .raise_for_status ()
4953 yield response
@@ -52,8 +56,8 @@ def _make_request(request: requests.PreparedRequest):
5256 error_msg = f"{ e .response .status_code } { e .response .reason } : { e .response .text } "
5357 raise IMAPDataAccessError (error_msg ) from e
5458 except requests .exceptions .RequestException as e :
55- error_msg = f" { e . response . status_code } { e . response . reason } : { e . response . text } "
56- raise IMAPDataAccessError (error_msg ) from e
59+ # Handle cases where response may not exist (connection errors, timeouts, etc.)
60+ raise IMAPDataAccessError (str ( e ) ) from e
5761
5862
5963def _get_base_url () -> str :
Original file line number Diff line number Diff line change @@ -97,15 +97,12 @@ def test_request_errors(mock_send_request):
9797 with pytest .raises (imap_data_access .io .IMAPDataAccessError , match = "404 Not Found" ):
9898 imap_data_access .download (test_science_path )
9999
100- # Set up the mock to raise a RequestException
101- mock_response .status_code = 400
102- mock_response .reason = "Request failed"
103- mock_response .text = ""
100+ # Set up the mock to raise a RequestException with a response
104101 mock_send_request .side_effect = requests .exceptions .RequestException (
105- response = mock_response
102+ "connection error"
106103 )
107104 with pytest .raises (
108- imap_data_access .io .IMAPDataAccessError , match = "400 Request failed "
105+ imap_data_access .io .IMAPDataAccessError , match = "connection error "
109106 ):
110107 imap_data_access .download (test_science_path )
111108
You can’t perform that action at this time.
0 commit comments