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..ac01abad 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,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, **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..e40f9824 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,13 @@ def test_login_2sa_failed(self): assert api._syno_token is None 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, timeout=2 + ) + assert api._timeout == 2 + def test_request_get(self): """Test get request.""" assert self.api.get(API_INFO, "query")