Browse Source

Automatic abortRequest for each API request

Defensive programming

Contributes to: CURA-8587
Jelle Spijker 3 years ago
parent
commit
7be2da587b

+ 16 - 7
plugins/Marketplace/PackageList.py

@@ -5,7 +5,7 @@ import json
 import os.path
 
 from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, Qt
-from typing import cast, Dict, List, Optional, Set, TYPE_CHECKING
+from typing import cast, Dict, Optional, Set, TYPE_CHECKING
 
 from UM.i18n import i18nCatalog
 from UM.Qt.ListModel import ListModel
@@ -53,15 +53,24 @@ class PackageList(ListModel):
         self._scope = JsonDecoratorScope(UltimakerCloudScope(CuraApplication.getInstance()))
         self._license_dialogs: Dict[str, QObject] = {}
 
+    def __del__(self) -> None:
+        """ When this object is deleted it will loop through all registered API requests and aborts them """
+        self.cleanUpAPIRequest()
+
+    def abortRequest(self, request_id: str) -> None:
+        """Aborts a single request"""
+        if request_id in self._ongoing_requests and self._ongoing_requests[request_id]:
+            HttpRequestManager.getInstance().abortRequest(self._ongoing_requests[request_id])
+            self._ongoing_requests[request_id] = None
+
     @pyqtSlot()
-    def updatePackages(self) -> None:
-        """ A Qt slot which will update the List from a source. Actual implementation should be done in the child class"""
-        pass
+    def cleanUpAPIRequest(self) -> None:
+        for request_id in self._ongoing_requests:
+            self.abortRequest(request_id)
 
     @pyqtSlot()
-    def abortUpdating(self) -> None:
-        """ A Qt slot which allows the update process to be aborted. Override this for child classes with async/callback
-        updatePackges methods"""
+    def updatePackages(self) -> None:
+        """ A Qt slot which will update the List from a source. Actual implementation should be done in the child class"""
         pass
 
     def reset(self) -> None:

+ 0 - 12
plugins/Marketplace/RemotePackageList.py

@@ -33,13 +33,6 @@ class RemotePackageList(PackageList):
         self.isLoadingChanged.connect(self._onLoadingChanged)
         self.isLoadingChanged.emit()
 
-    def __del__(self) -> None:
-        """
-        When deleting this object, abort the request so that we don't get a callback from it later on a deleted C++
-        object.
-        """
-        self.abortUpdating()
-
     @pyqtSlot()
     def updatePackages(self) -> None:
         """
@@ -57,11 +50,6 @@ class RemotePackageList(PackageList):
             error_callback = self._onError
         )
 
-    @pyqtSlot()
-    def abortUpdating(self) -> None:
-        HttpRequestManager.getInstance().abortRequest(self._ongoing_requests["get_packages"])
-        self._ongoing_requests["get_packages"] = None
-
     def reset(self) -> None:
         self.clear()
         self._request_url = self._initialRequestUrl()

+ 1 - 1
plugins/Marketplace/resources/qml/Packages.qml

@@ -24,7 +24,7 @@ ListView
     clip: true
 
     Component.onCompleted: model.updatePackages()
-    Component.onDestruction: model.abortUpdating()
+    Component.onDestruction: model.cleanUpAPIRequest()
 
     spacing: UM.Theme.getSize("default_margin").height