From 8491fdec4c34c6ff615a0e54cdac23562c3f182b Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 30 Sep 2019 19:54:25 +0100 Subject: [PATCH] [Jorge] Switch to use python sessions rather than requests --- README.rst | 17 +++++++++++++++++ webdav3/client.py | 19 ++++++++----------- webdav3/connection.py | 28 ---------------------------- 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/README.rst b/README.rst index e0e2f31..10648c5 100644 --- a/README.rst +++ b/README.rst @@ -8,6 +8,23 @@ webdavclient3 Based on https://github.com/designerror/webdav-client-python But uses `requests` instead of `PyCURL` +Sample Usage +____________ + +>>> from webdav3.client +>>> options = { +... webdav_hostname : +... webdav_login : +... webdav_password : +... } +>>> client = Client(options) +>>> client.verify = False # To not check SSL certificates (Default = True) +>>> client.session.proxies(...) # To set proxy directly into the session (Optional) +>>> client.session.auth(...) # To set proxy auth directly into the session (Optional) +>>> client.execute_request("mkdir", ) + + + Release Notes ============= diff --git a/webdav3/client.py b/webdav3/client.py index 82b26d6..b471dbb 100644 --- a/webdav3/client.py +++ b/webdav3/client.py @@ -89,6 +89,9 @@ class Client(object): # controls whether to verify the server's TLS certificate or not verify = True + # Sets the session for subsequent requests + session = requests.Session() + # HTTP headers for different actions http_header = { 'list': ["Accept: */*", "Depth: 1"], @@ -155,7 +158,7 @@ class Client(object): the specified action. :return: HTTP response of request. """ - response = requests.request( + response = self.session.request( method=Client.requests[action], url=self.get_url(path), auth=(self.webdav.login, self.webdav.password), @@ -197,7 +200,7 @@ class Client(object): def __init__(self, options): """Constructor of WebDAV client - :param options: the dictionary of connection options to WebDAV can include proxy server options. + :param options: the dictionary of connection options to WebDAV. WebDev settings: `webdav_hostname`: url for WebDAV server should contain protocol and ip address or domain name. Example: `https://webdav.server.com`. @@ -212,25 +215,19 @@ class Client(object): `webdav_send_speed`: (optional) rate limit data upload speed in Bytes per second. Defaults to unlimited speed. `webdav_verbose`: (optional) set verbose mode on.off. By default verbose mode is off. - Proxy settings (optional): - `proxy_hostname`: url to proxy server should contain protocol and ip address or domain name and if needed - port. Example: `https://proxy.server.com:8383`. - `proxy_login`: login name for proxy server. - `proxy_password`: password for proxy server. + """ webdav_options = get_options(option_type=WebDAVSettings, from_options=options) - proxy_options = get_options(option_type=ProxySettings, from_options=options) self.webdav = WebDAVSettings(webdav_options) - self.proxy = ProxySettings(proxy_options) self.default_options = {} def valid(self): - """Validates of WebDAV and proxy settings. + """Validates of WebDAV settings. :return: True in case settings are valid and False otherwise. """ - return True if self.webdav.valid() and self.proxy.valid() else False + return True if self.webdav.valid() else False @wrap_connection_error def list(self, remote_path=root): diff --git a/webdav3/connection.py b/webdav3/connection.py index c781797..50becc6 100644 --- a/webdav3/connection.py +++ b/webdav3/connection.py @@ -66,31 +66,3 @@ class WebDAVSettings(ConnectionSettings): if not self.token and not self.login: raise OptionNotValid(name="login", value=self.login, ns=self.ns) - - -class ProxySettings(ConnectionSettings): - ns = "proxy:" - prefix = "proxy_" - keys = {'hostname', 'login', 'password'} - - hostname = None - login = None - password = None - - def __init__(self, options): - - self.options = dict() - - for key in self.keys: - value = options.get(key, '') - self.options[key] = value - self.__dict__[key] = value - - def is_valid(self): - - if self.password and not self.login: - raise OptionNotValid(name="login", value=self.login, ns=self.ns) - - if self.login or self.password: - if not self.hostname: - raise OptionNotValid(name="hostname", value=self.hostname, ns=self.ns)