allow same name and add try for other requests

This commit is contained in:
Antonio J. Delgado 2024-11-11 10:19:56 +02:00
parent d120396957
commit 88b4dbf276

View file

@ -204,127 +204,158 @@ class NextcloudHandler:
def get(self, path):
'''Do a GET request'''
r = requests.get(
f'{self.http}://{self.host}/{path}',
auth=(self.user, self.token), verify=self.ssl, headers=self.headers(),
timeout=self.timeout
)
try:
r = requests.get(
f'{self.http}://{self.host}/{path}',
auth=(self.user, self.token), verify=self.ssl, headers=self.headers(),
timeout=self.timeout
)
if r.status_code == 200:
return r
elif r.status_code == 404:
self._log.error(f'File {path} does not exist')
sys.exit(3)
else:
self._log.error(r.status_code)
sys.exit(4)
if r.status_code == 200:
return r
elif r.status_code == 404:
self._log.error(
'Path %s does not exist',
path
)
sys.exit(3)
else:
self._log.error(r.status_code)
sys.exit(4)
except requests.exceptions.ReadTimeout as error:
self._log.error(
"Timeout (%s sec) error doing GET request. %s",
self.timeout,
error
)
def propfind(self, path):
'''Do a PROPFIND request'''
s = requests.Session()
s.auth = (self.user, self.token)
r = s.request(
method='PROPFIND',
url=f'{self.http}://{self.host}/{path}',
headers={'Depth': '0'},
data=DIOS_MIO,
verify=self.ssl
)
try:
r = s.request(
method='PROPFIND',
url=f'{self.http}://{self.host}/{path}',
headers={'Depth': '0'},
data=DIOS_MIO,
verify=self.ssl
)
if r.status_code == 207:
dom = minidom.parseString(r.text.encode('ascii', 'xmlcharrefreplace'))
try:
return {
'last_modified': dom.getElementsByTagName(
'd:getlastmodified'
)[0].firstChild.data,
'content_type': dom.getElementsByTagName(
'd:getcontenttype'
)[0].firstChild.data,
'file_id': int(dom.getElementsByTagName(
'oc:fileid'
)[0].firstChild.data),
'size': int(dom.getElementsByTagName(
'oc:size'
)[0].firstChild.data),
'favorite': int(dom.getElementsByTagName(
'oc:favorite'
)[0].firstChild.data),
'owner': dom.getElementsByTagName(
'oc:owner-display-name'
)[0].firstChild.data,
'href': dom.getElementsByTagName(
'd:href'
)[0].firstChild.data
}
except Exception:
# I guess it's folder, because it has no content_type
return {
'last_modified': dom.getElementsByTagName(
'd:getlastmodified'
)[0].firstChild.data,
'content_type': 'inode/directory',
'file_id': dom.getElementsByTagName(
'oc:fileid'
)[0].firstChild.data,
'size': dom.getElementsByTagName(
'oc:size'
)[0].firstChild.data,
'favorite': dom.getElementsByTagName(
'oc:favorite'
)[0].firstChild.data,
'owner': dom.getElementsByTagName(
'oc:owner-display-name'
)[0].firstChild.data,
'href': dom.getElementsByTagName(
'd:href'
)[0].firstChild.data
}
if r.status_code == 207:
dom = minidom.parseString(r.text.encode('ascii', 'xmlcharrefreplace'))
try:
return {
'last_modified': dom.getElementsByTagName(
'd:getlastmodified'
)[0].firstChild.data,
'content_type': dom.getElementsByTagName(
'd:getcontenttype'
)[0].firstChild.data,
'file_id': int(dom.getElementsByTagName(
'oc:fileid'
)[0].firstChild.data),
'size': int(dom.getElementsByTagName(
'oc:size'
)[0].firstChild.data),
'favorite': int(dom.getElementsByTagName(
'oc:favorite'
)[0].firstChild.data),
'owner': dom.getElementsByTagName(
'oc:owner-display-name'
)[0].firstChild.data,
'href': dom.getElementsByTagName(
'd:href'
)[0].firstChild.data
}
except Exception:
# I guess it's folder, because it has no content_type
return {
'last_modified': dom.getElementsByTagName(
'd:getlastmodified'
)[0].firstChild.data,
'content_type': 'inode/directory',
'file_id': dom.getElementsByTagName(
'oc:fileid'
)[0].firstChild.data,
'size': dom.getElementsByTagName(
'oc:size'
)[0].firstChild.data,
'favorite': dom.getElementsByTagName(
'oc:favorite'
)[0].firstChild.data,
'owner': dom.getElementsByTagName(
'oc:owner-display-name'
)[0].firstChild.data,
'href': dom.getElementsByTagName(
'd:href'
)[0].firstChild.data
}
elif r.status_code == 404:
return {}
else:
elif r.status_code == 404:
return {}
else:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
)
except requests.exceptions.ReadTimeout as error:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
"Timeout (%s sec) error doing GET request. %s",
self.timeout,
error
)
def put(self, path, src=None):
'''Do a PUT request'''
if src:
r = requests.put(
f'{self.http}://{self.host}/{path}',
data=open(src, 'rb'), auth=(self.user, self.token), verify=self.ssl, timeout=self.timeout
)
else:
r = requests.put(
f'{self.http}://{self.host}/{path}',
headers=self.headers, auth=(self.user, self.token), verify=self.ssl, timeout=self.timeout
)
try:
if src:
r = requests.put(
f'{self.http}://{self.host}/{path}',
data=open(src, 'rb'), auth=(self.user, self.token), verify=self.ssl, timeout=self.timeout
)
else:
r = requests.put(
f'{self.http}://{self.host}/{path}',
headers=self.headers, auth=(self.user, self.token), verify=self.ssl, timeout=self.timeout
)
if r.status_code in [200, 201, 204]:
return r, True
else:
if r.status_code in [200, 201, 204]:
return r, True
else:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
)
except requests.exceptions.ReadTimeout as error:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
"Timeout (%s sec) error doing GET request. %s",
self.timeout,
error
)
def delete(self, path):
'''Do a DELETE request'''
r = requests.delete(
f'{self.http}://{self.host}/{path}',
auth=(self.user, self.token), verify=self.ssl, timeout=self.timeout
)
try:
r = requests.delete(
f'{self.http}://{self.host}/{path}',
auth=(self.user, self.token), verify=self.ssl, timeout=self.timeout
)
if r.status_code in [200, 204]:
return r, True
elif r.status_code == 404:
return r, False
else:
if r.status_code in [200, 204]:
return r, True
elif r.status_code == 404:
return r, False
else:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
)
except requests.exceptions.ReadTimeout as error:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
"Timeout (%s sec) error doing GET request. %s",
self.timeout,
error
)
def talk(self, message, channel):
@ -335,21 +366,27 @@ class NextcloudHandler:
}
spreed_v1_path = "ocs/v2.php/apps/spreed/api/v1/chat"
try:
r = requests.post(
f'{self.http}://{self.host}/{spreed_v1_path}/{channel}',
data=body,
headers=self.headers(),
auth=(self.user, self.token),
verify=self.ssl, timeout=self.timeout
)
r = requests.post(
f'{self.http}://{self.host}/{spreed_v1_path}/{channel}',
data=body,
headers=self.headers(),
auth=(self.user, self.token),
verify=self.ssl, timeout=self.timeout
)
if r.status_code == 201:
return r, True
else:
if r.status_code == 201:
return r, True
else:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
)
except requests.exceptions.ReadTimeout as error:
self._log.error(
"Nextcloud instance returned status code: %s",
r.status_code
"Timeout (%s sec) error doing GET request. %s",
self.timeout,
error
)
def request_passwords_session(self):
@ -539,8 +576,6 @@ r.status_code
for password in self.list_passwords():
if self.is_same_password(obj, password):
return True
if password['label'] == obj['label']:
return True
return False
def create_password(self, post_obj):