From 400121b5bc03996f36b278398425c17fd35a77e5 Mon Sep 17 00:00:00 2001 From: Igor Elpin Date: Wed, 28 Dec 2022 03:38:01 +0300 Subject: [PATCH] add optional retry on connection error --- nextcloud_client/nextcloud_client.py | 35 +++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/nextcloud_client/nextcloud_client.py b/nextcloud_client/nextcloud_client.py index 81a7e1e..6754023 100644 --- a/nextcloud_client/nextcloud_client.py +++ b/nextcloud_client/nextcloud_client.py @@ -332,6 +332,9 @@ def __init__(self, url, **kwargs): :param dav_endpoint_version: None (default) to force using a specific endpoint version instead of relying on capabilities :param debug: set to True to print debugging messages to stdout, defaults to False + :param retry_on_error: False(default) set to True to resend chunk on connection error + :param retry_attempts: 5(default) How many attempts to resend chunk + :param show_progress: Show upload % progress, default False """ if not url.endswith('/'): url += '/' @@ -341,6 +344,9 @@ def __init__(self, url, **kwargs): self._debug = kwargs.get('debug', False) self._verify_certs = kwargs.get('verify_certs', True) self._dav_endpoint_version = kwargs.get('dav_endpoint_version', True) + self._retry_on_error = kwargs.get('retry_on_error',False) + self._retry_attempts = kwargs.get('retry_attempts',5) + self._show_progress = kwargs.get('show_progress',False) self._capabilities = None self._version = None @@ -677,7 +683,8 @@ def _put_file_chunked(self, remote_path, local_source_file, **kwargs): chunk_index) else: chunk_name = remote_path - + if self._show_progress and chunk_index % 2 == 0: + print(f"Progress {int(100*chunk_index/int(chunk_count))} % {chunk_index}/{chunk_count}") if not self._make_dav_request( 'PUT', chunk_name, @@ -1800,11 +1807,27 @@ def _make_dav_request(self, method, path, **kwargs): print('Headers: ', kwargs.get('headers')) path = self._normalize_path(path) - res = self._session.request( - method, - self._webdav_url + parse.quote(self._encode_string(path)), - **kwargs - ) + try: + res = self._session.request( + method, + self._webdav_url + parse.quote(self._encode_string(path)), + **kwargs + ) + except requests.exceptions.ConnectionError as e: + if self._retry_on_error: + for i in range(0,self._retry_attempts): + try: + res = self._session.request( + method, + self._webdav_url + parse.quote(self._encode_string(path)), + **kwargs + ) + except requests.exceptions.ConnectionError as e: + print(f"{path} => {e} occured. Resend {i} times") + time.sleep(1) + continue + finally: + break if self._debug: print('DAV status: %i' % res.status_code) if res.status_code in [200, 207]: