Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand Down
10 changes: 8 additions & 2 deletions synology_dsm/synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(
username,
password,
use_https=False,
timeout=None,
device_token=None,
debugmode=False,
):
Expand All @@ -112,6 +113,7 @@ def __init__(
username,
password,
use_https,
timeout,
device_token,
debugmode,
)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down