Skip to content

Commit

Permalink
Merge pull request #10 from ggravlingen/add-retry
Browse files Browse the repository at this point in the history
Add retry
  • Loading branch information
Patrik authored Apr 16, 2017
2 parents 5df870f + ef22106 commit ccb6007
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pytradfri/__init__.py
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']
17 changes: 17 additions & 0 deletions pytradfri/api.py
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
4 changes: 2 additions & 2 deletions pytradfri/coap_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

def api_factory(host, security_code):
"""Generate a request method."""
def request(method, path, data=None, *, parse_json=True):
def request(method, path, data=None, *, parse_json=True, timeout=10):
"""Make a request."""

path = '/'.join(str(v) for v in path)
Expand All @@ -34,7 +34,7 @@ def request(method, path, data=None, *, parse_json=True):
]

kwargs = {
'timeout': 10,
'timeout': timeout,
'stderr': subprocess.STDOUT,
}

Expand Down
56 changes: 56 additions & 0 deletions tests/test_api.py
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

0 comments on commit ccb6007

Please sign in to comment.