Browse Source

un-/subscribe the user to installed packages

Contributes to: CURA-8587
Jelle Spijker 3 years ago
parent
commit
743ac67cdb

+ 11 - 0
plugins/Marketplace/Constants.py

@@ -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"

+ 2 - 2
plugins/Marketplace/LocalPackageList.py

@@ -15,7 +15,7 @@ from UM.Logger import Logger
 
 from .PackageList import PackageList
 from .PackageModel import PackageModel
-from . import Marketplace
+from .Constants import PACKAGE_UPDATES_URL
 
 catalog = i18nCatalog("cura")
 
@@ -66,7 +66,7 @@ class LocalPackageList(PackageList):
 
     def checkForUpdates(self, packages: List[Dict[str, Any]]):
         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(
             request_url,

+ 0 - 6
plugins/Marketplace/Marketplace.py

@@ -6,9 +6,7 @@ from PyQt5.QtCore import pyqtSlot
 from PyQt5.QtQml import qmlRegisterType
 from typing import Optional, TYPE_CHECKING
 
-from cura.ApplicationMetadata import CuraSDKVersion
 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.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:
     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):
     """

+ 9 - 7
plugins/Marketplace/PackageList.py

@@ -1,6 +1,7 @@
 # Copyright (c) 2021 Ultimaker B.V.
 # Cura is released under the terms of the LGPLv3 or higher.
 import tempfile
+import json
 
 from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, Qt
 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 .PackageModel import PackageModel
+from .Constants import USER_PACKAGES_URL
 
 if TYPE_CHECKING:
     from PyQt5.QtCore import QObject
@@ -183,14 +185,14 @@ class PackageList(ListModel):
             package.setIsUpdating(False)
         else:
             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:
             Logger.debug(f"Subscribing the user for package: {package_id}")
-            self._ongoing_request = HttpRequestManager.getInstance().put(
-                url = "",
-                data = {},
+            HttpRequestManager.getInstance().put(
+                url = USER_PACKAGES_URL,
+                data = json.dumps({"data": {"package_id": package_id, "sdk_version": sdk_version}}).encode(),
                 scope = self._scope
             )
 
@@ -201,12 +203,12 @@ class PackageList(ListModel):
         self._manager.removePackage(package_id)
         package.setIsInstalling(False)
         package.setManageInstallState(False)
-        #self._unsunscribe(package_id)
+        self._unsunscribe(package_id)
 
     def _unsunscribe(self, package_id: str) -> None:
         if self._account.isLoggedIn:
             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)
     def updatePackage(self, package_id):

+ 1 - 0
plugins/Marketplace/PackageModel.py

@@ -65,6 +65,7 @@ class PackageModel(QObject):
         self._is_installing = False
         self._is_updating = False
         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.
 
     def __eq__(self, other: Union[str, "PackageModel"]):

+ 2 - 2
plugins/Marketplace/RemotePackageList.py

@@ -9,7 +9,7 @@ from UM.i18n import i18nCatalog
 from UM.Logger import Logger
 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 .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.
         :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 != "":
             request_url += f"&package_type={self._package_type_filter}"
         if self._current_search_string != "":