1"""Experimental implementation of Asset Administration Shell Registry related API calls."""
5from pathlib
import Path
6from typing
import TYPE_CHECKING
9 from aas_http_client.classes.client.aas_client
import AasHttpClient
12from pydantic
import BaseModel
22_logger = logging.getLogger(__name__)
26 """Implementation of Asset Administration Shell Registry related API calls."""
28 def __init__(self, client:
"AasHttpClient"):
29 """Initializes the ExperimentalImplementation with the given client."""
32 session = client.get_session()
35 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating SubmodelRegistryImplementation instance."
38 self._session: requests.Session = session
42 """Downloads file content from a specific submodel element from the Submodel at a specified path. Experimental feature - may not be supported by all servers.
44 :param submodel_identifier: The Submodels unique id
45 :param id_short_path: IdShort path to the submodel element (dot-separated)
46 :return: Attachment file data as bytes (octet-stream) or None if an error occurred
48 if not self.
_client.encoded_ids:
49 submodel_identifier = encode_base_64(submodel_identifier)
51 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
56 response = self._session.get(url, timeout=self.
_client.time_out)
57 _logger.debug(f
"Call REST API url '{response.url}'")
59 if response.status_code == STATUS_CODE_404:
61 f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' or file content not found."
63 _logger.debug(response.text)
66 if response.status_code != STATUS_CODE_200:
67 log_response(response)
70 except requests.exceptions.RequestException
as e:
71 _logger.error(f
"Error calling REST API: {e}")
74 return response.content
78 """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.
80 :param submodel_identifier: The Submodels unique id
81 :param id_short_path: IdShort path to the submodel element (dot-separated)
82 :param file: Path to the file to upload as attachment
83 :return: Attachment data as bytes or None if an error occurred
85 if file.exists()
is False or not file.is_file():
86 _logger.error(f
"Attachment file '{file}' does not exist.")
89 if not self.
_client.encoded_ids:
90 submodel_identifier = encode_base_64(submodel_identifier)
92 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
97 mime_type, _ = mimetypes.guess_type(file)
99 with file.open(
"rb")
as f:
100 files = {
"file": (file.name, f, mime_type
or "application/octet-stream")}
101 response = self._session.post(url, files=files, timeout=self.
_client.time_out)
103 _logger.debug(f
"Call REST API url '{response.url}'")
105 if response.status_code == STATUS_CODE_404:
106 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
107 _logger.debug(response.text)
111 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
112 log_response(response)
115 except requests.exceptions.RequestException
as e:
116 _logger.error(f
"Error call REST API: {e}")
123 """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.
125 :param submodel_identifier: The Submodels unique id
126 :param id_short_path: IdShort path to the submodel element (dot-separated)
127 :param file: Path to the file to upload as attachment
128 :return: Attachment data as bytes or None if an error occurred
130 if file.exists()
is False or not file.is_file():
131 _logger.error(f
"Attachment file '{file}' does not exist.")
134 if not self.
_client.encoded_ids:
135 submodel_identifier = encode_base_64(submodel_identifier)
137 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
142 mime_type, _ = mimetypes.guess_type(file)
144 with file.open(
"rb")
as f:
145 files = {
"file": (file.name, f, mime_type
or "application/octet-stream")}
146 response = self._session.put(url, files=files, timeout=self.
_client.time_out)
148 _logger.debug(f
"Call REST API url '{response.url}'")
150 if response.status_code == STATUS_CODE_404:
151 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
152 _logger.debug(response.text)
156 if response.status_code
not in (STATUS_CODE_200, STATUS_CODE_204):
157 log_response(response)
160 except requests.exceptions.RequestException
as e:
161 _logger.error(f
"Error call REST API: {e}")
168 """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.
170 :param submodel_identifier: The Submodels unique id
171 :param id_short_path: IdShort path to the submodel element (dot-separated)
172 :return: True if deletion was successful, False otherwise
174 if not self.
_client.encoded_ids:
175 submodel_identifier = encode_base_64(submodel_identifier)
177 url = f
"{self._client.base_url}/submodels/{submodel_identifier}/submodel-elements/{id_short_path}/attachment"
182 response = self._session.delete(url, timeout=self.
_client.time_out)
183 _logger.debug(f
"Call REST API url '{response.url}'")
185 if response.status_code == 404:
186 _logger.warning(f
"Submodel with id '{submodel_identifier}' or Submodel element with IDShort path '{id_short_path}' not found.")
189 if response.status_code != STATUS_CODE_200:
190 log_response(response)
193 except requests.exceptions.RequestException
as e:
194 _logger.error(f
"Error calling REST API: {e}")
200 headers = dict(self._session.headers)
201 headers.pop(
"Content-Type",
None)
202 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...
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.
__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...
_post_multipart(self, url, files)