Fixed #24 an issue with checking resources on Yandex WebDAV

This commit is contained in:
Evgeny Ezhov 2019-11-28 23:16:35 +03:00
parent 0b9c61e7e7
commit 5f84bb1343
2 changed files with 61 additions and 49 deletions

View file

@ -279,6 +279,9 @@ res1.write_async(local_path="~/Downloads/file1", callback)
Release Notes Release Notes
------------- -------------
**Version 0.14 TBD**
* Fixed an issue with checking resources on Yandex WebDAV server
**Version 0.13 27.11.2019** **Version 0.13 27.11.2019**
* Main version of Python is updated up to 3.7 * Main version of Python is updated up to 3.7
* Switch to use python sessions rather than requests by https://github.com/delrey1 * Switch to use python sessions rather than requests by https://github.com/delrey1

View file

@ -106,6 +106,59 @@ class Client(object):
'set_property': ["Accept: */*", "Depth: 1", "Content-Type: application/x-www-form-urlencoded"] 'set_property': ["Accept: */*", "Depth: 1", "Content-Type: application/x-www-form-urlencoded"]
} }
# mapping of actions to WebDAV methods
requests = {
'options': 'OPTIONS',
'download': "GET",
'upload': "PUT",
'copy': "COPY",
'move': "MOVE",
'mkdir': "MKCOL",
'clean': "DELETE",
'check': "HEAD",
'list': "PROPFIND",
'free': "PROPFIND",
'info': "PROPFIND",
'publish': "PROPPATCH",
'unpublish': "PROPPATCH",
'published': "PROPPATCH",
'get_property': "PROPFIND",
'set_property': "PROPPATCH"
}
meta_xmlns = {
'https://webdav.yandex.ru': "urn:yandex:disk:meta",
}
def __init__(self, options):
"""Constructor of WebDAV client
:param options: the dictionary of connection options to WebDAV.
WebDev settings:
`webdav_hostname`: url for WebDAV server should contain protocol and ip address or domain name.
Example: `https://webdav.server.com`.
`webdav_login`: (optional) login name for WebDAV server can be empty in case using of token auth.
`webdav_password`: (optional) password for WebDAV server can be empty in case using of token auth.
`webdav_token': (optional) token for WebDAV server can be empty in case using of login/password auth.
`webdav_root`: (optional) root directory of WebDAV server. Defaults is `/`.
`webdav_cert_path`: (optional) path to certificate.
`webdav_key_path`: (optional) path to private key.
`webdav_recv_speed`: (optional) rate limit data download speed in Bytes per second.
Defaults to unlimited speed.
`webdav_send_speed`: (optional) rate limit data upload speed in Bytes per second.
Defaults to unlimited speed.
`webdav_verbose`: (optional) set verbose mode on.off. By default verbose mode is off.
"""
webdav_options = get_options(option_type=WebDAVSettings, from_options=options)
self.webdav = WebDAVSettings(webdav_options)
response = self.execute_request('options', '')
self.supported_methods = response.headers.get('Allow')
if 'HEAD' not in self.supported_methods:
self.requests['check'] = 'GET'
self.default_options = {}
def get_headers(self, action, headers_ext=None): def get_headers(self, action, headers_ext=None):
"""Returns HTTP headers of specified WebDAV actions. """Returns HTTP headers of specified WebDAV actions.
@ -180,54 +233,6 @@ class Client(object):
raise ResponseErrorCode(url=self.get_url(path), code=response.status_code, message=response.content) raise ResponseErrorCode(url=self.get_url(path), code=response.status_code, message=response.content)
return response return response
# mapping of actions to WebDAV methods
requests = {
'download': "GET",
'upload': "PUT",
'copy': "COPY",
'move': "MOVE",
'mkdir': "MKCOL",
'clean': "DELETE",
'check': "GET",
'list': "PROPFIND",
'free': "PROPFIND",
'info': "PROPFIND",
'publish': "PROPPATCH",
'unpublish': "PROPPATCH",
'published': "PROPPATCH",
'get_property': "PROPFIND",
'set_property': "PROPPATCH"
}
meta_xmlns = {
'https://webdav.yandex.ru': "urn:yandex:disk:meta",
}
def __init__(self, options):
"""Constructor of WebDAV client
:param options: the dictionary of connection options to WebDAV.
WebDev settings:
`webdav_hostname`: url for WebDAV server should contain protocol and ip address or domain name.
Example: `https://webdav.server.com`.
`webdav_login`: (optional) login name for WebDAV server can be empty in case using of token auth.
`webdav_password`: (optional) password for WebDAV server can be empty in case using of token auth.
`webdav_token': (optional) token for WebDAV server can be empty in case using of login/password auth.
`webdav_root`: (optional) root directory of WebDAV server. Defaults is `/`.
`webdav_cert_path`: (optional) path to certificate.
`webdav_key_path`: (optional) path to private key.
`webdav_recv_speed`: (optional) rate limit data download speed in Bytes per second.
Defaults to unlimited speed.
`webdav_send_speed`: (optional) rate limit data upload speed in Bytes per second.
Defaults to unlimited speed.
`webdav_verbose`: (optional) set verbose mode on.off. By default verbose mode is off.
"""
webdav_options = get_options(option_type=WebDAVSettings, from_options=options)
self.webdav = WebDAVSettings(webdav_options)
self.default_options = {}
def valid(self): def valid(self):
"""Validates of WebDAV settings. """Validates of WebDAV settings.
@ -300,7 +305,11 @@ class Client(object):
if not self.check(directory_urn.parent()): if not self.check(directory_urn.parent()):
raise RemoteParentNotFound(directory_urn.path()) raise RemoteParentNotFound(directory_urn.path())
try:
response = self.execute_request(action='mkdir', path=directory_urn.quote()) response = self.execute_request(action='mkdir', path=directory_urn.quote())
except MethodNotSupported:
# Yandex WebDAV returns 405 status code when directory already exists
return True
return response.status_code in (200, 201) return response.status_code in (200, 201)
@wrap_connection_error @wrap_connection_error