Automatic abortRequest for each API request

Defensive programming

Contributes to: CURA-8587
This commit is contained in:
Jelle Spijker 2021-12-08 10:18:08 +01:00
parent 453de95d12
commit 7be2da587b
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A
3 changed files with 17 additions and 20 deletions

View file

@ -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,17 +53,26 @@ 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 cleanUpAPIRequest(self) -> None:
for request_id in self._ongoing_requests:
self.abortRequest(request_id)
@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
@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"""
pass
def reset(self) -> None:
""" Resets and clears the list"""
self.clear()