AAS HTTP Client Documentation
Loading...
Searching...
No Matches
sm_implementation.py
Go to the documentation of this file.
1"""Implementation of Submodel related API calls."""
2
3import json
4import logging
5from typing import TYPE_CHECKING
6
7import requests
8from pydantic import BaseModel
9
10if TYPE_CHECKING:
11 from aas_http_client.classes.client.aas_client import AasHttpClient
12
13from aas_http_client.utilities.constants import LogIntensity
14from aas_http_client.utilities.encoder import encode_base_64
16 STATUS_CODE_200,
17 STATUS_CODE_201,
18 STATUS_CODE_204,
19 STATUS_CODE_404,
20 log_response,
21)
22
23_logger = logging.getLogger(__name__)
24
25
27 """Implementation of Submodel related API calls."""
28
29 def __init__(self, client: "AasHttpClient"):
30 """Initializes the SmImplementation with the given parameters."""
31 self._client = client
32
33 session = client.get_session()
34 if session is None:
35 raise ValueError(
36 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating SubmodelRegistryImplementation instance."
37 )
38
39 self._session: requests.Session = session
40
41 # GET /submodels/{submodelIdentifier}
42 def get_submodel_by_id(self, submodel_identifier: str, level: str = "", extent: str = "") -> dict | None:
43 """Returns a specific Submodel.
44
45 :param submodel_identifier: The Submodels unique id
46 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
47 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
48 :return: Submodel data or None if an error occurred
49 """
50 if not self._client.encoded_ids:
51 submodel_identifier = encode_base_64(submodel_identifier)
53 url = f"{self._client.base_url}/submodels/{submodel_identifier}"
54
55 params: dict[str, str] = {}
56 if level:
57 params["level"] = level
58 if extent:
59 params["extent"] = extent
60
61 self._client.set_token()
62
63 try:
64 response = self._session.get(url, params=params, timeout=self._client.time_out)
65 _logger.debug(f"Call REST API url '{response.url}'")
66
67 if response.status_code == STATUS_CODE_404:
68 if self._client.get_log_intensity() == LogIntensity.HIGH:
69 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
70 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
71 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
72
73 return None
74
75 if response.status_code != STATUS_CODE_200:
76 log_response(response)
77 return None
78
79 except requests.exceptions.RequestException as e:
80 _logger.error(f"Error call REST API: {e}")
81 return None
82
83 content = response.content.decode("utf-8")
84 return json.loads(content)
85
86 # PUT /submodels/{submodelIdentifier}
87 def put_submodels_by_id(self, submodel_identifier: str, request_body: dict) -> bool:
88 """Updates a existing Submodel.
89
90 :param submodel_identifier: The Submodels unique id
91 :param request_body: Json data of the Submodel to update
92 :return: True if the update was successful, False otherwise
93 """
94 if not self._client.encoded_ids:
95 submodel_identifier = encode_base_64(submodel_identifier)
97 url = f"{self._client.base_url}/submodels/{submodel_identifier}"
98
99 self._client.set_token()
100
101 try:
102 response = self._session.put(url, json=request_body, timeout=self._client.time_out)
103 _logger.debug(f"Call REST API url '{response.url}'")
104
105 if response.status_code == STATUS_CODE_404:
106 if self._client.get_log_intensity() == LogIntensity.HIGH:
107 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
108 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
109 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
110
111 return False
112
113 if response.status_code != STATUS_CODE_204:
114 log_response(response)
115 return False
116
117 except requests.exceptions.RequestException as e:
118 _logger.error(f"Error call REST API: {e}")
119 return False
120
121 return True
122
123 # DELETE /submodels/{submodelIdentifier}
124 def delete_submodel_by_id(self, submodel_identifier: str) -> bool:
125 """Deletes a Submodel.
126
127 :param submodel_identifier: The Submodels unique id
128 :return: True if the deletion was successful, False otherwise
129 """
130 if not self._client.encoded_ids:
131 submodel_identifier = encode_base_64(submodel_identifier)
133 url = f"{self._client.base_url}/submodels/{submodel_identifier}"
134
135 self._client.set_token()
136
137 try:
138 response = self._session.delete(url, timeout=self._client.time_out)
139 _logger.debug(f"Call REST API url '{response.url}'")
140
141 if response.status_code == STATUS_CODE_404:
142 if self._client.get_log_intensity() == LogIntensity.HIGH:
143 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
144 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
145 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
146
147 return False
148
149 if response.status_code != STATUS_CODE_204:
150 log_response(response)
151 return False
152
153 except requests.exceptions.RequestException as e:
154 _logger.error(f"Error call REST API: {e}")
155 return False
156
157 return True
158
159 # GET /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}
161 self, submodel_identifier: str, id_short_path: str, level: str = "", extent: str = ""
162 ) -> dict | None:
163 """Returns a specific submodel element from the Submodel at a specified path.
164
165 :param submodel_identifier: The Submodels unique id
166 :param id_short_path: IdShort path to the submodel element (dot-separated)
167 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
168 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
169 :return: Submodel element data or None if an error occurred
170 """
171 if not self._client.encoded_ids:
172 submodel_identifier = encode_base_64(submodel_identifier)
173
174 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
175
176 params: dict[str, str] = {}
177 if level:
178 params["level"] = level
179 if extent:
180 params["extent"] = extent
181
182 self._client.set_token()
183
184 try:
185 response = self._session.get(url, params=params, timeout=self._client.time_out)
186 _logger.debug(f"Call REST API url '{response.url}'")
187
188 if response.status_code == STATUS_CODE_404:
189 if self._client.get_log_intensity() == LogIntensity.HIGH:
190 _logger.warning(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
191 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
192 _logger.debug(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
193
194 return None
195
196 if response.status_code != STATUS_CODE_200:
197 log_response(response)
198 return None
199
200 except requests.exceptions.RequestException as e:
201 _logger.error(f"Error call REST API: {e}")
202 return None
203
204 content = response.content.decode("utf-8")
205 return json.loads(content)
206
207 # PUT /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}
208 def put_submodel_element_by_path_submodel_repo(self, submodel_identifier: str, id_short_path: str, request_body: dict, level: str = "") -> bool:
209 """Creates or updates an existing submodel element at a specified path within submodel elements hierarchy.
210
211 :param submodel_identifier: The Submodels unique id
212 :param id_short_path: IdShort path to the submodel element (dot-separated)
213 :param request_body: Data for the submodel element
214 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
215 :return: True if the operation was successful, False otherwise
216 """
217 if not self._client.encoded_ids:
218 submodel_identifier = encode_base_64(submodel_identifier)
220 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
221
222 params: dict[str, str] = {}
223 if level:
224 params["level"] = level
225
226 self._client.set_token()
227
228 try:
229 response = self._session.put(url, json=request_body, params=params, timeout=self._client.time_out)
230 _logger.debug(f"Call REST API url '{response.url}'")
231
232 if response.status_code == STATUS_CODE_404:
233 if self._client.get_log_intensity() == LogIntensity.HIGH:
234 _logger.warning(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
235 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
236 _logger.debug(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
237
238 return False
239
240 if response.status_code != STATUS_CODE_204:
241 log_response(response)
242 return False
243
244 except requests.exceptions.RequestException as e:
245 _logger.error(f"Error call REST API: {e}")
246 return False
247
248 return True
249
250 # POST /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}
252 self, submodel_identifier: str, id_short_path: str, request_body: dict, level: str = "", extent: str = ""
253 ) -> dict | None:
254 """Creates a new submodel element at a specified path within submodel elements hierarchy.
255
256 :param submodel_identifier: The Submodels unique id
257 :param id_short_path: IdShort path to the submodel element (dot-separated)
258 :param request_body: Data for the new Submodel element
259 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
260 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
261 :return: Submodel element data or None if an error occurred
262 """
263 if not self._client.encoded_ids:
264 submodel_identifier = encode_base_64(submodel_identifier)
265
266 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
267
268 params: dict[str, str] = {}
269 if level:
270 params["level"] = level
271 if extent:
272 params["extent"] = extent
273
274 self._client.set_token()
275
276 try:
277 response = self._session.post(url, json=request_body, params=params, timeout=self._client.time_out)
278 _logger.debug(f"Call REST API url '{response.url}'")
279
280 if response.status_code == STATUS_CODE_404:
281 if self._client.get_log_intensity() == LogIntensity.HIGH:
282 _logger.warning(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
283 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
284 _logger.debug(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
285
286 return None
287
288 if response.status_code != STATUS_CODE_201:
289 log_response(response)
290 return None
291
292 except requests.exceptions.RequestException as e:
293 _logger.error(f"Error call REST API: {e}")
294 return None
295
296 content = response.content.decode("utf-8")
297 return json.loads(content)
298
299 # DELETE /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}
300 def delete_submodel_element_by_path_submodel_repo(self, submodel_identifier: str, id_short_path: str) -> bool:
301 """Deletes a submodel element at a specified path within the submodel elements hierarchy.
302
303 :param submodel_identifier: The Submodels unique id
304 :param id_short_path: IdShort path to the submodel element (dot-separated)
305 :return: True if the deletion was successful, False otherwise
306 """
307 if not self._client.encoded_ids:
308 submodel_identifier = encode_base_64(submodel_identifier)
310 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}"
311
312 self._client.set_token()
313 try:
314 response = self._session.delete(url, timeout=self._client.time_out)
315 _logger.debug(f"Call REST API url '{response.url}'")
316
317 if response.status_code == STATUS_CODE_404:
318 if self._client.get_log_intensity() == LogIntensity.HIGH:
319 _logger.warning(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
320 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
321 _logger.debug(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
322
323 return False
324
325 if response.status_code != STATUS_CODE_204:
326 log_response(response)
327 return False
328
329 except requests.exceptions.RequestException as e:
330 _logger.error(f"Error call REST API: {e}")
331 return False
332
333 return True
334
335 # GET /submodels
337 self, semantic_id: str = "", id_short: str = "", limit: int = 0, cursor: str = "", level: str = "", extent: str = ""
338 ) -> dict | None:
339 """Returns all Submodels.
340
341 :param semantic_id: The value of the semantic id reference (UTF8-BASE64-URL-encoded)
342 :param id_short: The Submodels IdShort
343 :param limit: The maximum number of elements in the response array
344 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
345 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
346 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
347 :return: List of Submodel data or None if an error occurred
348 """
349 url = f"{self._client.base_url}/submodels"
350
351 params: dict[str, str] = {}
352 if semantic_id:
353 params["semanticId"] = semantic_id
354 if id_short:
355 params["idShort"] = id_short
356 if limit:
357 params["limit"] = str(limit)
358 if cursor:
359 params["cursor"] = cursor
360 if level:
361 params["level"] = level
362 if extent:
363 params["extent"] = extent
364
365 self._client.set_token()
366
367 try:
368 response = self._session.get(url, params=params, timeout=self._client.time_out)
369 _logger.debug(f"Call REST API url '{response.url}'")
370
371 if response.status_code != STATUS_CODE_200:
372 log_response(response)
373 return None
374
375 except requests.exceptions.RequestException as e:
376 _logger.error(f"Error call REST API: {e}")
377 return None
378
379 content = response.content.decode("utf-8")
380 return json.loads(content)
381
382 # POST /submodels
383 def post_submodel(self, request_body: dict) -> dict | None:
384 """Creates a new Submodel.
385
386 :param request_body: Json data of the Submodel to post
387 :return: Submodel data or None if an error occurred
388 """
389 url = f"{self._client.base_url}/submodels"
390
391 self._client.set_token()
392
393 try:
394 response = self._session.post(url, json=request_body, timeout=self._client.time_out)
395 _logger.debug(f"Call REST API url '{response.url}'")
396
397 if response.status_code != STATUS_CODE_201:
398 log_response(response)
399 return None
400
401 except requests.exceptions.RequestException as e:
402 _logger.error(f"Error call REST API: {e}")
403 return None
404
405 content = response.content.decode("utf-8")
406 return json.loads(content)
407
408 # GET /submodels/{submodelIdentifier}/submodel-elements
410 self, submodel_identifier: str, limit: int = 100, cursor: str = "", level: str = "", extent: str = ""
411 ) -> dict | None:
412 """Returns all submodel elements including their hierarchy.
413
414 :param submodel_identifier: The Submodels unique id
415 :param limit: The maximum number of elements in the response array
416 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
417 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
418 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
419 :return: List of Submodel element data or None if an error occurred
420 """
421 if not self._client.encoded_ids:
422 submodel_identifier = encode_base_64(submodel_identifier)
423
424 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements"
425
426 params: dict[str, str] = {}
427 if limit:
428 params["limit"] = str(limit)
429 if cursor:
430 params["cursor"] = cursor
431 if level:
432 params["level"] = level
433 if extent:
434 params["extent"] = extent
435
436 self._client.set_token()
437
438 try:
439 response = self._session.get(url, params=params, timeout=self._client.time_out)
440 _logger.debug(f"Call REST API url '{response.url}'")
441
442 if response.status_code != STATUS_CODE_200:
443 log_response(response)
444 return None
445
446 except requests.exceptions.RequestException as e:
447 _logger.error(f"Error call REST API: {e}")
448 return None
449
450 content = response.content.decode("utf-8")
451 return json.loads(content)
452
453 # POST /submodels/{submodelIdentifier}/submodel-elements
454 def post_submodel_element_submodel_repo(self, submodel_identifier: str, request_body: dict) -> dict | None:
455 """Creates a new submodel element.
456
457 :param submodel_identifier: The Submodels unique id
458 :param request_body: Data for the new Submodel element
459 :return: Submodel element data or None if an error occurred
460 """
461 if not self._client.encoded_ids:
462 submodel_identifier = encode_base_64(submodel_identifier)
464 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements"
465
466 self._client.set_token()
467
468 try:
469 response = self._session.post(url, json=request_body, timeout=self._client.time_out)
470 _logger.debug(f"Call REST API url '{response.url}'")
471
472 if response.status_code != STATUS_CODE_201:
473 log_response(response)
474 return None
475
476 except requests.exceptions.RequestException as e:
477 _logger.error(f"Error call REST API: {e}")
478 return None
479
480 content = response.content.decode("utf-8")
481 return json.loads(content)
482
483 # POST /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/invoke
484 def invoke_operation_submodel_repo(self, submodel_identifier: str, id_short_path: str, request_body: dict, async_: str = "async") -> dict | None:
485 """Synchronously invokes an Operation at a specified path.
486
487 :param submodel_identifier: The Submodels unique id
488 :param id_short_path: IdShort path to the operation element (dot-separated)
489 :param request_body: Input parameters for the operation
490 :param async_: Determines whether an operation invocation is performed asynchronously or synchronously
491 :return: Operation result or None if an error occurred
492 """
493 if not self._client.encoded_ids:
494 submodel_identifier = encode_base_64(submodel_identifier)
496 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/invoke"
497
498 self._client.set_token()
499
500 try:
501 response = self._session.post(url, json=request_body, timeout=self._client.time_out)
502 _logger.debug(f"Call REST API url '{response.url}'")
503
504 if response.status_code != STATUS_CODE_200:
505 log_response(response)
506 return None
507
508 except requests.exceptions.RequestException as e:
509 _logger.error(f"Error call REST API: {e}")
510 return None
511
512 content = response.content.decode("utf-8")
513 return json.loads(content)
514
515 # GET /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/$value
516 def get_submodel_element_by_path_value_only_submodel_repo(self, submodel_identifier: str, id_short_path: str) -> str | None:
517 """Retrieves the value of a specific SubmodelElement.
518
519 :param submodel_identifier: The Submodels unique id
520 :param id_short_path: IdShort path to the submodel element (dot-separated)
521 :return: Submodel element value or None if an error occurred
522 """
523 if not self._client.encoded_ids:
524 submodel_identifier = encode_base_64(submodel_identifier)
526 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/$value"
527
528 self._client.set_token()
529
530 try:
531 response = self._session.get(url, timeout=self._client.time_out)
532 _logger.debug(f"Call REST API url '{response.url}'")
533
534 if response.status_code != STATUS_CODE_200:
535 log_response(response)
536 return None
537
538 except requests.exceptions.RequestException as e:
539 _logger.error(f"Error call REST API: {e}")
540 return None
541
542 content = response.content.decode("utf-8")
543 return json.loads(content)
544
545 # PATCH /submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/$value
547 self, submodel_identifier: str, id_short_path: str, value: str, level: str = ""
548 ) -> bool:
549 """Updates the value of an existing SubmodelElement.
550
551 :param submodel_identifier: The Submodels unique id
552 :param id_short_path: IdShort path to the submodel element (dot-separated)
553 :param value: Submodel element value to update as string
554 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
555 :return: True if the patch was successful, False otherwise
556 """
557 if not self._client.encoded_ids:
558 submodel_identifier = encode_base_64(submodel_identifier)
559
560 url = f"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/$value"
561
562 params: dict[str, str] = {}
563 if level:
564 params["level"] = level
565
566 self._client.set_token()
567
568 try:
569 response = self._session.patch(url, json=value, params=params, timeout=self._client.time_out)
570 _logger.debug(f"Call REST API url '{response.url}'")
571
572 if response.status_code == STATUS_CODE_404:
573 if self._client.get_log_intensity() == LogIntensity.HIGH:
574 _logger.warning(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
575 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
576 _logger.debug(f"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
577
578 return False
579
580 if response.status_code != STATUS_CODE_204:
581 log_response(response)
582 return False
583
584 except requests.exceptions.RequestException as e:
585 _logger.error(f"Error call REST API: {e}")
586 return False
587
588 return True
589
590 # GET /submodels/{submodelIdentifier}/$value
591 def get_submodel_by_id_value_only(self, submodel_identifier: str, level: str = "", extent: str = "") -> dict | None:
592 """Returns a specific Submodel in the ValueOnly representation.
593
594 :param submodel_identifier: The Submodels unique id
595 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
596 :param extent: Determines to which extent the resource is being serialized. Available values : withBlobValue, withoutBlobValue
597 :return: Submodel value as dict or None if an error occurred
598 """
599 params: dict[str, str] = {}
600 if level:
601 params["level"] = level
602 if extent:
603 params["extent"] = extent
604
605 if not self._client.encoded_ids:
606 submodel_identifier = encode_base_64(submodel_identifier)
607
608 url = f"{self._client.base_url}/submodels/{submodel_identifier}/$value"
609
610 self._client.set_token()
611
612 try:
613 response = self._session.get(url, params=params, timeout=self._client.time_out)
614 _logger.debug(f"Call REST API url '{response.url}'")
615
616 if response.status_code == STATUS_CODE_404:
617 if self._client.get_log_intensity() == LogIntensity.HIGH:
618 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
619 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
620 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
621
622 return None
623
624 if response.status_code != STATUS_CODE_200:
625 log_response(response)
626 return None
627
628 except requests.exceptions.RequestException as e:
629 _logger.error(f"Error call REST API: {e}")
630 return None
631
632 content = response.content.decode("utf-8")
633 return json.loads(content)
634
635 # PATCH /submodels/{submodelIdentifier}/$value
636 def patch_submodel_by_id_value_only(self, submodel_identifier: str, request_body: dict, level: str = "") -> bool:
637 """Updates the values of an existing Submodel.
638
639 :param submodel_identifier: The Submodels unique id
640 :param request_body: Submodel values to update as dict
641 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
642 :return: True if the patch was successful, False otherwise
643 """
644 params: dict[str, str] = {}
645 if level:
646 params["level"] = level
647
648 if not self._client.encoded_ids:
649 submodel_identifier = encode_base_64(submodel_identifier)
650
651 url = f"{self._client.base_url}/submodels/{submodel_identifier}/$value"
652
653 self._client.set_token()
654
655 try:
656 response = self._session.patch(url, json=request_body, params=params, timeout=self._client.time_out)
657 _logger.debug(f"Call REST API url '{response.url}'")
658
659 if response.status_code == STATUS_CODE_404:
660 if self._client.get_log_intensity() == LogIntensity.HIGH:
661 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
662 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
663 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
664
665 return False
666
667 if response.status_code != STATUS_CODE_204:
668 log_response(response)
669 return False
670
671 except requests.exceptions.RequestException as e:
672 _logger.error(f"Error call REST API: {e}")
673 return False
674
675 return True
676
677 # GET /submodels/{submodelIdentifier}/$metadata
678 def get_submodel_by_id_metadata(self, submodel_identifier: str, level: str = "") -> dict | None:
679 """Returns the metadata attributes of a specific Submodel.
680
681 :param submodel_identifier: The Submodels unique id
682 :param level: Determines the structural depth of the respective resource content. Available values : deep, core
683 :return: Metadata attributes of the Submodel as dict or None if an error occurred
684 """
685 params: dict[str, str] = {}
686 if level:
687 params["level"] = level
688
689 if not self._client.encoded_ids:
690 submodel_identifier = encode_base_64(submodel_identifier)
691
692 url = f"{self._client.base_url}/submodels/{submodel_identifier}/$metadata"
693
694 self._client.set_token()
695
696 try:
697 response = self._session.get(url, params=params, timeout=self._client.time_out)
698 _logger.debug(f"Call REST API url '{response.url}'")
699
700 if response.status_code == STATUS_CODE_404:
701 if self._client.get_log_intensity() == LogIntensity.HIGH:
702 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
703 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
704 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
705
706 return None
707
708 if response.status_code != STATUS_CODE_200:
709 log_response(response)
710 return None
711
712 except requests.exceptions.RequestException as e:
713 _logger.error(f"Error call REST API: {e}")
714 return None
715
716 content = response.content.decode("utf-8")
717 return json.loads(content)
718
719 # not supported by Java Server
720
721 # PATCH /submodels/{submodelIdentifier}
722 def patch_submodel_by_id(self, submodel_identifier: str, submodel_data: dict) -> bool:
723 """Updates an existing Submodel.
724
725 :param submodel_identifier: The Submodels unique id
726 :return: True if the patch was successful, False otherwise
727 """
728 if not self._client.encoded_ids:
729 submodel_identifier = encode_base_64(submodel_identifier)
731 url = f"{self._client.base_url}/submodels/{submodel_identifier}"
732
733 self._client.set_token()
734
735 try:
736 response = self._session.patch(url, json=submodel_data, timeout=self._client.time_out)
737 _logger.debug(f"Call REST API url '{response.url}'")
738
739 if response.status_code == STATUS_CODE_404:
740 if self._client.get_log_intensity() == LogIntensity.HIGH:
741 _logger.warning(f"Submodel with id '{submodel_identifier}' not found.")
742 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
743 _logger.debug(f"Submodel with id '{submodel_identifier}' not found.")
744
745 return False
746
747 if response.status_code != STATUS_CODE_204:
748 log_response(response)
749 return False
750
751 except requests.exceptions.RequestException as e:
752 _logger.error(f"Error call REST API: {e}")
753 return False
754
755 return True
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.