-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ggravlingen/add-retry
Add retry
- Loading branch information
Showing
4 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
"""Implement an API wrapper around Ikea Tradfri.""" | ||
|
||
from .api import retry_timeout | ||
from .coap_cli import api_factory as cli_api_factory | ||
from .error import ( | ||
PyTradFriError, RequestError, ClientError, ServerError, RequestTimeout) | ||
from .gateway import Gateway | ||
|
||
__all__ = ['Gateway', 'cli_api_factory', 'PyTradFriError', 'RequestError', | ||
'ClientError', 'ServerError', 'RequestTimeout'] | ||
'ClientError', 'ServerError', 'RequestTimeout', 'retry_timeout'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""API utilities.""" | ||
|
||
from .error import RequestTimeout | ||
|
||
|
||
def retry_timeout(api, retries=3): | ||
"""Retry API call when a timeout occurs.""" | ||
def retry_api(*args, **kwargs): | ||
"""Retrying API.""" | ||
for i in range(1, retries + 1): | ||
try: | ||
return api(*args, **kwargs) | ||
except RequestTimeout: | ||
if i == retries: | ||
raise | ||
|
||
return retry_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Test API utilities.""" | ||
import pytest | ||
|
||
from pytradfri import retry_timeout, RequestTimeout | ||
|
||
|
||
def test_retry_timeout_passes_args(): | ||
"""Test passing args.""" | ||
calls = [] | ||
|
||
def api(*args, **kwargs): | ||
"""Mock api""" | ||
calls.append((args, kwargs)) | ||
|
||
retry_api = retry_timeout(api) | ||
|
||
retry_api(1, 2, hello='world') | ||
assert len(calls) == 1 | ||
args, kwargs = calls[0] | ||
assert args == (1, 2) | ||
assert kwargs == {'hello': 'world'} | ||
|
||
|
||
def test_retry_timeout_retries_timeouts(): | ||
"""Test retrying timeout.""" | ||
calls = [] | ||
|
||
def api(*args, **kwargs): | ||
"""Mock api""" | ||
calls.append((args, kwargs)) | ||
|
||
if len(calls) == 1: | ||
raise RequestTimeout() | ||
|
||
retry_api = retry_timeout(api, retries=2) | ||
|
||
retry_api(1, 2, hello='world') | ||
assert len(calls) == 2 | ||
|
||
|
||
def test_retry_timeout_raises_after_max_retries(): | ||
"""Test retrying timeout.""" | ||
calls = [] | ||
|
||
def api(*args, **kwargs): | ||
"""Mock api""" | ||
calls.append((args, kwargs)) | ||
|
||
raise RequestTimeout() | ||
|
||
retry_api = retry_timeout(api, retries=5) | ||
|
||
with pytest.raises(RequestTimeout): | ||
retry_api(1, 2, hello='world') | ||
|
||
assert len(calls) == 5 |