mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-19 04:37:51 -06:00
Refactor refresh backups call to use HttpRequestManager
This commit is contained in:
parent
77590ad0e2
commit
86fb0383de
2 changed files with 24 additions and 25 deletions
|
@ -5,14 +5,19 @@ import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
from typing import Any, Optional, List, Dict
|
from typing import Any, Optional, List, Dict, Callable
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from UM.Logger import Logger
|
from UM.Logger import Logger
|
||||||
from UM.Message import Message
|
from UM.Message import Message
|
||||||
from UM.Signal import Signal, signalemitter
|
from UM.Signal import Signal, signalemitter
|
||||||
|
from UM.TaskManagement.HttpRequestManager import HttpRequestManager
|
||||||
|
from UM.TaskManagement.HttpRequestScope import JsonDecoratorScope
|
||||||
from cura.CuraApplication import CuraApplication
|
from cura.CuraApplication import CuraApplication
|
||||||
|
from plugins.Toolbox.src.UltimakerCloudScope import UltimakerCloudScope
|
||||||
|
|
||||||
|
from PyQt5.QtNetwork import QNetworkReply
|
||||||
|
|
||||||
from .UploadBackupJob import UploadBackupJob
|
from .UploadBackupJob import UploadBackupJob
|
||||||
from .Settings import Settings
|
from .Settings import Settings
|
||||||
|
@ -34,33 +39,24 @@ class DriveApiService:
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._cura_api = CuraApplication.getInstance().getCuraAPI()
|
self._cura_api = CuraApplication.getInstance().getCuraAPI()
|
||||||
|
self._scope = JsonDecoratorScope(UltimakerCloudScope(CuraApplication.getInstance()))
|
||||||
|
|
||||||
def getBackups(self) -> List[Dict[str, Any]]:
|
def getBackups(self, changed: Callable):
|
||||||
access_token = self._cura_api.account.accessToken
|
def callback(reply: QNetworkReply):
|
||||||
if not access_token:
|
backup_list_response = HttpRequestManager.readJSON(reply)
|
||||||
Logger.log("w", "Could not get access token.")
|
if "data" not in backup_list_response:
|
||||||
return []
|
Logger.log("w", "Could not get backups from remote, actual response body was: %s",
|
||||||
try:
|
str(backup_list_response))
|
||||||
backup_list_request = requests.get(self.BACKUP_URL, headers = {
|
changed([]) # empty list of backups
|
||||||
"Authorization": "Bearer {}".format(access_token)
|
|
||||||
})
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
Logger.logException("w", "Unable to connect with the server.")
|
|
||||||
return []
|
|
||||||
|
|
||||||
# HTTP status 300s mean redirection. 400s and 500s are errors.
|
changed(backup_list_response["data"])
|
||||||
# Technically 300s are not errors, but the use case here relies on "requests" to handle redirects automatically.
|
|
||||||
if backup_list_request.status_code >= 300:
|
|
||||||
Logger.log("w", "Could not get backups list from remote: %s", backup_list_request.text)
|
|
||||||
Message(catalog.i18nc("@info:backup_status", "There was an error listing your backups."), title = catalog.i18nc("@info:title", "Backup")).show()
|
|
||||||
return []
|
|
||||||
|
|
||||||
backup_list_response = backup_list_request.json()
|
HttpRequestManager.getInstance().get(
|
||||||
if "data" not in backup_list_response:
|
self.BACKUP_URL,
|
||||||
Logger.log("w", "Could not get backups from remote, actual response body was: %s", str(backup_list_response))
|
callback=callback,
|
||||||
return []
|
scope=self._scope
|
||||||
|
)
|
||||||
|
|
||||||
return backup_list_response["data"]
|
|
||||||
|
|
||||||
def createBackup(self) -> None:
|
def createBackup(self) -> None:
|
||||||
self.creatingStateChanged.emit(is_creating = True)
|
self.creatingStateChanged.emit(is_creating = True)
|
||||||
|
|
|
@ -133,7 +133,10 @@ class DrivePluginExtension(QObject, Extension):
|
||||||
|
|
||||||
@pyqtSlot(name = "refreshBackups")
|
@pyqtSlot(name = "refreshBackups")
|
||||||
def refreshBackups(self) -> None:
|
def refreshBackups(self) -> None:
|
||||||
self._backups = self._drive_api_service.getBackups()
|
self._drive_api_service.getBackups(self._backupsChangedCallback)
|
||||||
|
|
||||||
|
def _backupsChangedCallback(self, backups):
|
||||||
|
self.backups = backups
|
||||||
self.backupsChanged.emit()
|
self.backupsChanged.emit()
|
||||||
|
|
||||||
@pyqtProperty(bool, notify = restoringStateChanged)
|
@pyqtProperty(bool, notify = restoringStateChanged)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue