From 336db8ae19182992e3ad62c4a5f2a70e3be0d57a Mon Sep 17 00:00:00 2001 From: Evgeny Ezhov Date: Mon, 20 Jan 2020 17:31:25 -0800 Subject: [PATCH] Override methods for customizing communication with WebDAV servers. Fix #31 and #30 --- README.md | 23 +++++++++++++++++++++-- tests/base_client_it.py | 5 ++++- tests/test_client_it.py | 3 +++ webdav3/client.py | 5 +---- webdav3/connection.py | 3 ++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3027daa..53c148d 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,12 @@ client.execute_request("mkdir", 'directory_name') Webdav API ========== -Webdav API is a set of webdav methods of work with cloud storage. This set includes the following methods: +Webdav API is a set of webdav actions of work with cloud storage. This set includes the following actions: `check`, `free`, `info`, `list`, `mkdir`, `clean`, `copy`, `move`, `download`, `upload`, `publish` and `unpublish`. **Configuring the client** -Required keys for configuring client connection with WevDAV-server are webdav\_hostname and webdav\_login, webdav\_password. +Required keys for configuring client connection with WevDAV-server are `webdav_hostname` and `webdav_login`, `webdav_password`. ```python from webdav3.client import Client @@ -51,6 +51,25 @@ options = { client = Client(options) ``` +If your server does not support `HEAD` method or there are other reasons to override default WebDAV methods for actions use a dictionary option `webdav_override_methods`. +The key should be in the following list: `check`, `free`, `info`, `list`, `mkdir`, `clean`, `copy`, `move`, `download`, `upload`, + `publish` and `unpublish`. The value should a string name of WebDAV method, for example `GET`. + +```python +from webdav3.client import Client + +options = { + 'webdav_hostname': "https://webdav.server.ru", + 'webdav_login': "login", + 'webdav_password': "password", + 'webdav_override_methods': { + 'check': 'GET' + } + +} +client = Client(options) +``` + When a proxy server you need to specify settings to connect through it. ```python diff --git a/tests/base_client_it.py b/tests/base_client_it.py index b8bce2f..3b04cd4 100644 --- a/tests/base_client_it.py +++ b/tests/base_client_it.py @@ -21,7 +21,10 @@ class BaseClientTestCase(unittest.TestCase): options = { 'webdav_hostname': 'http://localhost:8585', 'webdav_login': 'alice', - 'webdav_password': 'secret1234' + 'webdav_password': 'secret1234', + 'webdav_override_methods': { + 'check': 'GET' + } } # options = { diff --git a/tests/test_client_it.py b/tests/test_client_it.py index 900df58..0998fe6 100644 --- a/tests/test_client_it.py +++ b/tests/test_client_it.py @@ -224,6 +224,9 @@ class ClientTestCase(BaseClientTestCase): def test_valid(self): self.assertTrue(self.client.valid()) + def test_check_is_overridden(self): + self.assertEqual('GET', self.client.requests['check']) + if __name__ == '__main__': unittest.main() diff --git a/webdav3/client.py b/webdav3/client.py index 766d0b8..0e85b08 100644 --- a/webdav3/client.py +++ b/webdav3/client.py @@ -153,10 +153,7 @@ class Client(object): webdav_options = get_options(option_type=WebDAVSettings, from_options=options) self.webdav = WebDAVSettings(webdav_options) - response = self.execute_request('options', '') - self.supported_methods = response.headers.get('Allow') - if 'HEAD' not in self.supported_methods: - self.requests['check'] = 'GET' + self.requests.update(self.webdav.override_methods) self.default_options = {} def get_headers(self, action, headers_ext=None): diff --git a/webdav3/connection.py b/webdav3/connection.py index ce99211..a122f86 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'} + 'verbose', 'disable_check', 'override_methods'} hostname = None login = None @@ -38,6 +38,7 @@ class WebDAVSettings(ConnectionSettings): send_speed = None verbose = None disable_check = False + override_methods = {} def __init__(self, options): self.options = dict()