From 93c80321418cde2d1455a4985910e0ccc05083e1 Mon Sep 17 00:00:00 2001 From: pierrerondel Date: Tue, 9 Sep 2025 16:04:14 +0200 Subject: [PATCH 1/3] Update order.py new function: def command_call_check_duplicate(self, command: str, mode: int, params, timeout=300) -> None: """ Command call with duplicate check for CREATE. Parameters ----------- command: str CRUD method in microservice to call ("CREATE", "UPDATE", etc.) mode: int 0 - No application 1 - Apply to base 2 - Apply to device params: dict Parameters for the command. When using CREATE, must contain: - object_name (str): microservice object name - object_id (str): instance ID timeout: int Timeout for the request in seconds (default=300) --- msa_sdk/order.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/msa_sdk/order.py b/msa_sdk/order.py index dd16555d..d3686a35 100644 --- a/msa_sdk/order.py +++ b/msa_sdk/order.py @@ -289,3 +289,75 @@ def command_objects_details_by_name(self, object_name): self._call_get() return json.loads(self.content) + + def command_call_check_duplicate(self, command: str, mode: int, params, timeout=300) -> None: + """ + Command call with duplicate check for CREATE. + + Parameters + ----------- + command: str + CRUD method in microservice to call ("CREATE", "UPDATE", etc.) + mode: int + 0 - No application + 1 - Apply to base + 2 - Apply to device + params: dict + Parameters for the command. + When using CREATE, must contain: + - object_name (str): microservice object name + - object_id (str): instance ID + timeout: int + Timeout for the request in seconds (default=300) + + Raises + ------ + ValueError + If the object already exists when trying to CREATE + RuntimeError + If an unexpected error occurs while checking existence + """ + + self.action = 'Call command' + + if command.upper() == "CREATE": + object_name = list(params.keys())[0] + object_id = list(params.get(object_name).keys())[0] + + if not object_name or not object_id: + raise ValueError(f"CREATE requires '{object_name}' and '{object_id}' in params") + + try: + # Directly check if object exists + self.action = 'Get Microservice Object Details' + self.path = '{}/objects/{}/{}/{}'.format( + self.api_path, + self.device_id, + object_name, + object_id + ) + self._call_get() + + # If we got JSON content and it's non-empty → object exists + existing = json.loads(self.content) + if isinstance(existing, dict) and existing: + raise ValueError( + f"Microservice object '{object_name}' with ID '{object_id}' already exists" + ) + + except json.JSONDecodeError: + # Response was not valid JSON (likely "not found") → treat as non-existent + pass + except Exception as e: + raise RuntimeError(f"Error checking object existence: {e}") + + # Build path for the actual command + self.path = '{}/call/{}/{}/{}'.format( + self.api_path, + self.device_id, + command, + mode + ) + + # Post command + self._call_post(params, timeout) From 7bd17c0f05a7022a6018db35046dc8aea3508cf6 Mon Sep 17 00:00:00 2001 From: pierrerondel Date: Tue, 9 Sep 2025 16:15:33 +0200 Subject: [PATCH 2/3] Update order.py replace word object by instance in the error message --- msa_sdk/order.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msa_sdk/order.py b/msa_sdk/order.py index d3686a35..35a21c87 100644 --- a/msa_sdk/order.py +++ b/msa_sdk/order.py @@ -342,7 +342,7 @@ def command_call_check_duplicate(self, command: str, mode: int, params, timeout= existing = json.loads(self.content) if isinstance(existing, dict) and existing: raise ValueError( - f"Microservice object '{object_name}' with ID '{object_id}' already exists" + f"Microservice instance '{object_name}' with ID '{object_id}' already exists" ) except json.JSONDecodeError: From d2d0bac8eccef4551261169800e3125b24a1466e Mon Sep 17 00:00:00 2001 From: pierrerondel Date: Wed, 10 Sep 2025 08:59:09 +0200 Subject: [PATCH 3/3] Update order.py remove a blank line that made pydocstring to fail during the tests. --- msa_sdk/order.py | 1 - 1 file changed, 1 deletion(-) diff --git a/msa_sdk/order.py b/msa_sdk/order.py index 35a21c87..5bc0c073 100644 --- a/msa_sdk/order.py +++ b/msa_sdk/order.py @@ -317,7 +317,6 @@ def command_call_check_duplicate(self, command: str, mode: int, params, timeout= RuntimeError If an unexpected error occurs while checking existence """ - self.action = 'Call command' if command.upper() == "CREATE":