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
178 """Creates or updates the thumbnail of the Asset Administration Shell.
180 :param aas_identifier: The Asset Administration Shells unique id
181 :param file_name: The name of the thumbnail file
182 :param file: Path to the thumbnail file to upload as attachment
183 :return: True if the update was successful, False otherwise
185 if file.exists()
is False or not file.is_file():
186 _logger.error(f
"Attachment file '{file}' does not exist.")
189 if not self.
_client.encoded_ids:
190 aas_identifier = encode_base_64(aas_identifier)
192 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
194 params = {
"fileName": file_name}
199 mime_type, _ = mimetypes.guess_type(file)
201 with file.open(
"rb")
as f:
202 files = {
"file": (file.name, f, mime_type
or "application/octet-stream")}
203 response = self._session.put(url, files=files, params=params, timeout=self.
_client.time_out)
205 _logger.debug(f
"Call REST API url '{response.url}'")
207 if response.status_code == STATUS_CODE_404:
208 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
209 _logger.debug(response.text)
213 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
214 log_response(response)
217 except requests.exceptions.RequestException
as e:
218 _logger.error(f
"Error call REST API: {e}")
225 """Deletes the thumbnail of the Asset Administration Shell.
227 :param aas_identifier: The Asset Administration Shells unique id
228 :return: True if the deletion was successful, False otherwise
230 if not self.
_client.encoded_ids:
231 aas_identifier = encode_base_64(aas_identifier)
233 url = f
"{self._client.base_url}/shells/{aas_identifier}/asset-information/thumbnail"
238 response = self._session.delete(url, timeout=self.
_client.time_out)
239 _logger.debug(f
"Call REST API url '{response.url}'")
241 if response.status_code == STATUS_CODE_404:
242 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or thumbnail file not found.")
243 _logger.debug(response.text)
246 if response.status_code != STATUS_CODE_200:
247 log_response(response)
250 except requests.exceptions.RequestException
as e:
251 _logger.error(f
"Error call REST API: {e}")
258 self, asset_ids: list[dict] |
None =
None, id_short: str =
"", limit: int = 100, cursor: str =
""
260 """Returns all Asset Administration Shells.
262 :param assetIds: A list of specific Asset identifiers (format: {"identifier": "string", "encodedIdentifier": "string"})
263 :param idShort: The Asset Administration Shells IdShort
264 :param limit: The maximum number of elements in the response array
265 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
266 :return: List of paginated Asset Administration Shells data or None if an error occurred
268 url = f
"{self._client.base_url}/shells"
271 if asset_ids
is None:
274 params: dict[str, Any] = {}
275 if asset_ids
is not None and len(asset_ids) > 0:
276 params[
"assetIds"] = asset_ids
278 params[
"idShort"] = id_short
280 params[
"limit"] = str(limit)
282 params[
"cursor"] = cursor
287 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
288 _logger.debug(f
"Call REST API url '{response.url}'")
290 if response.status_code != STATUS_CODE_200:
291 log_response(response)
294 except requests.exceptions.RequestException
as e:
295 _logger.error(f
"Error call REST API: {e}")
298 content = response.content.decode(
"utf-8")
299 return json.loads(content)
303 """Creates a new Asset Administration Shell.
305 :param request_body: Json data of the Asset Administration Shell to post
306 :return: Response data as a dictionary or None if an error occurred
308 url = f
"{self._client.base_url}/shells"
313 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
314 _logger.debug(f
"Call REST API url '{response.url}'")
316 if response.status_code != STATUS_CODE_201:
317 log_response(response)
320 except requests.exceptions.RequestException
as e:
321 _logger.error(f
"Error call REST API: {e}")
324 content = response.content.decode(
"utf-8")
325 return json.loads(content)
329 """Returns all submodel references.
331 :param aas_identifier: The Asset Administration Shells unique id
332 :param limit: The maximum number of elements in the response array
333 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
334 :return: List of Submodel references or None if an error occurred
336 if not self.
_client.encoded_ids:
337 aas_identifier = encode_base_64(aas_identifier)
339 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs"
341 params: dict[str, str] = {}
343 params[
"limit"] = str(limit)
345 params[
"cursor"] = cursor
350 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
351 _logger.debug(f
"Call REST API url '{response.url}'")
353 if response.status_code == STATUS_CODE_404:
354 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
355 _logger.debug(response.text)
358 if response.status_code != STATUS_CODE_200:
359 log_response(response)
362 except requests.exceptions.RequestException
as e:
363 _logger.error(f
"Error call REST API: {e}")
366 content = response.content.decode(
"utf-8")
367 return json.loads(content)
371 """Creates a submodel reference at the Asset Administration Shell.
373 :param aas_identifier: The Asset Administration Shells unique id
374 :param request_body: Reference to the Submodel
375 :return: Response data as a dictionary or None if an error occurred
377 if not self.
_client.encoded_ids:
378 aas_identifier = encode_base_64(aas_identifier)
380 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs"
385 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
386 _logger.debug(f
"Call REST API url '{response.url}'")
388 if response.status_code == STATUS_CODE_404:
389 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
390 _logger.debug(response.text)
393 if response.status_code != STATUS_CODE_201:
394 log_response(response)
397 except requests.exceptions.RequestException
as e:
398 _logger.error(f
"Error call REST API: {e}")
401 content = response.content.decode(
"utf-8")
402 return json.loads(content)
406 """Deletes the submodel reference from the Asset Administration Shell. Does not delete the submodel itself.
408 :param aas_identifier: The Asset Administration Shells unique id
409 :param submodel_identifier: The Submodels unique id
410 :return: True if the deletion was successful, False otherwise
412 if not self.
_client.encoded_ids:
413 aas_identifier = encode_base_64(aas_identifier)
414 submodel_identifier = encode_base_64(submodel_identifier)
416 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodel-refs/{submodel_identifier}"
421 response = self._session.delete(url, timeout=self.
_client.time_out)
422 _logger.debug(f
"Call REST API url '{response.url}'")
424 if response.status_code == STATUS_CODE_404:
425 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
426 _logger.debug(response.text)
429 if response.status_code
not in (STATUS_CODE_204, STATUS_CODE_200):
430 log_response(response)
433 except requests.exceptions.RequestException
as e:
434 _logger.error(f
"Error call REST API: {e}")
443 """Updates the Submodel.
445 :param aas_identifier: ID of the AAS to update the submodel for
446 :param submodel_identifier: ID of the submodel to update
447 :param request_body: Json data to the Submodel to put
448 :return: True if the update was successful, False otherwise
450 if not self.
_client.encoded_ids:
451 aas_identifier = encode_base_64(aas_identifier)
452 submodel_identifier = encode_base_64(submodel_identifier)
454 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodels/{submodel_identifier}"
459 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
460 _logger.debug(f
"Call REST API url '{response.url}'")
462 if response.status_code == STATUS_CODE_404:
463 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
464 _logger.debug(response.text)
467 if response.status_code != STATUS_CODE_204:
468 log_response(response)
471 except requests.exceptions.RequestException
as e:
472 _logger.error(f
"Error call REST API: {e}")
479 """Returns a specific Asset Administration Shell as a Reference.
481 :param aas_identifier: ID of the AAS reference to retrieve
482 :return: Asset Administration Shells reference data or None if an error occurred
484 if not self.
_client.encoded_ids:
485 aas_identifier = encode_base_64(aas_identifier)
487 url = f
"{self._client.base_url}/shells/{aas_identifier}/$reference"
492 response = self._session.get(url, timeout=self.
_client.time_out)
493 _logger.debug(f
"Call REST API url '{response.url}'")
495 if response.status_code == STATUS_CODE_404:
496 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' not found.")
497 _logger.debug(response.text)
500 if response.status_code != STATUS_CODE_200:
501 log_response(response)
504 except requests.exceptions.RequestException
as e:
505 _logger.error(f
"Error call REST API: {e}")
508 ref_dict_string = response.content.decode(
"utf-8")
509 return json.loads(ref_dict_string)
513 """Returns the Submodel.
515 :param aas_identifier: ID of the AAS to retrieve the submodel from
516 :param submodel_identifier: ID of the submodel to retrieve
517 :return: Submodel object or None if an error occurred
519 if not self.
_client.encoded_ids:
520 aas_identifier = encode_base_64(aas_identifier)
521 submodel_identifier = encode_base_64(submodel_identifier)
523 url = f
"{self._client.base_url}/shells/{aas_identifier}/submodels/{submodel_identifier}"
528 response = self._session.get(url, timeout=self.
_client.time_out)
529 _logger.debug(f
"Call REST API url '{response.url}'")
531 if response.status_code == STATUS_CODE_404:
532 _logger.warning(f
"Asset Administration Shell with id '{aas_identifier}' or submodel with id '{submodel_identifier}' not found.")
533 _logger.debug(response.text)
536 if response.status_code != STATUS_CODE_200:
537 log_response(response)
540 except requests.exceptions.RequestException
as e:
541 _logger.error(f
"Error call REST API: {e}")
544 content = response.content.decode(
"utf-8")
545 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.
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.