1"""Implementation of Submodel related API calls."""
5from typing
import TYPE_CHECKING
8from pydantic
import BaseModel
11 from aas_http_client.classes.client.aas_client
import AasHttpClient
22_logger = logging.getLogger(__name__)
26 """Implementation of Submodel related API calls."""
28 def __init__(self, client:
"AasHttpClient"):
29 """Initializes the SmImplementation with the given parameters."""
32 session = client.get_session()
35 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating SubmodelRegistryImplementation instance."
38 self._session: requests.Session = session
41 def get_submodel_by_id(self, submodel_identifier: str, level: str =
"", extent: str =
"") -> dict |
None:
42 """Returns a specific Submodel.
44 :param submodel_identifier: The Submodels unique id
45 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
46 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
47 :return: Submodel data or None if an error occurred
49 if not self.
_client.encoded_ids:
50 submodel_identifier = encode_base_64(submodel_identifier)
52 url = f
"{self._client.base_url}/submodels/{submodel_identifier}"
54 params: dict[str, str] = {}
56 params[
"level"] = level
58 params[
"extent"] = extent
63 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
64 _logger.debug(f
"Call REST API url '{response.url}'")
66 if response.status_code == STATUS_CODE_404:
67 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
68 _logger.debug(response.text)
71 if response.status_code != STATUS_CODE_200:
72 log_response(response)
75 except requests.exceptions.RequestException
as e:
76 _logger.error(f
"Error call REST API: {e}")
79 content = response.content.decode(
"utf-8")
80 return json.loads(content)
84 """Updates a existing Submodel.
86 :param submodel_identifier: The Submodels unique id
87 :param request_body: Json data of the Submodel to update
88 :return: True if the update was successful, False otherwise
90 if not self.
_client.encoded_ids:
91 submodel_identifier = encode_base_64(submodel_identifier)
93 url = f
"{self._client.base_url}/submodels/{submodel_identifier}"
98 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
99 _logger.debug(f
"Call REST API url '{response.url}'")
101 if response.status_code == STATUS_CODE_404:
102 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
103 _logger.debug(response.text)
106 if response.status_code != STATUS_CODE_204:
107 log_response(response)
110 except requests.exceptions.RequestException
as e:
111 _logger.error(f
"Error call REST API: {e}")
118 """Deletes a Submodel.
120 :param submodel_identifier: The Submodels unique id
121 :return: True if the deletion was successful, False otherwise
123 if not self.
_client.encoded_ids:
124 submodel_identifier = encode_base_64(submodel_identifier)
126 url = f
"{self._client.base_url}/submodels/{submodel_identifier}"
131 response = self._session.delete(url, timeout=self.
_client.time_out)
132 _logger.debug(f
"Call REST API url '{response.url}'")
134 if response.status_code == STATUS_CODE_404:
135 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
136 _logger.debug(response.text)
139 if response.status_code != STATUS_CODE_204:
140 log_response(response)
143 except requests.exceptions.RequestException
as e:
144 _logger.error(f
"Error call REST API: {e}")
151 self, submodel_identifier: str, id_short_path: str, level: str =
"", extent: str =
""
153 """Returns a specific submodel element from the Submodel at a specified path.
155 :param submodel_identifier: The Submodels unique id
156 :param id_short_path: IdShort path to the submodel element (dot-separated)
157 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
158 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
159 :return: Submodel element data or None if an error occurred
162 submodel_identifier = encode_base_64(submodel_identifier)
164 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
166 params: dict[str, str] = {}
168 params[
"level"] = level
170 params[
"extent"] = extent
175 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
176 _logger.debug(f
"Call REST API url '{response.url}'")
178 if response.status_code == STATUS_CODE_404:
179 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
180 _logger.debug(response.text)
183 if response.status_code != STATUS_CODE_200:
184 log_response(response)
187 except requests.exceptions.RequestException
as e:
188 _logger.error(f
"Error call REST API: {e}")
191 content = response.content.decode(
"utf-8")
192 return json.loads(content)
196 """Creates or updates an existing submodel element at a specified path within submodel elements hierarchy.
198 :param submodel_identifier: The Submodels unique id
199 :param id_short_path: IdShort path to the submodel element (dot-separated)
200 :param request_body: Data for the submodel element
201 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
202 :return: True if the operation was successful, False otherwise
204 if not self.
_client.encoded_ids:
205 submodel_identifier = encode_base_64(submodel_identifier)
207 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
209 params: dict[str, str] = {}
211 params[
"level"] = level
216 response = self._session.put(url, json=request_body, params=params, timeout=self.
_client.time_out)
217 _logger.debug(f
"Call REST API url '{response.url}'")
219 if response.status_code == STATUS_CODE_404:
220 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
221 _logger.debug(response.text)
224 if response.status_code != STATUS_CODE_204:
225 log_response(response)
228 except requests.exceptions.RequestException
as e:
229 _logger.error(f
"Error call REST API: {e}")
236 self, submodel_identifier: str, id_short_path: str, request_body: dict, level: str =
"", extent: str =
""
238 """Creates a new submodel element at a specified path within submodel elements hierarchy.
240 :param submodel_identifier: The Submodels unique id
241 :param id_short_path: IdShort path to the submodel element (dot-separated)
242 :param request_body: Data for the new Submodel element
243 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
244 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
245 :return: Submodel element data or None if an error occurred
248 submodel_identifier = encode_base_64(submodel_identifier)
250 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
252 params: dict[str, str] = {}
254 params[
"level"] = level
256 params[
"extent"] = extent
261 response = self._session.post(url, json=request_body, params=params, timeout=self.
_client.time_out)
262 _logger.debug(f
"Call REST API url '{response.url}'")
264 if response.status_code == STATUS_CODE_404:
265 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
266 _logger.debug(response.text)
269 if response.status_code != STATUS_CODE_201:
270 log_response(response)
273 except requests.exceptions.RequestException
as e:
274 _logger.error(f
"Error call REST API: {e}")
277 content = response.content.decode(
"utf-8")
278 return json.loads(content)
282 """Deletes a submodel element at a specified path within the submodel elements hierarchy.
284 :param submodel_identifier: The Submodels unique id
285 :param id_short_path: IdShort path to the submodel element (dot-separated)
286 :return: True if the deletion was successful, False otherwise
288 if not self.
_client.encoded_ids:
289 submodel_identifier = encode_base_64(submodel_identifier)
291 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
295 response = self._session.delete(url, timeout=self.
_client.time_out)
296 _logger.debug(f
"Call REST API url '{response.url}'")
298 if response.status_code == STATUS_CODE_404:
299 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
300 _logger.debug(response.text)
303 if response.status_code != STATUS_CODE_204:
304 log_response(response)
307 except requests.exceptions.RequestException
as e:
308 _logger.error(f
"Error call REST API: {e}")
315 self, semantic_id: str =
"", id_short: str =
"", limit: int = 0, cursor: str =
"", level: str =
"", extent: str =
""
317 """Returns all Submodels.
319 :param semantic_id: The value of the semantic id reference (UTF8-BASE64-URL-encoded)
320 :param id_short: The Submodels IdShort
321 :param limit: The maximum number of elements in the response array
322 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
323 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
324 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
325 :return: List of Submodel data or None if an error occurred
327 url = f
"{self._client.base_url}/submodels"
329 params: dict[str, str] = {}
331 params[
"semanticId"] = semantic_id
333 params[
"idShort"] = id_short
335 params[
"limit"] = str(limit)
337 params[
"cursor"] = cursor
339 params[
"level"] = level
341 params[
"extent"] = extent
346 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
347 _logger.debug(f
"Call REST API url '{response.url}'")
349 if response.status_code != STATUS_CODE_200:
350 log_response(response)
353 except requests.exceptions.RequestException
as e:
354 _logger.error(f
"Error call REST API: {e}")
357 content = response.content.decode(
"utf-8")
358 return json.loads(content)
362 """Creates a new Submodel.
364 :param request_body: Json data of the Submodel to post
365 :return: Submodel data or None if an error occurred
367 url = f
"{self._client.base_url}/submodels"
372 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
373 _logger.debug(f
"Call REST API url '{response.url}'")
375 if response.status_code != STATUS_CODE_201:
376 log_response(response)
379 except requests.exceptions.RequestException
as e:
380 _logger.error(f
"Error call REST API: {e}")
383 content = response.content.decode(
"utf-8")
384 return json.loads(content)
388 self, submodel_identifier: str, limit: int = 100, cursor: str =
"", level: str =
"", extent: str =
""
390 """Returns all submodel elements including their hierarchy.
392 :param submodel_identifier: The Submodels unique id
393 :param limit: The maximum number of elements in the response array
394 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
395 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
396 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
397 :return: List of Submodel element data or None if an error occurred
400 submodel_identifier = encode_base_64(submodel_identifier)
402 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements"
404 params: dict[str, str] = {}
406 params[
"limit"] = str(limit)
408 params[
"cursor"] = cursor
410 params[
"level"] = level
412 params[
"extent"] = extent
417 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
418 _logger.debug(f
"Call REST API url '{response.url}'")
420 if response.status_code != STATUS_CODE_200:
421 log_response(response)
424 except requests.exceptions.RequestException
as e:
425 _logger.error(f
"Error call REST API: {e}")
428 content = response.content.decode(
"utf-8")
429 return json.loads(content)
433 """Creates a new submodel element.
435 :param submodel_identifier: The Submodels unique id
436 :param request_body: Data for the new Submodel element
437 :return: Submodel element data or None if an error occurred
439 if not self.
_client.encoded_ids:
440 submodel_identifier = encode_base_64(submodel_identifier)
442 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements"
447 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
448 _logger.debug(f
"Call REST API url '{response.url}'")
450 if response.status_code != STATUS_CODE_201:
451 log_response(response)
454 except requests.exceptions.RequestException
as e:
455 _logger.error(f
"Error call REST API: {e}")
458 content = response.content.decode(
"utf-8")
459 return json.loads(content)
463 """Synchronously invokes an Operation at a specified path.
465 :param submodel_identifier: The Submodels unique id
466 :param id_short_path: IdShort path to the operation element (dot-separated)
467 :param request_body: Input parameters for the operation
468 :param async_: Determines whether an operation invocation is performed asynchronously or synchronously
469 :return: Operation result or None if an error occurred
471 if not self.
_client.encoded_ids:
472 submodel_identifier = encode_base_64(submodel_identifier)
474 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/invoke"
479 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
480 _logger.debug(f
"Call REST API url '{response.url}'")
482 if response.status_code != STATUS_CODE_200:
483 log_response(response)
486 except requests.exceptions.RequestException
as e:
487 _logger.error(f
"Error call REST API: {e}")
490 content = response.content.decode(
"utf-8")
491 return json.loads(content)
495 """Retrieves the value of a specific SubmodelElement.
497 :param submodel_identifier: The Submodels unique id
498 :param id_short_path: IdShort path to the submodel element (dot-separated)
499 :return: Submodel element value or None if an error occurred
501 if not self.
_client.encoded_ids:
502 submodel_identifier = encode_base_64(submodel_identifier)
504 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/$value"
509 response = self._session.get(url, timeout=self.
_client.time_out)
510 _logger.debug(f
"Call REST API url '{response.url}'")
512 if response.status_code != STATUS_CODE_200:
513 log_response(response)
516 except requests.exceptions.RequestException
as e:
517 _logger.error(f
"Error call REST API: {e}")
520 content = response.content.decode(
"utf-8")
521 return json.loads(content)
525 self, submodel_identifier: str, id_short_path: str, value: str, level: str =
""
527 """Updates the value of an existing SubmodelElement.
529 :param submodel_identifier: The Submodels unique id
530 :param id_short_path: IdShort path to the submodel element (dot-separated)
531 :param value: Submodel element value to update as string
532 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
533 :return: True if the patch was successful, False otherwise
536 submodel_identifier = encode_base_64(submodel_identifier)
538 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/$value"
540 params: dict[str, str] = {}
542 params[
"level"] = level
547 response = self._session.patch(url, json=value, params=params, timeout=self.
_client.time_out)
548 _logger.debug(f
"Call REST API url '{response.url}'")
550 if response.status_code == STATUS_CODE_404:
551 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
552 _logger.debug(response.text)
555 if response.status_code != STATUS_CODE_204:
556 log_response(response)
559 except requests.exceptions.RequestException
as e:
560 _logger.error(f
"Error call REST API: {e}")
567 """Returns a specific Submodel in the ValueOnly representation.
569 :param submodel_identifier: The Submodels unique id
570 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
571 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
572 :return: Submodel value as dict or None if an error occurred
574 params: dict[str, str] = {}
576 params[
"level"] = level
578 params[
"extent"] = extent
580 if not self.
_client.encoded_ids:
581 submodel_identifier = encode_base_64(submodel_identifier)
583 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/$value"
588 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
589 _logger.debug(f
"Call REST API url '{response.url}'")
591 if response.status_code == STATUS_CODE_404:
592 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
593 _logger.debug(response.text)
596 if response.status_code != STATUS_CODE_200:
597 log_response(response)
600 except requests.exceptions.RequestException
as e:
601 _logger.error(f
"Error call REST API: {e}")
604 content = response.content.decode(
"utf-8")
605 return json.loads(content)
609 """Updates the values of an existing Submodel.
611 :param submodel_identifier: The Submodels unique id
612 :param request_body: Submodel values to update as dict
613 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
614 :return: True if the patch was successful, False otherwise
616 params: dict[str, str] = {}
618 params[
"level"] = level
620 if not self.
_client.encoded_ids:
621 submodel_identifier = encode_base_64(submodel_identifier)
623 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/$value"
628 response = self._session.patch(url, json=request_body, params=params, timeout=self.
_client.time_out)
629 _logger.debug(f
"Call REST API url '{response.url}'")
631 if response.status_code == STATUS_CODE_404:
632 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
633 _logger.debug(response.text)
636 if response.status_code != STATUS_CODE_204:
637 log_response(response)
640 except requests.exceptions.RequestException
as e:
641 _logger.error(f
"Error call REST API: {e}")
648 """Returns the metadata attributes of a specific Submodel.
650 :param submodel_identifier: The Submodels unique id
651 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
652 :return: Metadata attributes of the Submodel as dict or None if an error occurred
654 params: dict[str, str] = {}
656 params[
"level"] = level
658 if not self.
_client.encoded_ids:
659 submodel_identifier = encode_base_64(submodel_identifier)
661 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/$metadata"
666 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
667 _logger.debug(f
"Call REST API url '{response.url}'")
669 if response.status_code == STATUS_CODE_404:
670 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
671 _logger.debug(response.text)
674 if response.status_code != STATUS_CODE_200:
675 log_response(response)
678 except requests.exceptions.RequestException
as e:
679 _logger.error(f
"Error call REST API: {e}")
682 content = response.content.decode(
"utf-8")
683 return json.loads(content)
689 """Updates an existing Submodel.
691 :param submodel_identifier: The Submodels unique id
692 :return: True if the patch was successful, False otherwise
694 if not self.
_client.encoded_ids:
695 submodel_identifier = encode_base_64(submodel_identifier)
697 url = f
"{self._client.base_url}/submodels/{submodel_identifier}"
702 response = self._session.patch(url, json=submodel_data, timeout=self.
_client.time_out)
703 _logger.debug(f
"Call REST API url '{response.url}'")
705 if response.status_code == STATUS_CODE_404:
706 _logger.warning(f
"Submodel with id '{submodel_identifier}' not found.")
707 _logger.debug(response.text)
710 if response.status_code != STATUS_CODE_204:
711 log_response(response)
714 except requests.exceptions.RequestException
as e:
715 _logger.error(f
"Error call REST API: {e}")
Implementation of Submodel related API calls.
dict|None get_submodel_by_id(self, str submodel_identifier, str level="", str extent="")
Returns a specific Submodel.
dict|None get_submodel_element_by_path_submodel_repo(self, str submodel_identifier, str id_short_path, str level="", str extent="")
Returns a specific submodel element from the Submodel at a specified path.
__init__(self, "AasHttpClient" client)
Initializes the SmImplementation with the given parameters.
str|None get_submodel_element_by_path_value_only_submodel_repo(self, str submodel_identifier, str id_short_path)
Retrieves the value of a specific SubmodelElement.
dict|None get_all_submodels(self, str semantic_id="", str id_short="", int limit=0, str cursor="", str level="", str extent="")
Returns all Submodels.
dict|None get_submodel_by_id_metadata(self, str submodel_identifier, str level="")
Returns the metadata attributes of a specific Submodel.
dict|None post_submodel(self, dict request_body)
Creates a new Submodel.
dict|None post_submodel_element_by_path_submodel_repo(self, str submodel_identifier, str id_short_path, dict request_body, str level="", str extent="")
Creates a new submodel element at a specified path within submodel elements hierarchy.
bool patch_submodel_element_by_path_value_only_submodel_repo(self, str submodel_identifier, str id_short_path, str value, str level="")
Updates the value of an existing SubmodelElement.
dict|None post_submodel_element_submodel_repo(self, str submodel_identifier, dict request_body)
Creates a new submodel element.
dict|None get_submodel_by_id_value_only(self, str submodel_identifier, str level="", str extent="")
Returns a specific Submodel in the ValueOnly representation.
bool put_submodel_element_by_path_submodel_repo(self, str submodel_identifier, str id_short_path, dict request_body, str level="")
Creates or updates an existing submodel element at a specified path within submodel elements hierarch...
bool patch_submodel_by_id(self, str submodel_identifier, dict submodel_data)
Updates an existing Submodel.
bool delete_submodel_by_id(self, str submodel_identifier)
Deletes a Submodel.
dict|None invoke_operation_submodel_repo(self, str submodel_identifier, str id_short_path, dict request_body, str async_="async")
Synchronously invokes an Operation at a specified path.
bool delete_submodel_element_by_path_submodel_repo(self, str submodel_identifier, str id_short_path)
Deletes a submodel element at a specified path within the submodel elements hierarchy.
bool put_submodels_by_id(self, str submodel_identifier, dict request_body)
Updates a existing Submodel.
bool patch_submodel_by_id_value_only(self, str submodel_identifier, dict request_body, str level="")
Updates the values of an existing Submodel.
dict|None get_all_submodel_elements_submodel_repository(self, str submodel_identifier, int limit=100, str cursor="", str level="", str extent="")
Returns all submodel elements including their hierarchy.