Make request to Marketplace API when package list loads

We don't parse the response just yet, but this is part of the work.

Contributes to issue CURA-8556.
This commit is contained in:
Ghostkeeper 2021-10-21 15:33:37 +02:00
parent 5851ad52c6
commit 4337e81b77
No known key found for this signature in database
GPG key ID: D2A8871EE34EC59A
2 changed files with 37 additions and 5 deletions

View file

@ -1,14 +1,20 @@
# Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from cura.CuraApplication import CuraApplication
from cura.UltimakerCloud.UltimakerCloudScope import UltimakerCloudScope # To make requests to the Ultimaker API with correct authorization.
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, Qt
from typing import List, TYPE_CHECKING
from typing import List, Optional, TYPE_CHECKING
from UM.Qt.ListModel import ListModel
from UM.TaskManagement.HttpRequestManager import HttpRequestManager # To request the package list from the API.
from UM.TaskManagement.HttpRequestScope import JsonDecoratorScope # To request JSON responses from the API.
from .Marketplace import PACKAGES_URL # To get the list of packages.
from .PackageModel import PackageModel # This list is a list of PackageModels.
if TYPE_CHECKING:
from PyQt5.QtCore import QObject
from PyQt5.QtNetwork import QNetworkReply
class PackageList(ListModel):
"""
@ -26,7 +32,8 @@ class PackageList(ListModel):
super().__init__(parent)
self._packages: List[PackageModel] = []
self.setIsLoading(True)
self._is_loading = True
self._scope = JsonDecoratorScope(UltimakerCloudScope(CuraApplication.getInstance()))
self.requestFirst()
@ -38,6 +45,14 @@ class PackageList(ListModel):
"""
self.setIsLoading(True)
http = HttpRequestManager.getInstance()
http.get(
PACKAGES_URL,
scope = self._scope,
callback = self._parseResponse,
error_callback = self._onError
)
isLoadingChanged = pyqtSignal()
@pyqtSlot(bool)
@ -54,6 +69,23 @@ class PackageList(ListModel):
"""
return self._is_loading
def _parseResponse(self, reply: "QNetworkReply") -> None:
"""
Parse the response from the package list API request.
This converts that response into PackageModels, and triggers the ListModel to update.
:param reply: A reply containing information about a number of packages.
"""
pass # TODO: Parse reply dictionary.
def _onError(self, reply: "QNetworkReply", error: Optional["QNetworkReply.NetworkError"]) -> None:
"""
Handles networking and server errors when requesting the list of packages.
:param reply: The reply with packages. This will most likely be incomplete and should be ignored.
:param error: The error status of the request.
"""
pass # TODO: Handle errors.
def _update(self) -> None:
# TODO: Get list of packages from Marketplace class.
pass