1"""Implementation of Asset Administration Shell related API calls."""
6from pathlib
import Path
7from typing
import TYPE_CHECKING, Any
10from pydantic
import BaseModel
13 from aas_http_client.classes.client.aas_client
import AasHttpClient
25_logger = logging.getLogger(__name__)
29 """Implementation of Asset Administration Shell related API calls."""
31 def __init__(self, client:
"AasHttpClient"):
32 """Initializes the ShellImplementation with the given parameters."""
35 session = client.get_session()
38 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating ShellRepoImplementation instance."
41 self._session: requests.Session = session
45 """Returns a specific Asset Administration Shell.
47 :param aas_identifier: The Asset Administration Shells unique id
48 :return: Asset Administration Shells data or None if an error occurred
50 if not self.
_client.encoded_ids:
51 aas_identifier = encode_base_64(aas_identifier)
53 url = f
"{self._client.base_url}/shells/{aas_identifier}"
58 response = self._session.get(url, timeout=self.
_client.time_out)
59 _logger.debug(f
"Call REST API url '{response.url}'")
61 if response.status_code == STATUS_CODE_404:
62 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
63 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
64 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
65 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
69 if response.status_code != STATUS_CODE_200:
70 log_response(response)
73 except requests.exceptions.RequestException
as e:
74 _logger.error(f
"Error call REST API: {e}")
77 content = response.content.decode(
"utf-8")
78 return json.loads(content)
82 """Creates or replaces an existing Asset Administration Shell.
84 :param aas_identifier: The Asset Administration Shells unique id
85 :param request_body: Json data of the Asset Administration Shell data to put
86 :return: True if the update was successful, False otherwise
88 if not self.
_client.encoded_ids:
89 aas_identifier = encode_base_64(aas_identifier)
91 url = f
"{self._client.base_url}/shells/{aas_identifier}"
96 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
97 _logger.debug(f
"Call REST API url '{response.url}'")
99 if response.status_code == STATUS_CODE_404:
100 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
101 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
102 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
103 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
107 if response.status_code != STATUS_CODE_204:
108 log_response(response)
111 except requests.exceptions.RequestException
as e:
112 _logger.error(f
"Error call REST API: {e}")
119 """Deletes an Asset Administration Shell.
121 :param aas_identifier: The Asset Administration Shells unique id
122 :return: True if the deletion was successful, False otherwise
124 if not self.
_client.encoded_ids:
125 aas_identifier = encode_base_64(aas_identifier)
127 url = f
"{self._client.base_url}/shells/{aas_identifier}"
132 response = self._session.delete(url, timeout=self.
_client.time_out)
133 _logger.debug(f
"Call REST API url '{response.url}'")
135 if response.status_code == STATUS_CODE_404:
136 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
137 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
138 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
139 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
143 if response.status_code != STATUS_CODE_204:
144 log_response(response)
147 except requests.exceptions.RequestException
as e:
148 _logger.error(f
"Error call REST API: {e}")
155 """Returns the thumbnail of the Asset Administration Shell.
157 :param aas_identifier: The Asset Administration Shells unique id
158 :return: Thumbnail file data as bytes (octet-stream) or None if an error occurred
160 if not self.
_client.encoded_ids:
161 aas_identifier = encode_base_64(aas_identifier)
163 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
168 response = self._session.get(url, timeout=self.
_client.time_out)
169 _logger.debug(f
"Call REST API url '{response.url}'")
171 if response.status_code == STATUS_CODE_404:
172 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
173 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
174 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
175 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
179 if response.status_code != STATUS_CODE_200:
180 log_response(response)
183 except requests.exceptions.RequestException
as e:
184 _logger.error(f
"Error call REST API: {e}")
187 return response.content
190 """Creates or updates the thumbnail of the Asset Administration Shell.
192 :param aas_identifier: The Asset Administration Shells unique id
193 :param file_name: The name of the thumbnail file
194 :param file: Path to the thumbnail file to upload as attachment
195 :return: True if the update was successful, False otherwise
197 if file.exists()
is False or not file.is_file():
198 _logger.error(f
"Attachment file '{file}' does not exist.")
201 mime_type, _ = mimetypes.guess_type(file)
203 with file.open(
"rb")
as f:
204 file_octet_stream = f.read()
210 self, aas_identifier: str, file_name: str, file_octet_stream: Any, mime_type: str =
"application/octet-stream"
212 """Creates or updates the thumbnail of the Asset Administration Shell.
214 :param aas_identifier: The Asset Administration Shells unique id
215 :param file_name: The name of the thumbnail file
216 :param file_octet_stream: The octet stream of the thumbnail file
217 :param mime_type: The MIME type of the thumbnail file (e.g., "image/png")
218 :return: True if the update was successful, False otherwise
220 if file_name
is None or file_name ==
"" or file_octet_stream
is None or mime_type
is None or mime_type ==
"":
221 _logger.error(f
"Attachment file '{file_name}' does not exist.")
224 if not self.
_client.encoded_ids:
225 aas_identifier = encode_base_64(aas_identifier)
227 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
229 params = {
"fileName": file_name}
234 files: dict[str, tuple[str, Any, str]] = {
"file": (file_name, file_octet_stream, mime_type)}
235 response = self._session.put(url, files=files, params=params, timeout=self.
_client.time_out)
237 _logger.debug(f
"Call REST API url '{response.url}'")
239 if response.status_code == STATUS_CODE_404:
240 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
241 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
242 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
243 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
248 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
249 log_response(response)
252 except requests.exceptions.RequestException
as e:
253 _logger.error(f
"Error call REST API: {e}")
260 """Deletes the thumbnail of the Asset Administration Shell.
262 :param aas_identifier: The Asset Administration Shells unique id
263 :return: True if the deletion was successful, False otherwise
265 if not self.
_client.encoded_ids:
266 aas_identifier = encode_base_64(aas_identifier)
268 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
273 response = self._session.delete(url, timeout=self.
_client.time_out)
274 _logger.debug(f
"Call REST API url '{response.url}'")
276 if response.status_code == STATUS_CODE_404:
277 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
278 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
279 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
280 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
284 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
285 log_response(response)
288 except requests.exceptions.RequestException
as e:
289 _logger.error(f
"Error call REST API: {e}")
296 self, asset_ids: list[dict] |
None =
None, id_short: str =
"", limit: int = 100, cursor: str =
""
298 """Returns all Asset Administration Shells.
300 :param assetIds: A list of specific Asset identifiers (format: {"identifier": "string", "encodedIdentifier": "string"})
301 :param idShort: The Asset Administration Shells IdShort
302 :param limit: The maximum number of elements in the response array
303 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
304 :return: List of paginated Asset Administration Shells data or None if an error occurred
306 url = f
"{self._client.base_url}/shells"
309 if asset_ids
is None:
312 params: dict[str, Any] = {}
313 if asset_ids
is not None and len(asset_ids) > 0:
314 params[
"assetIds"] = asset_ids
316 params[
"idShort"] = id_short
318 params[
"limit"] = str(limit)
320 params[
"cursor"] = cursor
325 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
326 _logger.debug(f
"Call REST API url '{response.url}'")
328 if response.status_code != STATUS_CODE_200:
329 log_response(response)
332 except requests.exceptions.RequestException
as e:
333 _logger.error(f
"Error call REST API: {e}")
336 content = response.content.decode(
"utf-8")
337 return json.loads(content)
341 """Creates a new Asset Administration Shell.
343 :param request_body: Json data of the Asset Administration Shell to post
344 :return: Response data as a dictionary or None if an error occurred
346 url = f
"{self._client.base_url}/shells"
351 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
352 _logger.debug(f
"Call REST API url '{response.url}'")
354 if response.status_code != STATUS_CODE_201:
355 log_response(response)
358 except requests.exceptions.RequestException
as e:
359 _logger.error(f
"Error call REST API: {e}")
362 content = response.content.decode(
"utf-8")
363 return json.loads(content)
367 """Returns all submodel references.
369 :param aas_identifier: The Asset Administration Shells unique id
370 :param limit: The maximum number of elements in the response array
371 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
372 :return: List of Submodel references or None if an error occurred
374 if not self.
_client.encoded_ids:
375 aas_identifier = encode_base_64(aas_identifier)
377 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs"
379 params: dict[str, str] = {}
381 params[
"limit"] = str(limit)
383 params[
"cursor"] = cursor
388 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
389 _logger.debug(f
"Call REST API url '{response.url}'")
391 if response.status_code == STATUS_CODE_404:
392 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
393 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
394 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
395 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
399 if response.status_code != STATUS_CODE_200:
400 log_response(response)
403 except requests.exceptions.RequestException
as e:
404 _logger.error(f
"Error call REST API: {e}")
407 content = response.content.decode(
"utf-8")
408 return json.loads(content)
412 """Creates a submodel reference at the Asset Administration Shell.
414 :param aas_identifier: The Asset Administration Shells unique id
415 :param request_body: Reference to the Submodel
416 :return: Response data as a dictionary or None if an error occurred
418 if not self.
_client.encoded_ids:
419 aas_identifier = encode_base_64(aas_identifier)
421 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs"
426 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
427 _logger.debug(f
"Call REST API url '{response.url}'")
429 if response.status_code == STATUS_CODE_404:
430 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
431 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
432 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
433 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
437 if response.status_code != STATUS_CODE_201:
438 log_response(response)
441 except requests.exceptions.RequestException
as e:
442 _logger.error(f
"Error call REST API: {e}")
445 content = response.content.decode(
"utf-8")
446 return json.loads(content)
450 """Deletes the submodel reference from the Asset Administration Shell. Does not delete the submodel itself.
452 :param aas_identifier: The Asset Administration Shells unique id
453 :param submodel_identifier: The Submodels unique id
454 :return: True if the deletion was successful, False otherwise
456 if not self.
_client.encoded_ids:
457 aas_identifier = encode_base_64(aas_identifier)
458 submodel_identifier = encode_base_64(submodel_identifier)
460 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs/{submodel_identifier}"
465 response = self._session.delete(url, timeout=self.
_client.time_out)
466 _logger.debug(f
"Call REST API url '{response.url}'")
468 if response.status_code == STATUS_CODE_404:
469 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
470 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
471 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
472 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
476 if response.status_code
not in (STATUS_CODE_204, STATUS_CODE_200):
477 log_response(response)
480 except requests.exceptions.RequestException
as e:
481 _logger.error(f
"Error call REST API: {e}")
490 """Updates the Submodel.
492 :param aas_identifier: ID of the AAS to update the submodel for
493 :param submodel_identifier: ID of the submodel to update
494 :param request_body: Json data to the Submodel to put
495 :return: True if the update was successful, False otherwise
497 if not self.
_client.encoded_ids:
498 aas_identifier = encode_base_64(aas_identifier)
499 submodel_identifier = encode_base_64(submodel_identifier)
501 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodels/{submodel_identifier}"
506 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
507 _logger.debug(f
"Call REST API url '{response.url}'")
509 if response.status_code == STATUS_CODE_404:
510 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
511 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
512 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
513 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
517 if response.status_code != STATUS_CODE_204:
518 log_response(response)
521 except requests.exceptions.RequestException
as e:
522 _logger.error(f
"Error call REST API: {e}")
529 """Returns a specific Asset Administration Shell as a Reference.
531 :param aas_identifier: ID of the AAS reference to retrieve
532 :return: Asset Administration Shells reference data or None if an error occurred
534 if not self.
_client.encoded_ids:
535 aas_identifier = encode_base_64(aas_identifier)
537 url = f
"{self._client.base_url}/shells/{aas_identifier}/$reference"
542 response = self._session.get(url, timeout=self.
_client.time_out)
543 _logger.debug(f
"Call REST API url '{response.url}'")
545 if response.status_code == STATUS_CODE_404:
546 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
547 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
548 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
549 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
553 if response.status_code != STATUS_CODE_200:
554 log_response(response)
557 except requests.exceptions.RequestException
as e:
558 _logger.error(f
"Error call REST API: {e}")
561 ref_dict_string = response.content.decode(
"utf-8")
562 return json.loads(ref_dict_string)
566 """Returns the Submodel.
568 :param aas_identifier: ID of the AAS to retrieve the submodel from
569 :param submodel_identifier: ID of the submodel to retrieve
570 :return: Submodel object or None if an error occurred
572 if not self.
_client.encoded_ids:
573 aas_identifier = encode_base_64(aas_identifier)
574 submodel_identifier = encode_base_64(submodel_identifier)
576 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodels/{submodel_identifier}"
581 response = self._session.get(url, timeout=self.
_client.time_out)
582 _logger.debug(f
"Call REST API url '{response.url}'")
584 if response.status_code == STATUS_CODE_404:
585 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
586 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
587 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
588 _logger.debug(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
592 if response.status_code != STATUS_CODE_200:
593 log_response(response)
596 except requests.exceptions.RequestException
as e:
597 _logger.error(f
"Error call REST API: {e}")
600 content = response.content.decode(
"utf-8")
601 return json.loads(content)
Implementation of Asset Administration Shell related API calls.
bool delete_thumbnail_aas_repository(self, str aas_identifier)
Deletes the thumbnail of the Asset Administration Shell.
dict|None post_asset_administration_shell(self, dict request_body)
Creates a new Asset Administration Shell.
dict|None get_all_submodel_references_aas_repository(self, str aas_identifier, int limit=100, str cursor="")
Returns all submodel references.
dict|None get_asset_administration_shell_by_id(self, str aas_identifier)
Returns a specific Asset Administration Shell.
__init__(self, "AasHttpClient" client)
Initializes the ShellImplementation with the given parameters.
bytes|None get_thumbnail_aas_repository(self, str aas_identifier)
Returns the thumbnail of the Asset Administration Shell.
dict|None get_asset_administration_shell_by_id_reference_aas_repository(self, str aas_identifier)
Returns a specific Asset Administration Shell as a Reference.
dict|None get_all_asset_administration_shells(self, list[dict]|None asset_ids=None, str id_short="", int limit=100, str cursor="")
Returns all Asset Administration Shells.
bool put_submodel_by_id_aas_repository(self, str aas_identifier, str submodel_identifier, dict request_body)
Updates the Submodel.
bool delete_asset_administration_shell_by_id(self, str aas_identifier)
Deletes an Asset Administration Shell.
dict|None get_submodel_by_id_aas_repository(self, str aas_identifier, str submodel_identifier)
Returns the Submodel.
bool put_thumbnail_aas_repository_stream(self, str aas_identifier, str file_name, Any file_octet_stream, str mime_type="application/octet-stream")
Creates or updates the thumbnail of the Asset Administration Shell.
dict|None post_submodel_reference_aas_repository(self, str aas_identifier, dict request_body)
Creates a submodel reference at the Asset Administration Shell.
bool delete_submodel_reference_by_id_aas_repository(self, str aas_identifier, str submodel_identifier)
Deletes the submodel reference from the Asset Administration Shell.
bool put_asset_administration_shell_by_id(self, str aas_identifier, dict request_body)
Creates or replaces an existing Asset Administration Shell.
bool put_thumbnail_aas_repository(self, str aas_identifier, str file_name, Path file)
Creates or updates the thumbnail of the Asset Administration Shell.