Skip to content

Commit 6904c60

Browse files
authored
Add request timeout (10s default) (#56)
1 parent fb5f5b0 commit 6904c60

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ Constructor
4545
username,
4646
password,
4747
use_https=False,
48+
timeout=None,
4849
device_token=None,
4950
debugmode=False,
5051
)
5152
5253
``device_token`` should be added when using a two-step authentication account, otherwise DSM will ask to login with a One Time Password (OTP) and requests will fail (see the login section for more details).
5354

55+
Default ``timeout`` is 10 seconds.
5456

5557
Login
5658
------

synology_dsm/synology_dsm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ def __init__(
4545
username,
4646
password,
4747
use_https=False,
48+
timeout=None,
4849
device_token=None,
4950
debugmode=False,
5051
):
5152
self.username = username
5253
self._password = password
54+
self._timeout = timeout or 10
5355
self._debugmode = debugmode
5456

5557
# Session
@@ -250,9 +252,13 @@ def _execute_request(self, method, url, params, **kwargs):
250252
encoded_params = "&".join(
251253
"%s=%s" % (key, quote(str(value))) for key, value in items
252254
)
253-
resp = self._session.get(url, params=encoded_params, **kwargs)
255+
resp = self._session.get(
256+
url, params=encoded_params, timeout=self._timeout, **kwargs
257+
)
254258
elif method == "POST":
255-
resp = self._session.post(url, params=params, **kwargs)
259+
resp = self._session.post(
260+
url, params=params, timeout=self._timeout, **kwargs
261+
)
256262

257263
self._debuglog("Request url: " + resp.url)
258264
self._debuglog("Request status_code: " + str(resp.status_code))

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(
102102
username,
103103
password,
104104
use_https=False,
105+
timeout=None,
105106
device_token=None,
106107
debugmode=False,
107108
):
@@ -112,6 +113,7 @@ def __init__(
112113
username,
113114
password,
114115
use_https,
116+
timeout,
115117
device_token,
116118
debugmode,
117119
)

tests/test_synology_dsm.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828
from .const import SESSION_ID, DEVICE_TOKEN, SYNO_TOKEN
2929

30-
# pylint: disable=no-self-use,protected-access,anomalous-backslash-in-string
30+
# pylint: disable=no-self-use,protected-access
3131
class TestSynologyDSM(TestCase):
3232
"""SynologyDSM test cases."""
3333

@@ -42,6 +42,7 @@ def test_init(self):
4242
"""Test init."""
4343
assert self.api.username
4444
assert self.api._base_url
45+
assert self.api._timeout == 10
4546
assert not self.api.apis.get(API_AUTH)
4647
assert not self.api._session_id
4748

@@ -219,6 +220,13 @@ def test_login_2sa_failed(self):
219220
assert api._syno_token is None
220221
assert api._device_token is None
221222

223+
def test_request_timeout(self):
224+
"""Test request timeout."""
225+
api = SynologyDSMMock(
226+
VALID_HOST, VALID_PORT, VALID_USER, VALID_PASSWORD, VALID_SSL, timeout=2
227+
)
228+
assert api._timeout == 2
229+
222230
def test_request_get(self):
223231
"""Test get request."""
224232
assert self.api.get(API_INFO, "query")

0 commit comments

Comments
 (0)