From ebd31db7fc63fdb892bd420e99c3b3d6d83fa655 Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Tue, 2 Jun 2020 11:12:22 +0200 Subject: [PATCH 1/2] Add request timeout --- README.rst | 2 ++ synology_dsm/synology_dsm.py | 6 ++++-- tests/__init__.py | 2 ++ tests/test_synology_dsm.py | 9 ++++++++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 0e196edd..196b2f8a 100644 --- a/README.rst +++ b/README.rst @@ -45,12 +45,14 @@ Constructor username, password, use_https=False, + timeout=None, device_token=None, debugmode=False, ) ``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). +Default ``timeout`` is 10 seconds. Login ------ diff --git a/synology_dsm/synology_dsm.py b/synology_dsm/synology_dsm.py index 4ab19d9d..f18d1e73 100644 --- a/synology_dsm/synology_dsm.py +++ b/synology_dsm/synology_dsm.py @@ -45,11 +45,13 @@ def __init__( username, password, use_https=False, + timeout=None, device_token=None, debugmode=False, ): self.username = username self._password = password + self._timeout = timeout or 10 self._debugmode = debugmode # Session @@ -250,9 +252,9 @@ def _execute_request(self, method, url, params, **kwargs): encoded_params = "&".join( "%s=%s" % (key, quote(str(value))) for key, value in items ) - resp = self._session.get(url, params=encoded_params, **kwargs) + resp = self._session.get(url, params=encoded_params, timeout=self._timeout, **kwargs) elif method == "POST": - resp = self._session.post(url, params=params, **kwargs) + resp = self._session.post(url, params=params, timeout=self._timeout, **kwargs) self._debuglog("Request url: " + resp.url) self._debuglog("Request status_code: " + str(resp.status_code)) diff --git a/tests/__init__.py b/tests/__init__.py index 07e5890d..d3a2c876 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -102,6 +102,7 @@ def __init__( username, password, use_https=False, + timeout=None, device_token=None, debugmode=False, ): @@ -112,6 +113,7 @@ def __init__( username, password, use_https, + timeout, device_token, debugmode, ) diff --git a/tests/test_synology_dsm.py b/tests/test_synology_dsm.py index dccfbd38..abc6e691 100644 --- a/tests/test_synology_dsm.py +++ b/tests/test_synology_dsm.py @@ -27,7 +27,7 @@ ) from .const import SESSION_ID, DEVICE_TOKEN, SYNO_TOKEN -# pylint: disable=no-self-use,protected-access,anomalous-backslash-in-string +# pylint: disable=no-self-use,protected-access class TestSynologyDSM(TestCase): """SynologyDSM test cases.""" @@ -42,6 +42,7 @@ def test_init(self): """Test init.""" assert self.api.username assert self.api._base_url + assert self.api._timeout == 10 assert not self.api.apis.get(API_AUTH) assert not self.api._session_id @@ -219,6 +220,12 @@ def test_login_2sa_failed(self): assert api._syno_token is None assert api._device_token is None + def test_request_timeout(self): + api = SynologyDSMMock( + VALID_HOST, VALID_PORT, VALID_USER, VALID_PASSWORD, VALID_SSL, 2 + ) + assert api._timeout == 2 + def test_request_get(self): """Test get request.""" assert self.api.get(API_INFO, "query") From 4581dd6544f688f2f9def795b3dd7d60b8d956a7 Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Tue, 2 Jun 2020 13:19:31 +0200 Subject: [PATCH 2/2] Pipe --- synology_dsm/synology_dsm.py | 8 ++++++-- tests/test_synology_dsm.py | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/synology_dsm/synology_dsm.py b/synology_dsm/synology_dsm.py index f18d1e73..ac01abad 100644 --- a/synology_dsm/synology_dsm.py +++ b/synology_dsm/synology_dsm.py @@ -252,9 +252,13 @@ def _execute_request(self, method, url, params, **kwargs): encoded_params = "&".join( "%s=%s" % (key, quote(str(value))) for key, value in items ) - resp = self._session.get(url, params=encoded_params, timeout=self._timeout, **kwargs) + resp = self._session.get( + url, params=encoded_params, timeout=self._timeout, **kwargs + ) elif method == "POST": - resp = self._session.post(url, params=params, timeout=self._timeout, **kwargs) + resp = self._session.post( + url, params=params, timeout=self._timeout, **kwargs + ) self._debuglog("Request url: " + resp.url) self._debuglog("Request status_code: " + str(resp.status_code)) diff --git a/tests/test_synology_dsm.py b/tests/test_synology_dsm.py index abc6e691..e40f9824 100644 --- a/tests/test_synology_dsm.py +++ b/tests/test_synology_dsm.py @@ -221,8 +221,9 @@ def test_login_2sa_failed(self): assert api._device_token is None def test_request_timeout(self): + """Test request timeout.""" api = SynologyDSMMock( - VALID_HOST, VALID_PORT, VALID_USER, VALID_PASSWORD, VALID_SSL, 2 + VALID_HOST, VALID_PORT, VALID_USER, VALID_PASSWORD, VALID_SSL, timeout=2 ) assert api._timeout == 2