Skip to content

Commit

Permalink
Add timeout to all Tuya API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
purple4reina committed Jul 12, 2023
1 parent 9c4b7ae commit 8e593ce
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## Unreleased
### Features
+ Add optional timeout to all Tuya API calls

## 0.6.2
### Bug Fixes
Expand Down
8 changes: 6 additions & 2 deletions gosundpy/gosund.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

from .device import GosundDevice
from .exceptions import assert_response_success, GosundException
from .utils import cache_response
from .utils import cache_response, wrap_session_timeout

class Gosund(object):

def __init__(self, username, password, access_id, access_key, country=1,
endpoint='https://openapi.tuyaus.com', status_cache_seconds=None):
endpoint='https://openapi.tuyaus.com', status_cache_seconds=None,
timeout=None):
self.api = TuyaOpenAPI(endpoint, access_id, access_key,
auth_type=AuthType.CUSTOM)
if timeout is not None:
wrap_session_timeout(self.api.session, timeout)

resp = self.api.connect(username, password, country)
assert_response_success('connect to api', resp)
self.manager = TuyaDeviceManager(self.api, TuyaOpenMQ(self.api))
Expand Down
8 changes: 8 additions & 0 deletions gosundpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ def _call(*args, **kwargs):
_call.clear_cache = cache.reset
return _call
return _rate_limit

def wrap_session_timeout(session, _timeout):
def _wrap(fn):
@functools.wraps(fn)
def _request(*args, timeout=None, **kwargs):
return fn(*args, timeout=_timeout, **kwargs)
return _request
session.request = _wrap(session.request)
2 changes: 1 addition & 1 deletion gosundpy/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.6.2"
version = "0.7.0"
17 changes: 15 additions & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pytest
import requests
import responses

from gosundpy.gosund import Gosund
Expand All @@ -18,8 +19,20 @@ def gosund2():
def gosund_non_caching():
return _gosund(caching_secs=None)

@pytest.fixture
def gosund_timeout(monkeypatch):
timeouts = []
_orig_request = requests.Session.request
def _request(*args, timeout=None, **kwargs):
timeouts.append(timeout)
return _orig_request(*args, timeout=timeout, **kwargs)
monkeypatch.setattr('requests.Session.request', _request)
gosund = _gosund(timeout=10)
gosund.timeouts = timeouts
return gosund

@responses.activate
def _gosund(caching_secs=60):
def _gosund(caching_secs=60, timeout=None):
users_login_uri = f'{BASEURL}/users/login'
responses.add(
responses.POST,
Expand All @@ -28,7 +41,7 @@ def _gosund(caching_secs=60):
status=200,
)
return Gosund('username', 'password', 'access_id', 'access_key',
status_cache_seconds=caching_secs)
status_cache_seconds=caching_secs, timeout=timeout)

def patch_get_device(device_id, category):
responses.add(
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/gosund_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,13 @@ def test_gosund_get_device_statuses_non_caching(gosund_non_caching):
resp2 = gosund_non_caching.get_device_statuses()
assert resp1 == {_test_device_id_1: _test_status_1}
assert id(resp1) != id(resp2)

@responses.activate
def test_gosund_timeouts(gosund_timeout):
_patch_testing_requests()

assert gosund_timeout.timeouts.pop() == 10
device = gosund_timeout.get_device(_test_device_id_1)
assert gosund_timeout.timeouts.pop() == 10
device.get_status()
assert gosund_timeout.timeouts.pop() == 10

0 comments on commit 8e593ce

Please sign in to comment.