1"""Experimental implementation of Asset Administration Shell Registry related API calls."""
6from pathlib
import Path
7from typing
import TYPE_CHECKING, Any
12 from aas_http_client.classes.client.aas_client
import AasHttpClient
15from pydantic
import BaseModel
26_logger = logging.getLogger(__name__)
30 """Implementation of Asset Administration Shell Registry related API calls."""
32 def __init__(self, client:
"AasHttpClient"):
33 """Initializes the ExperimentalImplementation with the given client."""
36 session = client.get_session()
39 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating ExperimentalImplementation instance."
42 self._session: requests.Session = session
46 self, id_short: str =
"", is_case_of: str =
"", data_specification_ref: str =
"", limit: int = 100, cursor: str =
""
48 """Retrieves concept descriptions. Experimental feature - may not be supported by all servers.
50 :param id_short: Filter by idShort
51 :param is_case_of: Filter by isCaseOf
52 :param data_specification_ref: Filter by dataSpecificationRef
53 :param limit: Maximum number of results to return
54 :param cursor: Cursor for pagination
55 :return: List of concept descriptions as dictionaries or None if an error occurs
57 url = f
"{self._client.base_url}/concept-descriptions"
61 params: dict[str, Any] = {}
63 params[
"idShort"] = id_short
65 params[
"isCaseOf"] = is_case_of
66 if data_specification_ref:
67 params[
"dataSpecificationRef"] = data_specification_ref
69 params[
"limit"] = str(limit)
71 params[
"cursor"] = cursor
76 response = self._session.get(url, timeout=self.
_client.time_out, params=params)
77 _logger.debug(f
"Call REST API url '{response.url}'")
79 if response.status_code != STATUS_CODE_200:
80 log_response(response)
83 except requests.exceptions.RequestException
as e:
84 _logger.error(f
"Error call REST API: {e}")
87 content = response.content.decode(
"utf-8")
88 return json.loads(content)
92 """Retrieves a specific concept description by its identifier. Experimental feature - may not be supported by all servers.
94 :param concept_description_identifier: The unique identifier of the concept description
95 :return: Concept description data as a dictionary or None if not found
97 url = f
"{self._client.base_url}/concept-descriptions/{concept_description_identifier}"
102 response = self._session.get(url, timeout=self.
_client.time_out)
103 _logger.debug(f
"Call REST API url '{response.url}'")
105 if response.status_code == STATUS_CODE_404:
106 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
107 _logger.warning(f
"Concept description with id '{concept_description_identifier}' not found.")
108 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
109 _logger.debug(f
"Concept description with id '{concept_description_identifier}' not found.")
113 if response.status_code != STATUS_CODE_200:
114 log_response(response)
117 except requests.exceptions.RequestException
as e:
118 _logger.error(f
"Error calling REST API: {e}")
121 return response.json()
125 """Creates a new concept description. Experimental feature - may not be supported by all servers.
127 :param concept_description: Concept description data as a dictionary
128 :return: True if creation was successful, False otherwise
130 url = f
"{self._client.base_url}/concept-descriptions"
135 response = self._session.post(url, json=concept_description, timeout=self.
_client.time_out)
136 _logger.debug(f
"Call REST API url '{response.url}'")
138 if response.status_code == STATUS_CODE_404:
139 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
140 _logger.warning(
"Concept descriptions endpoint not found.")
141 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
142 _logger.debug(
"Concept descriptions endpoint not found.")
146 if response.status_code != STATUS_CODE_201:
147 log_response(response)
150 except requests.exceptions.RequestException
as e:
151 _logger.error(f
"Error calling REST API: {e}")
158 """Deletes a specific concept description by its identifier. Experimental feature - may not be supported by all servers.
160 :param concept_description_identifier: The unique identifier of the concept description
161 :return: True if deletion was successful, False otherwise
163 url = f
"{self._client.base_url}/concept-descriptions/{concept_description_identifier}"
168 response = self._session.delete(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
"Concept description with id '{concept_description_identifier}' not found.")
174 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
175 _logger.debug(f
"Concept description with id '{concept_description_identifier}' not found.")
179 if response.status_code != STATUS_CODE_204:
180 log_response(response)
183 except requests.exceptions.RequestException
as e:
184 _logger.error(f
"Error calling REST API: {e}")
191 """Downloads file content from a specific submodel element from the Submodel at a specified path. Experimental feature - may not be supported by all servers.
193 :param submodel_identifier: The Submodels unique id
194 :param id_short_path: IdShort path to the submodel element (dot-separated)
195 :return: Attachment file data as bytes (octet-stream) or None if an error occurred
197 if not self.
_client.encoded_ids:
198 submodel_identifier = encode_base_64(submodel_identifier)
200 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
205 response = self._session.get(url, timeout=self.
_client.time_out)
206 _logger.debug(f
"Call REST API url '{response.url}'")
208 if response.status_code == STATUS_CODE_404:
209 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
211 f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' or file content not found."
213 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
215 f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' or file content not found."
220 if response.status_code != STATUS_CODE_200:
221 log_response(response)
224 except requests.exceptions.RequestException
as e:
225 _logger.error(f
"Error calling REST API: {e}")
228 return response.content
232 """Uploads file content to an existing submodel element at a specified path within submodel elements hierarchy. Experimental feature - may not be supported by all servers.
234 :param submodel_identifier: The Submodels unique id
235 :param id_short_path: IdShort path to the submodel element (dot-separated)
236 :param file: Path to the file to upload as attachment
237 :return: Attachment data as bytes or None if an error occurred
239 if file.exists()
is False or not file.is_file():
240 _logger.error(f
"Attachment file '{file}' does not exist.")
243 if not self.
_client.encoded_ids:
244 submodel_identifier = encode_base_64(submodel_identifier)
246 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
251 mime_type, _ = mimetypes.guess_type(file)
253 with file.open(
"rb")
as f:
254 files = {
"file": (file.name, f, mime_type
or "application/octet-stream")}
255 response = self._session.post(url, files=files, timeout=self.
_client.time_out)
257 _logger.debug(f
"Call REST API url '{response.url}'")
259 if response.status_code == STATUS_CODE_404:
260 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
261 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
262 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
263 _logger.debug(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
268 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
269 log_response(response)
272 except requests.exceptions.RequestException
as e:
273 _logger.error(f
"Error call REST API: {e}")
280 self, submodel_identifier: str, id_short_path: str, file_octet_stream: Any, mime_type: str =
"application/octet-stream"
282 """Uploads file content to an existing submodel element at a specified path within submodel elements hierarchy. Experimental feature - may not be supported by all servers.
284 :param submodel_identifier: The Submodels unique id
285 :param id_short_path: IdShort path to the submodel element (dot-separated)
286 :param file_octet_stream: File content as a byte stream
287 :return: Attachment data as bytes or None if an error occurred
289 if file_octet_stream
is None:
290 _logger.error(
"Attachment file stream is None.")
293 if not self.
_client.encoded_ids:
294 submodel_identifier = encode_base_64(submodel_identifier)
296 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
301 files = {
"file": (
"attachment", file_octet_stream, mime_type
or "application/octet-stream")}
302 response = self._session.post(url, files=files, timeout=self.
_client.time_out)
304 _logger.debug(f
"Call REST API url '{response.url}'")
306 if response.status_code == STATUS_CODE_404:
307 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
308 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
309 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
310 _logger.debug(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
315 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
316 log_response(response)
319 except requests.exceptions.RequestException
as e:
320 _logger.error(f
"Error call REST API: {e}")
327 """Uploads file content to an existing submodel element at a specified path within submodel elements hierarchy. Experimental feature - may not be supported by all servers.
329 :param submodel_identifier: The Submodels unique id
330 :param id_short_path: IdShort path to the submodel element (dot-separated)
331 :param file: Path to the file to upload as attachment
332 :return: Attachment data as bytes or None if an error occurred
334 if file.exists()
is False or not file.is_file():
335 _logger.error(f
"Attachment file '{file}' does not exist.")
338 if not self.
_client.encoded_ids:
339 submodel_identifier = encode_base_64(submodel_identifier)
341 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
346 mime_type, _ = mimetypes.guess_type(file)
348 with file.open(
"rb")
as f:
349 files = {
"file": (file.name, f, mime_type
or "application/octet-stream")}
350 response = self._session.put(url, files=files, timeout=self.
_client.time_out)
352 _logger.debug(f
"Call REST API url '{response.url}'")
354 if response.status_code == STATUS_CODE_404:
355 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
356 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
357 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
358 _logger.debug(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
363 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
364 log_response(response)
367 except requests.exceptions.RequestException
as e:
368 _logger.error(f
"Error call REST API: {e}")
375 self, submodel_identifier: str, id_short_path: str, file_octet_stream: Any, mime_type: str =
"application/octet-stream"
377 """Uploads file content to an existing submodel element at a specified path within submodel elements hierarchy. Experimental feature - may not be supported by all servers.
379 :param submodel_identifier: The Submodels unique id
380 :param id_short_path: IdShort path to the submodel element (dot-separated)
381 :param file_octet_stream: File content as a byte stream
382 :return: Attachment data as bytes or None if an error occurred
384 if file_octet_stream
is None:
385 _logger.error(
"Attachment file stream is None.")
388 if not self.
_client.encoded_ids:
389 submodel_identifier = encode_base_64(submodel_identifier)
391 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
396 files = {
"file": (
"attachment", file_octet_stream, mime_type
or "application/octet-stream")}
397 response = self._session.put(url, files=files, timeout=self.
_client.time_out)
399 _logger.debug(f
"Call REST API url '{response.url}'")
401 if response.status_code == STATUS_CODE_404:
402 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
403 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
404 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
405 _logger.debug(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
410 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
411 log_response(response)
414 except requests.exceptions.RequestException
as e:
415 _logger.error(f
"Error call REST API: {e}")
422 """Deletes file content of an existing submodel element at a specified path within submodel elements hierarchy. Experimental feature - may not be supported by all servers.
424 :param submodel_identifier: The Submodels unique id
425 :param id_short_path: IdShort path to the submodel element (dot-separated)
426 :return: True if deletion was successful, False otherwise
428 if not self.
_client.encoded_ids:
429 submodel_identifier = encode_base_64(submodel_identifier)
431 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
436 response = self._session.delete(url, timeout=self.
_client.time_out)
437 _logger.debug(f
"Call REST API url '{response.url}'")
439 if response.status_code == 404:
440 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
441 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
442 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
443 _logger.debug(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
447 if response.status_code != STATUS_CODE_200:
448 log_response(response)
451 except requests.exceptions.RequestException
as e:
452 _logger.error(f
"Error calling REST API: {e}")
457 def __post_multipart(self, url, files) -> requests.Response:
458 headers = dict(self._session.headers)
459 headers.pop(
"Content-Type",
None)
460 return self._session.post(url, headers=headers, files=files)
Implementation of Asset Administration Shell Registry related API calls.
bool delete_file_by_path_submodel_repo(self, str submodel_identifier, str id_short_path)
Deletes file content of an existing submodel element at a specified path within submodel elements hie...
bool post_file_by_path_submodel_repo(self, str submodel_identifier, str id_short_path, Path file)
Uploads file content to an existing submodel element at a specified path within submodel elements hie...
bool post_file_by_path_submodel_repo_stream(self, str submodel_identifier, str id_short_path, Any file_octet_stream, str mime_type="application/octet-stream")
Uploads file content to an existing submodel element at a specified path within submodel elements hie...
bytes|None get_file_by_path_submodel_repo(self, str submodel_identifier, str id_short_path)
Downloads file content from a specific submodel element from the Submodel at a specified path.
get_concept_description_by_id(self, str concept_description_identifier)
Retrieves a specific concept description by its identifier.
bool put_file_by_path_submodel_repo_stream(self, str submodel_identifier, str id_short_path, Any file_octet_stream, str mime_type="application/octet-stream")
Uploads file content to an existing submodel element at a specified path within submodel elements hie...
bool post_concept_description(self, dict concept_description)
Creates a new concept description.
__init__(self, "AasHttpClient" client)
Initializes the ExperimentalImplementation with the given client.
bool put_file_by_path_submodel_repo(self, str submodel_identifier, str id_short_path, Path file)
Uploads file content to an existing submodel element at a specified path within submodel elements hie...
get_concept_descriptions(self, str id_short="", str is_case_of="", str data_specification_ref="", int limit=100, str cursor="")
Retrieves concept descriptions.
bool delete_concept_description_by_id(self, str concept_description_identifier)
Deletes a specific concept description by its identifier.