mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-12 09:17:50 -06:00
Parse responses from package API call
Only positive responses so far. Error handling is not implemented yet. Contributes to issue CURA-8556.
This commit is contained in:
parent
3138452f94
commit
6415a2649e
2 changed files with 22 additions and 5 deletions
|
@ -76,7 +76,14 @@ class PackageList(ListModel):
|
|||
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.
|
||||
response_data = HttpRequestManager.readJSON(reply)
|
||||
if "data" not in response_data:
|
||||
return # TODO: Handle invalid response.
|
||||
|
||||
for package_data in response_data["data"]:
|
||||
package = PackageModel(package_data, parent = self)
|
||||
self._packages.append(package)
|
||||
self._update()
|
||||
|
||||
def _onError(self, reply: "QNetworkReply", error: Optional["QNetworkReply.NetworkError"]) -> None:
|
||||
"""
|
||||
|
|
|
@ -2,19 +2,29 @@
|
|||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
from PyQt5.QtCore import pyqtProperty, QObject
|
||||
from typing import Any, Dict
|
||||
|
||||
from UM.i18n import i18nCatalog # To translate placeholder names if data is not present.
|
||||
|
||||
catalog = i18nCatalog("cura")
|
||||
|
||||
class PackageModel(QObject):
|
||||
"""
|
||||
Represents a package, containing all the relevant information to be displayed about a package.
|
||||
|
||||
Effectively this behaves like a glorified named tuple, but as a QObject so that its properties can be obtained from
|
||||
QML.
|
||||
QML. The model can also be constructed directly from a response received by the API.
|
||||
"""
|
||||
|
||||
def __init__(self, package_id: str, display_name: str, parent: QObject = None):
|
||||
def __init__(self, package_data: Dict[str, Any], parent: QObject = None):
|
||||
"""
|
||||
Constructs a new model for a single package.
|
||||
:param package_data: The data received from the Marketplace API about the package to create.
|
||||
:param parent: The parent QML object that controls the lifetime of this model (normally a PackageList).
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self._package_id = package_id
|
||||
self._display_name = display_name
|
||||
self._package_id = package_data.get("package_id", "UnknownPackageId")
|
||||
self._display_name = package_data.get("display_name", catalog.i18nc("@label:property", "Unknown Package"))
|
||||
|
||||
@pyqtProperty(str, constant = True)
|
||||
def packageId(self) -> str:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue