mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-06-01 23:17:26 +08:00
Merge pull request #18741 from Ultimaker/CURA-11786_cloud_plugin_cleanup
[CURA-11786] [PP-452] Clean up the CloudAPIClient
This commit is contained in:
commit
f89d4ddb4c
@ -5,6 +5,7 @@ import urllib.parse
|
||||
from json import JSONDecodeError
|
||||
from time import time
|
||||
from typing import Callable, List, Type, TypeVar, Union, Optional, Tuple, Dict, Any, cast
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtCore import QUrl
|
||||
from PyQt6.QtNetwork import QNetworkRequest, QNetworkReply
|
||||
@ -38,14 +39,17 @@ class CloudApiClient:
|
||||
|
||||
# The cloud URL to use for this remote cluster.
|
||||
ROOT_PATH = UltimakerCloudConstants.CuraCloudAPIRoot
|
||||
CLUSTER_API_ROOT = "{}/connect/v1".format(ROOT_PATH)
|
||||
CURA_API_ROOT = "{}/cura/v1".format(ROOT_PATH)
|
||||
CLUSTER_API_ROOT = f"{ROOT_PATH}/connect/v1"
|
||||
CURA_API_ROOT = f"{ROOT_PATH}/cura/v1"
|
||||
|
||||
DEFAULT_REQUEST_TIMEOUT = 10 # seconds
|
||||
|
||||
# In order to avoid garbage collection we keep the callbacks in this list.
|
||||
_anti_gc_callbacks = [] # type: List[Callable[[Any], None]]
|
||||
|
||||
# Custom machine definition ID to cloud cluster name mapping
|
||||
_machine_id_to_name: Dict[str, str] = None
|
||||
|
||||
def __init__(self, app: CuraApplication, on_error: Callable[[List[CloudError]], None]) -> None:
|
||||
"""Initializes a new cloud API client.
|
||||
|
||||
@ -84,13 +88,9 @@ class CloudApiClient:
|
||||
# conversion!
|
||||
# API points to "MakerBot Method" for a makerbot printertypes which we already changed to allign with other printer_type
|
||||
|
||||
method_x = {
|
||||
"ultimaker_method":"MakerBot Method",
|
||||
"ultimaker_methodx":"MakerBot Method X",
|
||||
"ultimaker_methodxl":"MakerBot Method XL"
|
||||
}
|
||||
if machine_type in method_x:
|
||||
machine_type = method_x[machine_type]
|
||||
machine_id_to_name = self.getMachineIDMap()
|
||||
if machine_type in machine_id_to_name:
|
||||
machine_type = machine_id_to_name[machine_type]
|
||||
else:
|
||||
machine_type = machine_type.replace("_plus", "+")
|
||||
machine_type = machine_type.replace("_", " ")
|
||||
@ -174,6 +174,7 @@ class CloudApiClient:
|
||||
:param cluster_id: The ID of the cluster.
|
||||
:param cluster_job_id: The ID of the print job within the cluster.
|
||||
:param action: The name of the action to execute.
|
||||
:param data: Optional data to send with the POST request
|
||||
"""
|
||||
|
||||
body = json.dumps({"data": data}).encode() if data else b""
|
||||
@ -216,8 +217,11 @@ class CloudApiClient:
|
||||
Logger.logException("e", "Could not parse the stardust response: %s", error.toDict())
|
||||
return status_code, {"errors": [error.toDict()]}
|
||||
|
||||
def _parseResponse(self, response: Dict[str, Any], on_finished: Union[Callable[[CloudApiClientModel], Any],
|
||||
Callable[[List[CloudApiClientModel]], Any]], model_class: Type[CloudApiClientModel]) -> None:
|
||||
def _parseResponse(self,
|
||||
response: Dict[str, Any],
|
||||
on_finished: Union[Callable[[CloudApiClientModel], Any],
|
||||
Callable[[List[CloudApiClientModel]], Any]],
|
||||
model_class: Type[CloudApiClientModel]) -> None:
|
||||
"""Parses the given response and calls the correct callback depending on the result.
|
||||
|
||||
:param response: The response from the server, after being converted to a dict.
|
||||
@ -276,3 +280,14 @@ class CloudApiClient:
|
||||
|
||||
self._anti_gc_callbacks.append(parse)
|
||||
return parse
|
||||
|
||||
@classmethod
|
||||
def getMachineIDMap(cls) -> Dict[str, str]:
|
||||
if cls._machine_id_to_name is None:
|
||||
try:
|
||||
with open(Path(__file__).parent / "machine_id_to_name.json", "rt") as f:
|
||||
cls._machine_id_to_name = json.load(f)
|
||||
except Exception as e:
|
||||
Logger.logException("e", f"Could not load machine_id_to_name.json: '{e}'")
|
||||
cls._machine_id_to_name = {}
|
||||
return cls._machine_id_to_name
|
||||
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"ultimaker_method": "MakerBot Method",
|
||||
"ultimaker_methodx": "MakerBot Method X",
|
||||
"ultimaker_methodxl": "MakerBot Method XL"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user