[Jorge] Switch to use python sessions rather than requests

This commit is contained in:
Jorge 2019-09-30 19:54:25 +01:00 committed by Evgeny Ezhov
parent dc617d43aa
commit 5a01cbf823
3 changed files with 25 additions and 39 deletions

View file

@ -8,6 +8,23 @@ webdavclient3
Based on https://github.com/designerror/webdav-client-python Based on https://github.com/designerror/webdav-client-python
But uses `requests` instead of `PyCURL` But uses `requests` instead of `PyCURL`
Sample Usage
____________
>>> from webdav3.client
>>> options = {
... webdav_hostname : <hostname>
... webdav_login : <login>
... webdav_password : <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", <directory_name>)
Release Notes Release Notes
============= =============
**Version 0.13 TBD** **Version 0.13 TBD**

View file

@ -89,6 +89,9 @@ class Client(object):
# controls whether to verify the server's TLS certificate or not # controls whether to verify the server's TLS certificate or not
verify = True verify = True
# Sets the session for subsequent requests
session = requests.Session()
# HTTP headers for different actions # HTTP headers for different actions
http_header = { http_header = {
'list': ["Accept: */*", "Depth: 1"], 'list': ["Accept: */*", "Depth: 1"],
@ -155,7 +158,7 @@ class Client(object):
the specified action. the specified action.
:return: HTTP response of request. :return: HTTP response of request.
""" """
response = requests.request( response = self.session.request(
method=Client.requests[action], method=Client.requests[action],
url=self.get_url(path), url=self.get_url(path),
auth=(self.webdav.login, self.webdav.password), auth=(self.webdav.login, self.webdav.password),
@ -197,7 +200,7 @@ class Client(object):
def __init__(self, options): def __init__(self, options):
"""Constructor of WebDAV client """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: WebDev settings:
`webdav_hostname`: url for WebDAV server should contain protocol and ip address or domain name. `webdav_hostname`: url for WebDAV server should contain protocol and ip address or domain name.
Example: `https://webdav.server.com`. 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. `webdav_send_speed`: (optional) rate limit data upload speed in Bytes per second.
Defaults to unlimited speed. Defaults to unlimited speed.
`webdav_verbose`: (optional) set verbose mode on.off. By default verbose mode is off. `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) 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.webdav = WebDAVSettings(webdav_options)
self.proxy = ProxySettings(proxy_options)
self.default_options = {} self.default_options = {}
def valid(self): 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 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 @wrap_connection_error
def list(self, remote_path=root): def list(self, remote_path=root):

View file

@ -66,31 +66,3 @@ class WebDAVSettings(ConnectionSettings):
if not self.token and not self.login: if not self.token and not self.login:
raise OptionNotValid(name="login", value=self.login, ns=self.ns) 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)