Add HttpRequestScope and fix the CompatibilityDialog

CURA-6983
This commit is contained in:
Nino van Hooff 2020-01-07 10:53:26 +01:00
parent 71000180fc
commit 6eab5e2492
3 changed files with 8 additions and 23 deletions

View file

@ -54,7 +54,7 @@ UM.Dialog{
} }
Repeater Repeater
{ {
model: toolbox.subscribedPackagesModel model: subscribedPackagesModel
Component Component
{ {
Item Item
@ -97,7 +97,7 @@ UM.Dialog{
} }
Repeater Repeater
{ {
model: toolbox.subscribedPackagesModel model: subscribedPackagesModel
Component Component
{ {
Item Item

View file

@ -1,6 +1,5 @@
import json import json
import os import os
import platform
from typing import Dict, Optional from typing import Dict, Optional
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
@ -10,6 +9,7 @@ from UM.Extension import Extension
from UM.Logger import Logger from UM.Logger import Logger
from UM.Message import Message from UM.Message import Message
from UM.PluginRegistry import PluginRegistry from UM.PluginRegistry import PluginRegistry
from UM.TaskManagement.HttpRequestScope import UltimakerCloudScope
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
from plugins.Toolbox.src.CloudApiModel import CloudApiModel from plugins.Toolbox.src.CloudApiModel import CloudApiModel
from plugins.Toolbox.src.SubscribedPackagesModel import SubscribedPackagesModel from plugins.Toolbox.src.SubscribedPackagesModel import SubscribedPackagesModel
@ -22,10 +22,10 @@ class SubscriptionChecker(QObject, Extension):
super().__init__() super().__init__()
self._application = application # type: CuraApplication self._application = application # type: CuraApplication
self._scope = UltimakerCloudScope(application)
self._model = SubscribedPackagesModel() self._model = SubscribedPackagesModel()
self._application.initializationFinished.connect(self._onAppInitialized) self._application.initializationFinished.connect(self._onAppInitialized)
self._application.getCuraAPI().account.accessTokenChanged.connect(self._updateRequestHeader)
# This is a plugin, so most of the components required are not ready when # This is a plugin, so most of the components required are not ready when
# this is initialized. Therefore, we wait until the application is ready. # this is initialized. Therefore, we wait until the application is ready.
@ -76,17 +76,16 @@ class SubscriptionChecker(QObject, Extension):
plugin_path_prefix = PluginRegistry.getInstance().getPluginPath(self.getPluginId()) plugin_path_prefix = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
if plugin_path_prefix: if plugin_path_prefix:
path = os.path.join(plugin_path_prefix, compatibility_dialog_path) path = os.path.join(plugin_path_prefix, compatibility_dialog_path)
self.compatibility_dialog_view = self._application.getInstance().createQmlComponent(path, {"toolbox": self}) self.compatibility_dialog_view = self._application.createQmlComponent(path, {"subscribedPackagesModel": self._model})
def _getUserPackages(self, request_type: str) -> None: def _getUserPackages(self, request_type: str) -> None:
Logger.log("d", "Requesting [%s] metadata from server.", request_type) Logger.log("d", "Requesting [%s] metadata from server.", request_type)
self._updateRequestHeader()
url = CloudApiModel.api_url_user_packages url = CloudApiModel.api_url_user_packages
self._application.getHttpRequestManager().get(url, self._application.getHttpRequestManager().get(url,
headers_dict = self._request_headers,
callback = self._onUserPackagesRequestFinished, callback = self._onUserPackagesRequestFinished,
error_callback = self._onUserPackagesRequestFinished) error_callback = self._onUserPackagesRequestFinished,
scope = self._scope)
def _onUserPackagesRequestFinished(self, def _onUserPackagesRequestFinished(self,
reply: "QNetworkReply", reply: "QNetworkReply",
@ -109,16 +108,3 @@ class SubscriptionChecker(QObject, Extension):
self._handleCompatibilityData(json_data["data"]) self._handleCompatibilityData(json_data["data"])
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
Logger.log("w", "Received invalid JSON for user packages") Logger.log("w", "Received invalid JSON for user packages")
def _updateRequestHeader(self):
# todo DRY, copied from Toolbox. To RequestManager?
self._request_headers = {
"User-Agent": "%s/%s (%s %s)" % (self._application.getApplicationName(),
self._application.getVersion(),
platform.system(),
platform.machine())
}
access_token = self._application.getCuraAPI().account.accessToken
if access_token:
self._request_headers["Authorization"] = "Bearer {}".format(access_token)

View file

@ -17,7 +17,6 @@ from UM.i18n import i18nCatalog
from UM.Version import Version from UM.Version import Version
from cura import ApplicationMetadata from cura import ApplicationMetadata
from cura import UltimakerCloudAuthentication
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
from cura.Machines.ContainerTree import ContainerTree from cura.Machines.ContainerTree import ContainerTree
from plugins.Toolbox.src.CloudApiModel import CloudApiModel from plugins.Toolbox.src.CloudApiModel import CloudApiModel
@ -45,7 +44,7 @@ class Toolbox(QObject, Extension):
self._download_request_data = None # type: Optional[HttpRequestData] self._download_request_data = None # type: Optional[HttpRequestData]
self._download_progress = 0 # type: float self._download_progress = 0 # type: float
self._is_downloading = False # type: bool self._is_downloading = False # type: bool
self._request_headers = dict() # type: Dict[str, str] self._request_headers = dict() # type: Dict[str, str] # todo DRY headers, use scope
self._updateRequestHeader() self._updateRequestHeader()
self._request_urls = {} # type: Dict[str, str] self._request_urls = {} # type: Dict[str, str]