Support multiple clients simultaneously

Fixed #34
This commit is contained in:
Evgeny Ezhov 2020-01-26 15:34:05 -08:00
parent fc14ed2be1
commit f3e7d44276
3 changed files with 48 additions and 22 deletions

View file

@ -0,0 +1,26 @@
import unittest
from tests.test_client_it import ClientTestCase
from webdav3.client import Client
class MultiClientTestCase(ClientTestCase):
options2 = {
'webdav_hostname': 'https://wrong.url.ru',
'webdav_login': 'webdavclient.test2',
'webdav_password': 'Qwerty123!',
'webdav_override_methods': {
'check': 'FAKE',
'download': 'FAKE',
'upload': 'FAKE',
'clean': 'FAKE',
}
}
def setUp(self):
super(ClientTestCase, self).setUp()
self.second_client = Client(self.options2)
if __name__ == '__main__':
unittest.main()

View file

@ -89,11 +89,8 @@ 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 = { default_http_header = {
'list': ["Accept: */*", "Depth: 1"], 'list': ["Accept: */*", "Depth: 1"],
'free': ["Accept: */*", "Depth: 0", "Content-Type: text/xml"], 'free': ["Accept: */*", "Depth: 0", "Content-Type: text/xml"],
'copy': ["Accept: */*"], 'copy': ["Accept: */*"],
@ -107,7 +104,7 @@ class Client(object):
} }
# mapping of actions to WebDAV methods # mapping of actions to WebDAV methods
requests = { default_requests = {
'options': 'OPTIONS', 'options': 'OPTIONS',
'download': "GET", 'download': "GET",
'upload': "PUT", 'upload': "PUT",
@ -150,6 +147,9 @@ class Client(object):
`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.
""" """
self.session = requests.Session()
self.http_header = Client.default_http_header.copy()
self.requests = Client.default_requests.copy()
webdav_options = get_options(option_type=WebDAVSettings, from_options=options) webdav_options = get_options(option_type=WebDAVSettings, from_options=options)
self.webdav = WebDAVSettings(webdav_options) self.webdav = WebDAVSettings(webdav_options)
@ -164,11 +164,11 @@ class Client(object):
the specified action. the specified action.
:return: the dictionary of headers for specified action. :return: the dictionary of headers for specified action.
""" """
if action in Client.http_header: if action in self.http_header:
try: try:
headers = Client.http_header[action].copy() headers = self.http_header[action].copy()
except AttributeError: except AttributeError:
headers = Client.http_header[action][:] headers = self.http_header[action][:]
else: else:
headers = list() headers = list()
@ -211,7 +211,7 @@ class Client(object):
if self.session.auth: 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) # (Re)Authenticates against the proxy
response = self.session.request( response = self.session.request(
method=Client.requests[action], method=self.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),
headers=self.get_headers(action, headers_ext), headers=self.get_headers(action, headers_ext),

View file

@ -27,20 +27,20 @@ class WebDAVSettings(ConnectionSettings):
keys = {'hostname', 'login', 'password', 'token', 'root', 'cert_path', 'key_path', 'recv_speed', 'send_speed', keys = {'hostname', 'login', 'password', 'token', 'root', 'cert_path', 'key_path', 'recv_speed', 'send_speed',
'verbose', 'disable_check', 'override_methods'} 'verbose', 'disable_check', 'override_methods'}
hostname = None
login = None
password = None
token = None
root = None
cert_path = None
key_path = None
recv_speed = None
send_speed = None
verbose = None
disable_check = False
override_methods = {}
def __init__(self, options): def __init__(self, options):
self.hostname = None
self.login = None
self.password = None
self.token = None
self.root = None
self.cert_path = None
self.key_path = None
self.recv_speed = None
self.send_speed = None
self.verbose = None
self.disable_check = False
self.override_methods = {}
self.options = dict() self.options = dict()
for key in self.keys: for key in self.keys: