1"""Submodel Registry Implementation Module."""
5from typing
import TYPE_CHECKING
8from pydantic
import BaseModel
11 from aas_http_client.classes.client.aas_client
import AasHttpClient
24_logger = logging.getLogger(__name__)
28 """Implementation of Submodel Registry related API calls."""
30 def __init__(self, client:
"AasHttpClient"):
31 """Initializes the SubmodelRegistryImplementation with the given client."""
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 the Submodel Descriptor for the given submodel identifier.
46 :param submodel_identifier: The unique identifier of the Submodel Descriptor
47 :return: Submodel Descriptor data or None if an error occurred
49 if not self.
_client.encoded_ids:
50 submodel_identifier = encode_base_64(submodel_identifier)
52 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_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 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
62 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
63 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
64 _logger.debug(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
68 if response.status_code != STATUS_CODE_200:
69 log_response(response)
72 except requests.exceptions.RequestException
as e:
73 _logger.error(f
"Error call REST API: {e}")
76 content = response.content.decode(
"utf-8")
77 return json.loads(content)
81 """Creates or updates an existing Submodel Descriptor.
83 :param submodel_identifier: The unique identifier of the Submodel Descriptor
84 :param request_body: Submodel Descriptor object
85 :return: Updated Submodel Descriptor data or None if an error occurred
87 if not self.
_client.encoded_ids:
88 submodel_identifier = encode_base_64(submodel_identifier)
90 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_identifier}"
95 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
96 _logger.debug(f
"Call REST API url '{response.url}'")
98 if response.status_code == STATUS_CODE_404:
99 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
100 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
101 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
102 _logger.debug(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
106 if response.status_code != STATUS_CODE_204:
107 log_response(response)
110 except requests.exceptions.RequestException
as e:
111 _logger.error(f
"Error call REST API: {e}")
118 """Deletes a Submodel Descriptor, i.e. de-registers a submodel.
120 :param submodel_identifier: The unique identifier of the Submodel Descriptor
121 :return: True if deletion was successful, False otherwise
123 if not self.
_client.encoded_ids:
124 submodel_identifier = encode_base_64(submodel_identifier)
126 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_identifier}"
131 response = self._session.delete(url, timeout=self.
_client.time_out)
132 _logger.debug(f
"Call REST API url '{response.url}'")
134 if response.status_code == STATUS_CODE_404:
135 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
136 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
137 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
138 _logger.debug(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
142 if response.status_code != STATUS_CODE_204:
143 log_response(response)
146 except requests.exceptions.RequestException
as e:
147 _logger.error(f
"Error call REST API: {e}")
154 """Returns all Submodel Descriptors.
156 :param limit: The maximum number of elements in the response array
157 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
158 :return: Submodel Descriptors data or None if an error occurred
160 url = f
"{self._client.base_url}/submodel-descriptors"
162 params: dict[str, str] = {}
164 params[
"limit"] = str(limit)
166 params[
"cursor"] = cursor
171 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
172 _logger.debug(f
"Call REST API url '{response.url}'")
174 if response.status_code != STATUS_CODE_200:
175 log_response(response)
178 except requests.exceptions.RequestException
as e:
179 _logger.error(f
"Error call REST API: {e}")
182 content = response.content.decode(
"utf-8")
183 return json.loads(content)
187 """Creates a new Submodel Descriptor, i.e. registers a submodel.
189 :param request_body: Submodel Descriptor object
190 :return: Created Submodel Descriptor data or None if an error occurred
192 url = f
"{self._client.base_url}/submodel-descriptors"
197 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
198 _logger.debug(f
"Call REST API url '{response.url}'")
200 if response.status_code != STATUS_CODE_201:
201 log_response(response)
204 except requests.exceptions.RequestException
as e:
205 _logger.error(f
"Error call REST API: {e}")
208 content = response.content.decode(
"utf-8")
209 return json.loads(content)
213 """Deletes all Submodel Descriptors.
215 :return: True if deletion was successful, False otherwise
217 url = f
"{self._client.base_url}/submodel-descriptors"
222 response = self._session.delete(url, timeout=self.
_client.time_out)
223 _logger.debug(f
"Call REST API url '{response.url}'")
225 if response.status_code != STATUS_CODE_204:
226 log_response(response)
229 except requests.exceptions.RequestException
as e:
230 _logger.error(f
"Error call REST API: {e}")
237 """Returns the self-describing information of a network resource (ServiceDescription).
239 :return: self-describing information of a network resource
241 url = f
"{self._client.base_url}/description"
246 response = self._session.get(url, timeout=self.
_client.time_out)
247 _logger.debug(f
"Call REST API url '{response.url}'")
249 if response.status_code != STATUS_CODE_200:
250 log_response(response)
253 except requests.exceptions.RequestException
as e:
254 _logger.error(f
"Error call REST API: {e}")
257 content = response.content.decode(
"utf-8")
258 return json.loads(content)
Implementation of Submodel Registry related API calls.
bool delete_submodel_descriptor_by_id(self, str submodel_identifier)
Deletes a Submodel Descriptor, i.e.
dict|None get_all_submodel_descriptors(self, int limit=100, str cursor="")
Returns all Submodel Descriptors.
bool put_submodel_descriptor_by_id(self, str submodel_identifier, dict request_body)
Creates or updates an existing Submodel Descriptor.
bool delete_all_submodel_descriptors(self)
Deletes all Submodel Descriptors.
dict|None get_submodel_descriptor_by_id(self, str submodel_identifier)
Returns the Submodel Descriptor for the given submodel identifier.
dict|None post_submodel_descriptor(self, dict request_body)
Creates a new Submodel Descriptor, i.e.
dict|None get_self_description(self)
Returns the self-describing information of a network resource (ServiceDescription).
__init__(self, "AasHttpClient" client)
Initializes the SubmodelRegistryImplementation with the given client.