un-/subscribe the user to installed packages

Contributes to: CURA-8587
This commit is contained in:
Jelle Spijker 2021-12-03 17:08:28 +01:00
parent a83a598e96
commit 743ac67cdb
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A
6 changed files with 25 additions and 17 deletions

View file

@ -0,0 +1,11 @@
# Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from cura.UltimakerCloud import UltimakerCloudConstants
from cura.ApplicationMetadata import CuraSDKVersion
ROOT_URL = f"{UltimakerCloudConstants.CuraCloudAPIRoot}/cura-packages/v{UltimakerCloudConstants.CuraCloudAPIVersion}"
ROOT_CURA_URL = f"{ROOT_URL}/cura/v{CuraSDKVersion}" # Root of all Marketplace API requests.
ROOT_USER_URL = f"{ROOT_URL}/user"
PACKAGES_URL = f"{ROOT_CURA_URL}/packages" # URL to use for requesting the list of packages.
PACKAGE_UPDATES_URL = f"{PACKAGES_URL}/package-updates" # URL to use for requesting the list of packages that can be updated.
USER_PACKAGES_URL = f"{ROOT_USER_URL}/packages"

View file

@ -15,7 +15,7 @@ from UM.Logger import Logger
from .PackageList import PackageList from .PackageList import PackageList
from .PackageModel import PackageModel from .PackageModel import PackageModel
from . import Marketplace from .Constants import PACKAGE_UPDATES_URL
catalog = i18nCatalog("cura") catalog = i18nCatalog("cura")
@ -66,7 +66,7 @@ class LocalPackageList(PackageList):
def checkForUpdates(self, packages: List[Dict[str, Any]]): def checkForUpdates(self, packages: List[Dict[str, Any]]):
installed_packages = "installed_packages=".join([f"{package['package_id']}:{package['package_version']}&" for package in packages]) installed_packages = "installed_packages=".join([f"{package['package_id']}:{package['package_version']}&" for package in packages])
request_url = f"{Marketplace.PACKAGE_UPDATES_URL}?installed_packages={installed_packages[:-1]}" request_url = f"{PACKAGE_UPDATES_URL}?installed_packages={installed_packages[:-1]}"
self._ongoing_request = HttpRequestManager.getInstance().get( self._ongoing_request = HttpRequestManager.getInstance().get(
request_url, request_url,

View file

@ -6,9 +6,7 @@ from PyQt5.QtCore import pyqtSlot
from PyQt5.QtQml import qmlRegisterType from PyQt5.QtQml import qmlRegisterType
from typing import Optional, TYPE_CHECKING from typing import Optional, TYPE_CHECKING
from cura.ApplicationMetadata import CuraSDKVersion
from cura.CuraApplication import CuraApplication # Creating QML objects and managing packages. from cura.CuraApplication import CuraApplication # Creating QML objects and managing packages.
from cura.UltimakerCloud import UltimakerCloudConstants
from UM.Extension import Extension # We are implementing the main object of an extension here. from UM.Extension import Extension # We are implementing the main object of an extension here.
from UM.PluginRegistry import PluginRegistry # To find out where we are stored (the proper way). from UM.PluginRegistry import PluginRegistry # To find out where we are stored (the proper way).
@ -19,10 +17,6 @@ from .LocalPackageList import LocalPackageList # To register this type with QML
if TYPE_CHECKING: if TYPE_CHECKING:
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
ROOT_URL = f"{UltimakerCloudConstants.CuraCloudAPIRoot}/cura-packages/v{UltimakerCloudConstants.CuraCloudAPIVersion}/cura/v{CuraSDKVersion}" # Root of all Marketplace API requests.
PACKAGES_URL = f"{ROOT_URL}/packages" # URL to use for requesting the list of packages.
PACKAGE_UPDATES_URL = f"{PACKAGES_URL}/package-updates" # URL to use for requesting the list of packages that can be updated.
class Marketplace(Extension): class Marketplace(Extension):
""" """

View file

@ -1,6 +1,7 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 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.
import tempfile import tempfile
import json
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, Qt from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, Qt
from typing import Dict, Optional, TYPE_CHECKING from typing import Dict, Optional, TYPE_CHECKING
@ -17,6 +18,7 @@ from cura import CuraPackageManager
from cura.UltimakerCloud.UltimakerCloudScope import UltimakerCloudScope # To make requests to the Ultimaker API with correct authorization. from cura.UltimakerCloud.UltimakerCloudScope import UltimakerCloudScope # To make requests to the Ultimaker API with correct authorization.
from .PackageModel import PackageModel from .PackageModel import PackageModel
from .Constants import USER_PACKAGES_URL
if TYPE_CHECKING: if TYPE_CHECKING:
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
@ -183,14 +185,14 @@ class PackageList(ListModel):
package.setIsUpdating(False) package.setIsUpdating(False)
else: else:
package.setIsInstalling(False) package.setIsInstalling(False)
# self._subscribe(package_id) self._subscribe(package_id, str(package.sdk_version))
def _subscribe(self, package_id: str) -> None: def _subscribe(self, package_id: str, sdk_version: str) -> None:
if self._account.isLoggedIn: if self._account.isLoggedIn:
Logger.debug(f"Subscribing the user for package: {package_id}") Logger.debug(f"Subscribing the user for package: {package_id}")
self._ongoing_request = HttpRequestManager.getInstance().put( HttpRequestManager.getInstance().put(
url = "", url = USER_PACKAGES_URL,
data = {}, data = json.dumps({"data": {"package_id": package_id, "sdk_version": sdk_version}}).encode(),
scope = self._scope scope = self._scope
) )
@ -201,12 +203,12 @@ class PackageList(ListModel):
self._manager.removePackage(package_id) self._manager.removePackage(package_id)
package.setIsInstalling(False) package.setIsInstalling(False)
package.setManageInstallState(False) package.setManageInstallState(False)
#self._unsunscribe(package_id) self._unsunscribe(package_id)
def _unsunscribe(self, package_id: str) -> None: def _unsunscribe(self, package_id: str) -> None:
if self._account.isLoggedIn: if self._account.isLoggedIn:
Logger.debug(f"Unsubscribing the user for package: {package_id}") Logger.debug(f"Unsubscribing the user for package: {package_id}")
self._ongoing_request = HttpRequestManager.getInstance().delete(url = "", scope = self._scope) HttpRequestManager.getInstance().delete(url = f"{USER_PACKAGES_URL}/{package_id}", scope = self._scope)
@pyqtSlot(str) @pyqtSlot(str)
def updatePackage(self, package_id): def updatePackage(self, package_id):

View file

@ -65,6 +65,7 @@ class PackageModel(QObject):
self._is_installing = False self._is_installing = False
self._is_updating = False self._is_updating = False
self._section_title = section_title self._section_title = section_title
self.sdk_version = package_data.get("sdk_version_semver", "")
# Note that there's a lot more info in the package_data than just these specified here. # Note that there's a lot more info in the package_data than just these specified here.
def __eq__(self, other: Union[str, "PackageModel"]): def __eq__(self, other: Union[str, "PackageModel"]):

View file

@ -9,7 +9,7 @@ from UM.i18n import i18nCatalog
from UM.Logger import Logger from UM.Logger import Logger
from UM.TaskManagement.HttpRequestManager import HttpRequestManager # To request the package list from the API. from UM.TaskManagement.HttpRequestManager import HttpRequestManager # To request the package list from the API.
from . import Marketplace # To get the list of packages. Imported this way to prevent circular imports. from .Constants import PACKAGES_URL # To get the list of packages. Imported this way to prevent circular imports.
from .PackageList import PackageList from .PackageList import PackageList
from .PackageModel import PackageModel # The contents of this list. from .PackageModel import PackageModel # The contents of this list.
@ -108,7 +108,7 @@ class RemotePackageList(PackageList):
Get the URL to request the first paginated page with. Get the URL to request the first paginated page with.
:return: A URL to request. :return: A URL to request.
""" """
request_url = f"{Marketplace.PACKAGES_URL}?limit={self.ITEMS_PER_PAGE}" request_url = f"{PACKAGES_URL}?limit={self.ITEMS_PER_PAGE}"
if self._package_type_filter != "": if self._package_type_filter != "":
request_url += f"&package_type={self._package_type_filter}" request_url += f"&package_type={self._package_type_filter}"
if self._current_search_string != "": if self._current_search_string != "":