From 44ec8fcc66451396bc870999ce29c9d1fd794695 Mon Sep 17 00:00:00 2001 From: "evgeny.ezhov" Date: Mon, 27 Nov 2017 22:16:17 +0300 Subject: [PATCH] Fixed problem when connection lost during request executing and nothing was happened, now it raises an exception. --- tests/test_client_it.py | 5 +++++ tests/test_client_unit.py | 5 +++++ webdav3/client.py | 9 +++++++-- webdav3/exceptions.py | 19 ++++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/test_client_it.py b/tests/test_client_it.py index 51138e0..a72cb3c 100644 --- a/tests/test_client_it.py +++ b/tests/test_client_it.py @@ -1,5 +1,6 @@ import os.path import shutil +import unittest from io import BytesIO, StringIO from os import path from unittest import TestCase @@ -188,3 +189,7 @@ class ClientTestCase(TestCase): os.mkdir(self.local_path_dir) if not path.exists(path=self.local_path_dir + os.sep + self.local_path_file): shutil.copy(src=self.local_path_file, dst=self.local_path_dir + os.sep + self.local_path_file) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_client_unit.py b/tests/test_client_unit.py index c7d1630..83732f2 100644 --- a/tests/test_client_unit.py +++ b/tests/test_client_unit.py @@ -1,3 +1,4 @@ +import unittest from unittest import TestCase from lxml.etree import ElementTree, Element @@ -93,3 +94,7 @@ class ClientTestCase(TestCase): tree = ElementTree(Element('test')) result = utils.etree_to_string(tree) self.assertEquals(result, '\n') + + +if __name__ == '__main__': + unittest.main() diff --git a/webdav3/client.py b/webdav3/client.py index 0ec4292..aa07880 100644 --- a/webdav3/client.py +++ b/webdav3/client.py @@ -68,10 +68,11 @@ def wrap_connection_error(fn): try: res = fn(self, *args, **kw) except requests.ConnectionError: - raise NotConnection(self.webdav.hostname) + raise NoConnection(self.webdav.hostname) + except requests.RequestException as re: + raise ConnectionException(re) else: return res - return _wrapper @@ -84,6 +85,9 @@ class Client(object): # Max size of file for uploading large_size = 2 * 1024 * 1024 * 1024 + # request timeout in seconds + timeout = 30 + # HTTP headers for different actions http_header = { 'list': ["Accept: */*", "Depth: 1"], @@ -155,6 +159,7 @@ class Client(object): url=self.get_url(path), auth=(self.webdav.login, self.webdav.password), headers=self.get_headers(action, headers_ext), + timeout=self.timeout, data=data ) if response.status_code == 507: diff --git a/webdav3/exceptions.py b/webdav3/exceptions.py index 7cfb02f..b30abbc 100644 --- a/webdav3/exceptions.py +++ b/webdav3/exceptions.py @@ -71,7 +71,15 @@ class MethodNotSupported(WebDavException): return "Method {name} not supported for {server}".format(name=self.name, server=self.server) -class NotConnection(WebDavException): +class ConnectionException(WebDavException): + def __init__(self, exception): + self.exception = exception + + def __str__(self): + return self.exception.__str__() + + +class NoConnection(WebDavException): def __init__(self, hostname): self.hostname = hostname @@ -79,6 +87,15 @@ class NotConnection(WebDavException): return "Not connection with {hostname}".format(hostname=self.hostname) +# This exception left only for supporting original library interface. +class NotConnection(WebDavException): + def __init__(self, hostname): + self.hostname = hostname + + def __str__(self): + return "No connection with {hostname}".format(hostname=self.hostname) + + class ResponseErrorCode(WebDavException): def __init__(self, url, code, message): self.url = url