Refactor UploadBackupJob to use HttpRequestManager

This commit is contained in:
Nino van Hooff 2020-02-28 17:37:21 +01:00
parent 19c3f765f5
commit 762f699f64
2 changed files with 25 additions and 22 deletions

View file

@ -63,7 +63,6 @@ class DriveApiService:
scope=self._jsonCloudScope scope=self._jsonCloudScope
) )
def createBackup(self) -> None: def createBackup(self) -> None:
self.creatingStateChanged.emit(is_creating = True) self.creatingStateChanged.emit(is_creating = True)
@ -87,9 +86,9 @@ class DriveApiService:
upload_backup_job.start() upload_backup_job.start()
def _onUploadFinished(self, job: "UploadBackupJob") -> None: def _onUploadFinished(self, job: "UploadBackupJob") -> None:
if job.backup_upload_error_message != "": if job.backup_upload_error_text != "":
# If the job contains an error message we pass it along so the UI can display it. # If the job contains an error message we pass it along so the UI can display it.
self.creatingStateChanged.emit(is_creating = False, error_message = job.backup_upload_error_message) self.creatingStateChanged.emit(is_creating = False, error_message = job.backup_upload_error_text)
else: else:
self.creatingStateChanged.emit(is_creating = False) self.creatingStateChanged.emit(is_creating = False)
@ -110,7 +109,7 @@ class DriveApiService:
) )
def _onRestoreRequestCompleted(self, reply: QNetworkReply, error: Optional["QNetworkReply.NetworkError"] = None, backup = None): def _onRestoreRequestCompleted(self, reply: QNetworkReply, error: Optional["QNetworkReply.NetworkError"] = None, backup = None):
if not DriveApiService._replyIndicatesSuccess(reply, error): if not HttpRequestManager.replyIndicatesSuccess(reply, error):
Logger.log("w", Logger.log("w",
"Requesting backup failed, response code %s while trying to connect to %s", "Requesting backup failed, response code %s while trying to connect to %s",
reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), reply.url()) reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), reply.url())
@ -153,11 +152,6 @@ class DriveApiService:
local_md5_hash = base64.b64encode(hashlib.md5(read_backup.read()).digest(), altchars = b"_-").decode("utf-8") local_md5_hash = base64.b64encode(hashlib.md5(read_backup.read()).digest(), altchars = b"_-").decode("utf-8")
return known_hash == local_md5_hash return known_hash == local_md5_hash
@staticmethod
def _replyIndicatesSuccess(reply: QNetworkReply, error: Optional["QNetworkReply.NetworkError"] = None):
"""Returns whether reply status code indicates success and error is None"""
return error is None and 200 <= reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) < 300
def deleteBackup(self, backup_id: str, callable: Callable[[bool], None]): def deleteBackup(self, backup_id: str, callable: Callable[[bool], None]):
def finishedCallback(reply: QNetworkReply, ca=callable) -> None: def finishedCallback(reply: QNetworkReply, ca=callable) -> None:
@ -174,7 +168,7 @@ class DriveApiService:
) )
def _onDeleteRequestCompleted(self, reply: QNetworkReply, error: Optional["QNetworkReply.NetworkError"] = None, callable = None): def _onDeleteRequestCompleted(self, reply: QNetworkReply, error: Optional["QNetworkReply.NetworkError"] = None, callable = None):
callable(DriveApiService._replyIndicatesSuccess(reply, error)) callable(HttpRequestManager.replyIndicatesSuccess(reply, error))
# Request a backup upload slot from the API. # Request a backup upload slot from the API.
# \param backup_metadata: A dict containing some meta data about the backup. # \param backup_metadata: A dict containing some meta data about the backup.

View file

@ -1,11 +1,11 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from PyQt5.QtNetwork import QNetworkReply
import requests
from UM.Job import Job from UM.Job import Job
from UM.Logger import Logger from UM.Logger import Logger
from UM.Message import Message from UM.Message import Message
from UM.TaskManagement.HttpRequestManager import HttpRequestManager
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura") catalog = i18nCatalog("cura")
@ -21,21 +21,30 @@ class UploadBackupJob(Job):
self._signed_upload_url = signed_upload_url self._signed_upload_url = signed_upload_url
self._backup_zip = backup_zip self._backup_zip = backup_zip
self._upload_success = False self._upload_success = False
self.backup_upload_error_message = "" self.backup_upload_error_text = ""
self._message = None
def run(self) -> None: def run(self) -> None:
upload_message = Message(catalog.i18nc("@info:backup_status", "Uploading your backup..."), title = self.MESSAGE_TITLE, progress = -1) self._message = Message(catalog.i18nc("@info:backup_status", "Uploading your backup..."), title = self.MESSAGE_TITLE, progress = -1)
upload_message.show() self._message.show()
backup_upload = requests.put(self._signed_upload_url, data = self._backup_zip) HttpRequestManager.getInstance().put(
upload_message.hide() self._signed_upload_url,
data = self._backup_zip
)
if backup_upload.status_code >= 300: def uploadFinishedCallback(self, reply: QNetworkReply, error: QNetworkReply.NetworkError):
self.backup_upload_error_message = backup_upload.text self._message.hide()
Logger.log("w", "Could not upload backup file: %s", backup_upload.text)
Message(catalog.i18nc("@info:backup_status", "There was an error while uploading your backup."), title = self.MESSAGE_TITLE).show() self.backup_upload_error_text = HttpRequestManager.readText(reply)
else:
if HttpRequestManager.replyIndicatesSuccess(reply, error):
self._upload_success = True self._upload_success = True
Message(catalog.i18nc("@info:backup_status", "Your backup has finished uploading."), title = self.MESSAGE_TITLE).show() Message(catalog.i18nc("@info:backup_status", "Your backup has finished uploading."), title = self.MESSAGE_TITLE).show()
else:
self.backup_upload_error_text = self.backup_upload_error_text
Logger.log("w", "Could not upload backup file: %s", self.backup_upload_error_text)
Message(catalog.i18nc("@info:backup_status", "There was an error while uploading your backup."),
title=self.MESSAGE_TITLE).show()
self.finished.emit(self) self.finished.emit(self)