From 8494f070f1c379eccbfd1a623be58a3f541991eb Mon Sep 17 00:00:00 2001 From: Evan Berquist Date: Mon, 23 Nov 2026 11:39:44 -0500 Subject: [PATCH] add connect and read timeouts --- README.md | 15 +++++++++++++++ emailable/client.py | 8 +++++++- tests/test_timeout.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/test_timeout.py diff --git a/README.md b/README.md index bfda25c..e1efaa2 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,21 @@ client.verify(api_key='your_api_key') client.verify(access_token='your_access_token') ``` +### Timeouts + +By default, the client waits up to 30 seconds to connect and up to 60 seconds +for the server to send data before raising `emailable.ClientError`. The read +timeout applies to each wait between bytes, not to the whole response. Both +must be positive numbers of seconds. To change them for every client: + +```python +emailable.Client.open_timeout = 10 +emailable.Client.read_timeout = 45 +``` + +The read timeout is separate from the `timeout` argument to `verify`, which +tells the API how long to spend verifying before returning a 249 response. + ### Verification ```python diff --git a/emailable/client.py b/emailable/client.py index 986f110..8b6376e 100644 --- a/emailable/client.py +++ b/emailable/client.py @@ -5,6 +5,8 @@ ServerUnavailableError) class Client: + open_timeout = 30 + read_timeout = 60 def __init__(self, api_key=None): self.api_key = api_key @@ -65,6 +67,10 @@ def __request(self, method, url, options, key_or_token): options['headers'] = { 'Authorization': f'Bearer {key_or_token or self.api_key}' } + # requests treats None as "wait forever". + if self.open_timeout is None or self.read_timeout is None: + raise ValueError('open_timeout and read_timeout cannot be None') + options['timeout'] = (self.open_timeout, self.read_timeout) try: response = requests.request(method, url, **options) response.raise_for_status() @@ -74,7 +80,7 @@ def __request(self, method, url, options, key_or_token): def __handle_error(self, e, response): status_code = response.status_code if response is not None else None - message = response.json()['message'] if response is not None else None + message = response.json()['message'] if response is not None else str(e) if status_code == 401 or status_code == 403: raise AuthError(message) elif status_code == 402: diff --git a/tests/test_timeout.py b/tests/test_timeout.py new file mode 100644 index 0000000..120be6e --- /dev/null +++ b/tests/test_timeout.py @@ -0,0 +1,43 @@ +from unittest import TestCase +from unittest.mock import Mock, patch +import requests +import emailable + +OK = Mock(status_code=200) +OK.json.return_value = {} + +@patch('emailable.client.requests.request', return_value=OK) +class TestTimeout(TestCase): + + def test_default_timeouts(self, request): + emailable.Client('key').account() + self.assertEqual(request.call_args[1]['timeout'], (30, 60)) + + @patch.object(emailable.Client, 'open_timeout', 5) + @patch.object(emailable.Client, 'read_timeout', 45) + def test_configured_timeouts(self, request): + emailable.Client('key').account() + self.assertEqual(request.call_args[1]['timeout'], (5, 45)) + + @patch.object(emailable.Client, 'open_timeout', None) + def test_open_timeout_cannot_be_none(self, request): + with self.assertRaises(ValueError): + emailable.Client('key').account() + request.assert_not_called() + + @patch.object(emailable.Client, 'read_timeout', None) + def test_read_timeout_cannot_be_none(self, request): + with self.assertRaises(ValueError): + emailable.Client('key').account() + request.assert_not_called() + + def test_verify_timeout_is_not_request_timeout(self, request): + emailable.Client('key').verify('evan@emailable.com', timeout=25) + self.assertEqual(request.call_args[1]['params']['timeout'], 25) + self.assertEqual(request.call_args[1]['timeout'], (30, 60)) + + def test_timeout_raises_client_error(self, request): + request.side_effect = requests.exceptions.ReadTimeout('read timed out') + with self.assertRaisesRegex(emailable.ClientError, 'read timed out'): + emailable.Client('key').account() + self.assertEqual(request.call_count, 1)