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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion emailable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
ServerUnavailableError)

class Client:
open_timeout = 30
read_timeout = 60

def __init__(self, api_key=None):
self.api_key = api_key
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions tests/test_timeout.py
Original file line number Diff line number Diff line change
@@ -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)