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))
|
|
@ -388,7 +388,7 @@ class Client(object):
|
|||
if os.path.isdir(local_path):
|
||||
self.upload_directory(local_path=local_path, remote_path=remote_path, progress=progress)
|
||||
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):
|
||||
|
||||
|
@ -414,7 +414,7 @@ class Client(object):
|
|||
self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
|
||||
|
||||
@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):
|
||||
raise LocalResourceNotFound(local_path)
|
||||
|
@ -431,37 +431,22 @@ class Client(object):
|
|||
raise RemoteParentNotFound(urn.path())
|
||||
|
||||
with open(local_path, "rb") as local_file:
|
||||
|
||||
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)
|
||||
if file_size > self.large_size:
|
||||
options['INFILESIZE_LARGE'] = file_size
|
||||
else:
|
||||
options['INFILESIZE'] = file_size
|
||||
raise ResourceTooBig(path=local_path, size=file_size)
|
||||
|
||||
response = requests.request(
|
||||
'PUT',
|
||||
options["URL"],
|
||||
method='PUT',
|
||||
url="{hostname}{root}{path}".format(**url),
|
||||
auth=(self.webdav.login, self.webdav.password),
|
||||
headers=self.get_header('upload_file'),
|
||||
data=buff
|
||||
data=local_file
|
||||
)
|
||||
if request.status_code == 507:
|
||||
if response.status_code == 507:
|
||||
raise NotEnoughSpace()
|
||||
|
||||
# request.close()
|
||||
|
||||
def upload_sync(self, remote_path, local_path, callback=None):
|
||||
|
||||
self.upload(local_path=local_path, remote_path=remote_path)
|
||||
|
|
|
@ -13,7 +13,8 @@ class OptionNotValid(NotValid):
|
|||
self.ns = ns
|
||||
|
||||
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):
|
||||
|
@ -48,6 +49,19 @@ class RemoteParentNotFound(NotFound):
|
|||
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):
|
||||
def __init__(self, name, server):
|
||||
self.name = name
|
||||
|
|
Loading…
Add table
Reference in a new issue