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.")
66 _logger.debug(response.text)
69 if response.status_code != STATUS_CODE_200:
70 log_response(response)
73 except requests.exceptions.RequestException
as e:
74 _logger.error(f
"Error call REST API: {e}")
77 content = response.content.decode(
"utf-8")
78 return json.loads(content)
82 """Creates or updates an existing Submodel Descriptor.
84 :param submodel_identifier: The unique identifier of the Submodel Descriptor
85 :param request_body: Submodel Descriptor object
86 :return: Updated Submodel Descriptor data or None if an error occurred
88 if not self.
_client.encoded_ids:
89 submodel_identifier = encode_base_64(submodel_identifier)
91 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_identifier}"
96 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
97 _logger.debug(f
"Call REST API url '{response.url}'")
99 if response.status_code == STATUS_CODE_404:
100 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
101 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
102 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
103 _logger.debug(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
105 _logger.debug(response.text)
108 if response.status_code != STATUS_CODE_204:
109 log_response(response)
112 except requests.exceptions.RequestException
as e:
113 _logger.error(f
"Error call REST API: {e}")
120 """Deletes a Submodel Descriptor, i.e. de-registers a submodel.
122 :param submodel_identifier: The unique identifier of the Submodel Descriptor
123 :return: True if deletion was successful, False otherwise
125 if not self.
_client.encoded_ids:
126 submodel_identifier = encode_base_64(submodel_identifier)
128 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_identifier}"
133 response = self._session.delete(url, timeout=self.
_client.time_out)
134 _logger.debug(f
"Call REST API url '{response.url}'")
136 if response.status_code == STATUS_CODE_404:
137 if self.
_client.get_log_intensity() == LogIntensity.HIGH:
138 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
139 elif self.
_client.get_log_intensity() == LogIntensity.STANDARD:
140 _logger.debug(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
142 _logger.debug(response.text)
145 if response.status_code != STATUS_CODE_204:
146 log_response(response)
149 except requests.exceptions.RequestException
as e:
150 _logger.error(f
"Error call REST API: {e}")
157 """Returns all Submodel Descriptors.
159 :param limit: The maximum number of elements in the response array
160 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
161 :return: Submodel Descriptors data or None if an error occurred
163 url = f
"{self._client.base_url}/submodel-descriptors"
165 params: dict[str, str] = {}
167 params[
"limit"] = str(limit)
169 params[
"cursor"] = cursor
174 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
175 _logger.debug(f
"Call REST API url '{response.url}'")
177 if response.status_code != STATUS_CODE_200:
178 log_response(response)
181 except requests.exceptions.RequestException
as e:
182 _logger.error(f
"Error call REST API: {e}")
185 content = response.content.decode(
"utf-8")
186 return json.loads(content)
190 """Creates a new Submodel Descriptor, i.e. registers a submodel.
192 :param request_body: Submodel Descriptor object
193 :return: Created Submodel Descriptor data or None if an error occurred
195 url = f
"{self._client.base_url}/submodel-descriptors"
200 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
201 _logger.debug(f
"Call REST API url '{response.url}'")
203 if response.status_code != STATUS_CODE_201:
204 log_response(response)
207 except requests.exceptions.RequestException
as e:
208 _logger.error(f
"Error call REST API: {e}")
211 content = response.content.decode(
"utf-8")
212 return json.loads(content)
216 """Deletes all Submodel Descriptors.
218 :return: True if deletion was successful, False otherwise
220 url = f
"{self._client.base_url}/submodel-descriptors"
225 response = self._session.delete(url, timeout=self.
_client.time_out)
226 _logger.debug(f
"Call REST API url '{response.url}'")
228 if response.status_code != STATUS_CODE_204:
229 log_response(response)
232 except requests.exceptions.RequestException
as e:
233 _logger.error(f
"Error call REST API: {e}")
240 """Returns the self-describing information of a network resource (ServiceDescription).
242 :return: self-describing information of a network resource
244 url = f
"{self._client.base_url}/description"
249 response = self._session.get(url, timeout=self.
_client.time_out)
250 _logger.debug(f
"Call REST API url '{response.url}'")
252 if response.status_code != STATUS_CODE_200:
253 log_response(response)
256 except requests.exceptions.RequestException
as e:
257 _logger.error(f
"Error call REST API: {e}")
260 content = response.content.decode(
"utf-8")
261 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.