Skip to content

Commit

Permalink
#67 Ability to configure a requests timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ezhov-evgeny committed Jun 7, 2020
1 parent d92d1b7 commit 8b66ada
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -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`

Expand Down
1 change: 1 addition & 0 deletions tests/base_client_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
3 changes: 3 additions & 0 deletions tests/test_client_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions tests/test_tailing_slash_client_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions webdav3/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 5 additions & 3 deletions webdav3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8b66ada

Please sign in to comment.