From 8b66ada48bee1a721f48062d0322a495b80ca845 Mon Sep 17 00:00:00 2001 From: Evgeny Ezhov Date: Sat, 6 Jun 2020 17:33:28 -0700 Subject: [PATCH] #67 Ability to configure a requests timeout --- README.md | 14 ++++++++++++++ RELEASE_NOTES.md | 3 +++ tests/base_client_it.py | 1 + tests/test_client_it.py | 3 +++ tests/test_connection.py | 21 +++++++++++++++++++++ tests/test_tailing_slash_client_it.py | 3 +++ webdav3/client.py | 6 ++---- webdav3/connection.py | 8 +++++--- 8 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0f15fbc..5068c01 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,20 @@ options = { client = Client(options) ``` +For configuring a requests timeout you can use an option `webdav_timeout` with int value in seconds, by default the timeout is set to 30 seconds. + +```python +from webdav3.client import Client + +options = { + 'webdav_hostname': "https://webdav.server.ru", + 'webdav_login': "login", + 'webdav_password': "password", + 'webdav_timeout': 30 +} +client = Client(options) +``` + When a proxy server you need to specify settings to connect through it. ```python diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index d38be6a..56e4a9e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,8 @@ Release Notes ------------- +**Version 3.14.5** + * Ability to configure a requests timeout + **Version 3.14.4** * Fixed the issue with gzipped content on `download_from` diff --git a/tests/base_client_it.py b/tests/base_client_it.py index 491f450..54a8da1 100644 --- a/tests/base_client_it.py +++ b/tests/base_client_it.py @@ -36,6 +36,7 @@ class BaseClientTestCase(unittest.TestCase): 'webdav_hostname': 'http://localhost:8585', 'webdav_login': 'alice', 'webdav_password': 'secret1234', + 'webdav_timeout': 10, 'webdav_override_methods': { 'check': 'GET' } diff --git a/tests/test_client_it.py b/tests/test_client_it.py index 9d89406..8f0fe18 100644 --- a/tests/test_client_it.py +++ b/tests/test_client_it.py @@ -13,6 +13,9 @@ class ClientTestCase(BaseClientTestCase): pulled_file = BaseClientTestCase.local_path_dir + os.sep + BaseClientTestCase.local_file + def test_timeout_set(self): + self.assertEqual(10, self.client.timeout) + def test_list(self): self._prepare_for_downloading() file_list = self.client.list() diff --git a/tests/test_connection.py b/tests/test_connection.py index dff3772..d1d04ae 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -16,6 +16,27 @@ def test_connection_settings_valid(self): self.assertTrue(settings.is_valid()) self.assertTrue(settings.valid()) + def test_connection_settings_timeout_set(self): + options = { + 'webdav_hostname': 'http://localhost:8585', + 'webdav_login': 'alice', + 'webdav_password': 'secret1234', + 'timeout': 60 + } + webdav_options = get_options(option_type=WebDAVSettings, from_options=options) + settings = WebDAVSettings(webdav_options) + self.assertEqual(60, settings.timeout) + + def test_connection_settings_timeout_default(self): + options = { + 'webdav_hostname': 'http://localhost:8585', + 'webdav_login': 'alice', + 'webdav_password': 'secret1234' + } + webdav_options = get_options(option_type=WebDAVSettings, from_options=options) + settings = WebDAVSettings(webdav_options) + self.assertEqual(30, settings.timeout) + def test_connection_settings_no_hostname(self): options = { 'webdav_login': 'alice', diff --git a/tests/test_tailing_slash_client_it.py b/tests/test_tailing_slash_client_it.py index 6a8744c..92d2b11 100644 --- a/tests/test_tailing_slash_client_it.py +++ b/tests/test_tailing_slash_client_it.py @@ -13,6 +13,9 @@ class TailingSlashClientTestCase(ClientTestCase): } } + def test_timeout_set(self): + self.assertEqual(30, self.client.timeout) + def test_list_inner(self): self._prepare_for_downloading(True) file_list = self.client.list(self.remote_inner_path_dir) diff --git a/webdav3/client.py b/webdav3/client.py index 56c6030..6323484 100644 --- a/webdav3/client.py +++ b/webdav3/client.py @@ -79,9 +79,6 @@ class Client(object): # path to root directory of WebDAV root = '/' - # request timeout in seconds - timeout = 30 - # controls whether to verify the server's TLS certificate or not verify = True @@ -151,6 +148,7 @@ def __init__(self, options): self.webdav = WebDAVSettings(webdav_options) self.requests.update(self.webdav.override_methods) self.default_options = {} + self.timeout = self.webdav.timeout def get_headers(self, action, headers_ext=None): """Returns HTTP headers of specified WebDAV actions. @@ -205,7 +203,7 @@ def execute_request(self, action, path, data=None, headers_ext=None): :return: HTTP response of request. """ if self.session.auth: - self.session.request(method="GET", url=self.webdav.hostname, verify=self.verify) # (Re)Authenticates against the proxy + self.session.request(method="GET", url=self.webdav.hostname, verify=self.verify, timeout=self.timeout) # (Re)Authenticates against the proxy response = self.session.request( method=self.requests[action], url=self.get_url(path), diff --git a/webdav3/connection.py b/webdav3/connection.py index 499ba23..bba80db 100644 --- a/webdav3/connection.py +++ b/webdav3/connection.py @@ -25,7 +25,7 @@ class WebDAVSettings(ConnectionSettings): ns = "webdav:" prefix = "webdav_" keys = {'hostname', 'login', 'password', 'token', 'root', 'cert_path', 'key_path', 'recv_speed', 'send_speed', - 'verbose', 'disable_check', 'override_methods'} + 'verbose', 'disable_check', 'override_methods', 'timeout'} def __init__(self, options): self.hostname = None @@ -40,13 +40,15 @@ def __init__(self, options): self.verbose = None self.disable_check = False self.override_methods = {} + self.timeout = 30 self.options = dict() for key in self.keys: value = options.get(key, '') - self.options[key] = value - self.__dict__[key] = value + if not (self.__dict__[key] and not value): + self.options[key] = value + self.__dict__[key] = value self.root = Urn(self.root).quote() if self.root else '' self.root = self.root.rstrip(Urn.separate)