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
24_logger = logging.getLogger(__name__)
28 """Implementation of Asset Administration Shell related API calls."""
30 def __init__(self, client:
"AasHttpClient"):
31 """Initializes the ShellImplementation with the given parameters."""
34 session = client.get_session()
37 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating SubmodelRegistryImplementation instance."
40 self._session: requests.Session = session
44 """Returns a specific Asset Administration Shell.
46 :param aas_identifier: The Asset Administration Shells unique id
47 :return: Asset Administration Shells data or None if an error occurred
49 if not self.
_client.encoded_ids:
50 aas_identifier = encode_base_64(aas_identifier)
52 url = f
"{self._client.base_url}/shells/{aas_identifier}"
57 response = self._session.get(url, timeout=self.
_client.time_out)
58 _logger.debug(f
"Call REST API url '{response.url}'")
60 if response.status_code == STATUS_CODE_404:
61 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
62 _logger.debug(response.text)
65 if response.status_code != STATUS_CODE_200:
66 log_response(response)
69 except requests.exceptions.RequestException
as e:
70 _logger.error(f
"Error call REST API: {e}")
73 content = response.content.decode(
"utf-8")
74 return json.loads(content)
78 """Creates or replaces an existing Asset Administration Shell.
80 :param aas_identifier: The Asset Administration Shells unique id
81 :param request_body: Json data of the Asset Administration Shell data to put
82 :return: True if the update was successful, False otherwise
84 if not self.
_client.encoded_ids:
85 aas_identifier = encode_base_64(aas_identifier)
87 url = f
"{self._client.base_url}/shells/{aas_identifier}"
92 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
93 _logger.debug(f
"Call REST API url '{response.url}'")
95 if response.status_code == STATUS_CODE_404:
96 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
97 _logger.debug(response.text)
100 if response.status_code
is not STATUS_CODE_204:
101 log_response(response)
104 except requests.exceptions.RequestException
as e:
105 _logger.error(f
"Error call REST API: {e}")
112 """Deletes an Asset Administration Shell.
114 :param aas_identifier: The Asset Administration Shells unique id
115 :return: True if the deletion was successful, False otherwise
117 if not self.
_client.encoded_ids:
118 aas_identifier = encode_base_64(aas_identifier)
120 url = f
"{self._client.base_url}/shells/{aas_identifier}"
125 response = self._session.delete(url, timeout=self.
_client.time_out)
126 _logger.debug(f
"Call REST API url '{response.url}'")
128 if response.status_code == STATUS_CODE_404:
129 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
130 _logger.debug(response.text)
133 if response.status_code != STATUS_CODE_204:
134 log_response(response)
137 except requests.exceptions.RequestException
as e:
138 _logger.error(f
"Error call REST API: {e}")
145 """Returns the thumbnail of the Asset Administration Shell.
147 :param aas_identifier: The Asset Administration Shells unique id
148 :return: Thumbnail file data as bytes (octet-stream) or None if an error occurred
150 if not self.
_client.encoded_ids:
151 aas_identifier = encode_base_64(aas_identifier)
153 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
158 response = self._session.get(url, timeout=self.
_client.time_out)
159 _logger.debug(f
"Call REST API url '{response.url}'")
161 if response.status_code == STATUS_CODE_404:
162 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
163 _logger.debug(response.text)
166 if response.status_code != STATUS_CODE_200:
167 log_response(response)
170 except requests.exceptions.RequestException
as e:
171 _logger.error(f
"Error call REST API: {e}")
174 return response.content
177 """Creates or updates the thumbnail of the Asset Administration Shell.
179 :param aas_identifier: The Asset Administration Shells unique id
180 :param file_name: The name of the thumbnail file
181 :param file: Path to the thumbnail file to upload as attachment
182 :return: True if the update was successful, False otherwise
184 if file.exists()
is False or not file.is_file():
185 _logger.error(f
"Attachment file '{file}' does not exist.")
188 mime_type, _ = mimetypes.guess_type(file)
190 with file.open(
"rb")
as f:
191 file_octet_stream = f.read()
197 self, aas_identifier: str, file_name: str, file_octet_stream: Any, mime_type: str =
"application/octet-stream"
199 """Creates or updates the thumbnail of the Asset Administration Shell.
201 :param aas_identifier: The Asset Administration Shells unique id
202 :param file_name: The name of the thumbnail file
203 :param file_octet_stream: The octet stream of the thumbnail file
204 :param mime_type: The MIME type of the thumbnail file (e.g., "image/png")
205 :return: True if the update was successful, False otherwise
207 if file_name
is None or file_name ==
"" or file_octet_stream
is None or mime_type
is None or mime_type ==
"":
208 _logger.error(f
"Attachment file '{file_name}' does not exist.")
211 if not self.
_client.encoded_ids:
212 aas_identifier = encode_base_64(aas_identifier)
214 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
216 params = {
"fileName": file_name}
221 files: dict[str, tuple[str, Any, str]] = {
"file": (file_name, file_octet_stream, mime_type)}
222 response = self._session.put(url, files=files, params=params, timeout=self.
_client.time_out)
224 _logger.debug(f
"Call REST API url '{response.url}'")
226 if response.status_code == STATUS_CODE_404:
227 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
228 _logger.debug(response.text)
232 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
233 log_response(response)
236 except requests.exceptions.RequestException
as e:
237 _logger.error(f
"Error call REST API: {e}")
244 """Deletes the thumbnail of the Asset Administration Shell.
246 :param aas_identifier: The Asset Administration Shells unique id
247 :return: True if the deletion was successful, False otherwise
249 if not self.
_client.encoded_ids:
250 aas_identifier = encode_base_64(aas_identifier)
252 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
257 response = self._session.delete(url, timeout=self.
_client.time_out)
258 _logger.debug(f
"Call REST API url '{response.url}'")
260 if response.status_code == STATUS_CODE_404:
261 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
262 _logger.debug(response.text)
265 if response.status_code != STATUS_CODE_200:
266 log_response(response)
269 except requests.exceptions.RequestException
as e:
270 _logger.error(f
"Error call REST API: {e}")
277 self, asset_ids: list[dict] |
None =
None, id_short: str =
"", limit: int = 100, cursor: str =
""
279 """Returns all Asset Administration Shells.
281 :param assetIds: A list of specific Asset identifiers (format: {"identifier": "string", "encodedIdentifier": "string"})
282 :param idShort: The Asset Administration Shells IdShort
283 :param limit: The maximum number of elements in the response array
284 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
285 :return: List of paginated Asset Administration Shells data or None if an error occurred
287 url = f
"{self._client.base_url}/shells"
290 if asset_ids
is None:
293 params: dict[str, Any] = {}
294 if asset_ids
is not None and len(asset_ids) > 0:
295 params[
"assetIds"] = asset_ids
297 params[
"idShort"] = id_short
299 params[
"limit"] = str(limit)
301 params[
"cursor"] = cursor
306 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
307 _logger.debug(f
"Call REST API url '{response.url}'")
309 if response.status_code != STATUS_CODE_200:
310 log_response(response)
313 except requests.exceptions.RequestException
as e:
314 _logger.error(f
"Error call REST API: {e}")
317 content = response.content.decode(
"utf-8")
318 return json.loads(content)
322 """Creates a new Asset Administration Shell.
324 :param request_body: Json data of the Asset Administration Shell to post
325 :return: Response data as a dictionary or None if an error occurred
327 url = f
"{self._client.base_url}/shells"
332 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
333 _logger.debug(f
"Call REST API url '{response.url}'")
335 if response.status_code != STATUS_CODE_201:
336 log_response(response)
339 except requests.exceptions.RequestException
as e:
340 _logger.error(f
"Error call REST API: {e}")
343 content = response.content.decode(
"utf-8")
344 return json.loads(content)
348 """Returns all submodel references.
350 :param aas_identifier: The Asset Administration Shells unique id
351 :param limit: The maximum number of elements in the response array
352 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
353 :return: List of Submodel references or None if an error occurred
355 if not self.
_client.encoded_ids:
356 aas_identifier = encode_base_64(aas_identifier)
358 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs"
360 params: dict[str, str] = {}
362 params[
"limit"] = str(limit)
364 params[
"cursor"] = cursor
369 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
370 _logger.debug(f
"Call REST API url '{response.url}'")
372 if response.status_code == STATUS_CODE_404:
373 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
374 _logger.debug(response.text)
377 if response.status_code != STATUS_CODE_200:
378 log_response(response)
381 except requests.exceptions.RequestException
as e:
382 _logger.error(f
"Error call REST API: {e}")
385 content = response.content.decode(
"utf-8")
386 return json.loads(content)
390 """Creates a submodel reference at the Asset Administration Shell.
392 :param aas_identifier: The Asset Administration Shells unique id
393 :param request_body: Reference to the Submodel
394 :return: Response data as a dictionary or None if an error occurred
396 if not self.
_client.encoded_ids:
397 aas_identifier = encode_base_64(aas_identifier)
399 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs"
404 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
405 _logger.debug(f
"Call REST API url '{response.url}'")
407 if response.status_code == STATUS_CODE_404:
408 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
409 _logger.debug(response.text)
412 if response.status_code != STATUS_CODE_201:
413 log_response(response)
416 except requests.exceptions.RequestException
as e:
417 _logger.error(f
"Error call REST API: {e}")
420 content = response.content.decode(
"utf-8")
421 return json.loads(content)
425 """Deletes the submodel reference from the Asset Administration Shell. Does not delete the submodel itself.
427 :param aas_identifier: The Asset Administration Shells unique id
428 :param submodel_identifier: The Submodels unique id
429 :return: True if the deletion was successful, False otherwise
431 if not self.
_client.encoded_ids:
432 aas_identifier = encode_base_64(aas_identifier)
433 submodel_identifier = encode_base_64(submodel_identifier)
435 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs/{submodel_identifier}"
440 response = self._session.delete(url, timeout=self.
_client.time_out)
441 _logger.debug(f
"Call REST API url '{response.url}'")
443 if response.status_code == STATUS_CODE_404:
444 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
445 _logger.debug(response.text)
448 if response.status_code
not in (STATUS_CODE_204, STATUS_CODE_200):
449 log_response(response)
452 except requests.exceptions.RequestException
as e:
453 _logger.error(f
"Error call REST API: {e}")
462 """Updates the Submodel.
464 :param aas_identifier: ID of the AAS to update the submodel for
465 :param submodel_identifier: ID of the submodel to update
466 :param request_body: Json data to the Submodel to put
467 :return: True if the update was successful, False otherwise
469 if not self.
_client.encoded_ids:
470 aas_identifier = encode_base_64(aas_identifier)
471 submodel_identifier = encode_base_64(submodel_identifier)
473 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodels/{submodel_identifier}"
478 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
479 _logger.debug(f
"Call REST API url '{response.url}'")
481 if response.status_code == STATUS_CODE_404:
482 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
483 _logger.debug(response.text)
486 if response.status_code != STATUS_CODE_204:
487 log_response(response)
490 except requests.exceptions.RequestException
as e:
491 _logger.error(f
"Error call REST API: {e}")
498 """Returns a specific Asset Administration Shell as a Reference.
500 :param aas_identifier: ID of the AAS reference to retrieve
501 :return: Asset Administration Shells reference data or None if an error occurred
503 if not self.
_client.encoded_ids:
504 aas_identifier = encode_base_64(aas_identifier)
506 url = f
"{self._client.base_url}/shells/{aas_identifier}/$reference"
511 response = self._session.get(url, timeout=self.
_client.time_out)
512 _logger.debug(f
"Call REST API url '{response.url}'")
514 if response.status_code == STATUS_CODE_404:
515 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
516 _logger.debug(response.text)
519 if response.status_code != STATUS_CODE_200:
520 log_response(response)
523 except requests.exceptions.RequestException
as e:
524 _logger.error(f
"Error call REST API: {e}")
527 ref_dict_string = response.content.decode(
"utf-8")
528 return json.loads(ref_dict_string)
532 """Returns the Submodel.
534 :param aas_identifier: ID of the AAS to retrieve the submodel from
535 :param submodel_identifier: ID of the submodel to retrieve
536 :return: Submodel object or None if an error occurred
538 if not self.
_client.encoded_ids:
539 aas_identifier = encode_base_64(aas_identifier)
540 submodel_identifier = encode_base_64(submodel_identifier)
542 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodels/{submodel_identifier}"
547 response = self._session.get(url, 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
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
552 _logger.debug(response.text)
555 if response.status_code != STATUS_CODE_200:
556 log_response(response)
559 except requests.exceptions.RequestException
as e:
560 _logger.error(f
"Error call REST API: {e}")
563 content = response.content.decode(
"utf-8")
564 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.