fixes #2 – client method upload_file won't work
This commit is contained in:
parent
ffe7d0178d
commit
b749855a80
4 changed files with 51 additions and 25 deletions
1
tests/res/test.txt
Normal file
1
tests/res/test.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
test content for testing of webdav client
|
26
tests/test_client.py
Normal file
26
tests/test_client.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from webdav2.client import Client
|
||||||
|
|
||||||
|
|
||||||
|
class ClientTestCase(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
options = {
|
||||||
|
'webdav_hostname': 'https://webdav.yandex.ru',
|
||||||
|
'webdav_login': 'webdavclient.test',
|
||||||
|
'webdav_password': 'Qwerty123!'
|
||||||
|
}
|
||||||
|
self.client = Client(options)
|
||||||
|
|
||||||
|
def test_list(self):
|
||||||
|
file_list = self.client.list()
|
||||||
|
self.assertIsNotNone(file_list, 'List of files should not be None')
|
||||||
|
self.assertGreater(file_list.__len__(), 0, 'Expected that amount of files more then 0')
|
||||||
|
|
||||||
|
def test_upload_file(self):
|
||||||
|
remote_path = 'test.txt'
|
||||||
|
local_path = './res/test.txt'
|
||||||
|
if self.client.check(remote_path=remote_path):
|
||||||
|
self.client.clean(remote_path=remote_path)
|
||||||
|
self.client.upload_file(remote_path=remote_path, local_path=local_path)
|
||||||
|
self.assertTrue(self.client.check(remote_path=remote_path))
|
|
@ -371,7 +371,7 @@ class Client(object):
|
||||||
if buff.tell() == 0:
|
if buff.tell() == 0:
|
||||||
data = buff.read()
|
data = buff.read()
|
||||||
else:
|
else:
|
||||||
data = buff
|
data = buff
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
'PUT',
|
'PUT',
|
||||||
options["URL"],
|
options["URL"],
|
||||||
|
@ -388,7 +388,7 @@ class Client(object):
|
||||||
if os.path.isdir(local_path):
|
if os.path.isdir(local_path):
|
||||||
self.upload_directory(local_path=local_path, remote_path=remote_path, progress=progress)
|
self.upload_directory(local_path=local_path, remote_path=remote_path, progress=progress)
|
||||||
else:
|
else:
|
||||||
self.upload_file(local_path=local_path, remote_path=remote_path, progress=progress)
|
self.upload_file(local_path=local_path, remote_path=remote_path)
|
||||||
|
|
||||||
def upload_directory(self, remote_path, local_path, progress=None):
|
def upload_directory(self, remote_path, local_path, progress=None):
|
||||||
|
|
||||||
|
@ -414,7 +414,7 @@ class Client(object):
|
||||||
self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
|
self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
|
||||||
|
|
||||||
@wrap_connection_error
|
@wrap_connection_error
|
||||||
def upload_file(self, remote_path, local_path, progress=None):
|
def upload_file(self, remote_path, local_path):
|
||||||
|
|
||||||
if not os.path.exists(local_path):
|
if not os.path.exists(local_path):
|
||||||
raise LocalResourceNotFound(local_path)
|
raise LocalResourceNotFound(local_path)
|
||||||
|
@ -431,37 +431,22 @@ class Client(object):
|
||||||
raise RemoteParentNotFound(urn.path())
|
raise RemoteParentNotFound(urn.path())
|
||||||
|
|
||||||
with open(local_path, "rb") as local_file:
|
with open(local_path, "rb") as local_file:
|
||||||
|
|
||||||
url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()}
|
url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()}
|
||||||
options = {
|
|
||||||
'URL': "{hostname}{root}{path}".format(**url),
|
|
||||||
'HTTPHEADER': self.get_header('upload_file'),
|
|
||||||
'UPLOAD': 1,
|
|
||||||
'READFUNCTION': local_file.read,
|
|
||||||
# 'NOPROGRESS': 0 if progress else 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# if progress:
|
|
||||||
# options["PROGRESSFUNCTION"] = progress
|
|
||||||
|
|
||||||
file_size = os.path.getsize(local_path)
|
file_size = os.path.getsize(local_path)
|
||||||
if file_size > self.large_size:
|
if file_size > self.large_size:
|
||||||
options['INFILESIZE_LARGE'] = file_size
|
raise ResourceTooBig(path=local_path, size=file_size)
|
||||||
else:
|
|
||||||
options['INFILESIZE'] = file_size
|
|
||||||
|
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
'PUT',
|
method='PUT',
|
||||||
options["URL"],
|
url="{hostname}{root}{path}".format(**url),
|
||||||
auth=(self.webdav.login, self.webdav.password),
|
auth=(self.webdav.login, self.webdav.password),
|
||||||
headers=self.get_header('upload_file'),
|
headers=self.get_header('upload_file'),
|
||||||
data=buff
|
data=local_file
|
||||||
)
|
)
|
||||||
if request.status_code == 507:
|
if response.status_code == 507:
|
||||||
raise NotEnoughSpace()
|
raise NotEnoughSpace()
|
||||||
|
|
||||||
# request.close()
|
|
||||||
|
|
||||||
def upload_sync(self, remote_path, local_path, callback=None):
|
def upload_sync(self, remote_path, local_path, callback=None):
|
||||||
|
|
||||||
self.upload(local_path=local_path, remote_path=remote_path)
|
self.upload(local_path=local_path, remote_path=remote_path)
|
||||||
|
|
|
@ -13,7 +13,8 @@ class OptionNotValid(NotValid):
|
||||||
self.ns = ns
|
self.ns = ns
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Option ({ns}{name}={value}) have invalid name or value".format(ns=self.ns, name=self.name, value=self.value)
|
return "Option ({ns}{name}={value}) have invalid name or value".format(ns=self.ns, name=self.name,
|
||||||
|
value=self.value)
|
||||||
|
|
||||||
|
|
||||||
class CertificateNotValid(NotValid):
|
class CertificateNotValid(NotValid):
|
||||||
|
@ -48,6 +49,19 @@ class RemoteParentNotFound(NotFound):
|
||||||
return "Remote parent for: {path} not found".format(path=self.path)
|
return "Remote parent for: {path} not found".format(path=self.path)
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceTooBig(WebDavException):
|
||||||
|
def __init__(self, path, size, max_size):
|
||||||
|
self.path = path
|
||||||
|
self.size = size
|
||||||
|
self.max_size = max_size
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Resource {path} is too big, it should be less then {max_size} but actually: {size}".format(
|
||||||
|
path=self.path,
|
||||||
|
max_size=self.max_size,
|
||||||
|
size=self.size)
|
||||||
|
|
||||||
|
|
||||||
class MethodNotSupported(WebDavException):
|
class MethodNotSupported(WebDavException):
|
||||||
def __init__(self, name, server):
|
def __init__(self, name, server):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -70,4 +84,4 @@ class NotEnoughSpace(WebDavException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Not enough space on the server"
|
return "Not enough space on the server"
|
||||||
|
|
Loading…
Add table
Reference in a new issue