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
23_logger = logging.getLogger(__name__)
27 """Implementation of Submodel Registry related API calls."""
29 def __init__(self, client:
"AasHttpClient"):
30 """Initializes the SubmodelRegistryImplementation with the given client."""
33 session = client.get_session()
36 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating SubmodelRegistryImplementation instance."
39 self._session: requests.Session = session
43 """Returns the Submodel Descriptor for the given submodel identifier.
45 :param submodel_identifier: The unique identifier of the Submodel Descriptor
46 :return: Submodel Descriptor data 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}/submodel-descriptors/{submodel_identifier}"
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:
60 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
61 _logger.debug(response.text)
64 if response.status_code != STATUS_CODE_200:
65 log_response(response)
68 except requests.exceptions.RequestException
as e:
69 _logger.error(f
"Error call REST API: {e}")
72 content = response.content.decode(
"utf-8")
73 return json.loads(content)
77 """Creates or updates an existing Submodel Descriptor.
79 :param submodel_identifier: The unique identifier of the Submodel Descriptor
80 :param request_body: Submodel Descriptor object
81 :return: Updated Submodel Descriptor data or None if an error occurred
83 if not self.
_client.encoded_ids:
84 submodel_identifier = encode_base_64(submodel_identifier)
86 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_identifier}"
91 response = self._session.put(url, json=request_body, timeout=self.
_client.time_out)
92 _logger.debug(f
"Call REST API url '{response.url}'")
94 if response.status_code == STATUS_CODE_404:
95 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
96 _logger.debug(response.text)
99 if response.status_code != STATUS_CODE_204:
100 log_response(response)
103 except requests.exceptions.RequestException
as e:
104 _logger.error(f
"Error call REST API: {e}")
111 """Deletes a Submodel Descriptor, i.e. de-registers a submodel.
113 :param submodel_identifier: The unique identifier of the Submodel Descriptor
114 :return: True if deletion was successful, False otherwise
116 if not self.
_client.encoded_ids:
117 submodel_identifier = encode_base_64(submodel_identifier)
119 url = f
"{self._client.base_url}/submodel-descriptors/{submodel_identifier}"
124 response = self._session.delete(url, timeout=self.
_client.time_out)
125 _logger.debug(f
"Call REST API url '{response.url}'")
127 if response.status_code == STATUS_CODE_404:
128 _logger.warning(f
"Submodel Descriptor with id '{submodel_identifier}' not found.")
129 _logger.debug(response.text)
132 if response.status_code != STATUS_CODE_204:
133 log_response(response)
136 except requests.exceptions.RequestException
as e:
137 _logger.error(f
"Error call REST API: {e}")
144 """Returns all Submodel Descriptors.
146 :param limit: The maximum number of elements in the response array
147 :param cursor: A server-generated identifier retrieved from pagingMetadata that specifies from which position the result listing should continue
148 :return: Submodel Descriptors data or None if an error occurred
150 url = f
"{self._client.base_url}/submodel-descriptors"
152 params: dict[str, str] = {}
154 params[
"limit"] = str(limit)
156 params[
"cursor"] = cursor
161 response = self._session.get(url, params=params, timeout=self.
_client.time_out)
162 _logger.debug(f
"Call REST API url '{response.url}'")
164 if response.status_code != STATUS_CODE_200:
165 log_response(response)
168 except requests.exceptions.RequestException
as e:
169 _logger.error(f
"Error call REST API: {e}")
172 content = response.content.decode(
"utf-8")
173 return json.loads(content)
177 """Creates a new Submodel Descriptor, i.e. registers a submodel.
179 :param request_body: Submodel Descriptor object
180 :return: Created Submodel Descriptor data or None if an error occurred
182 url = f
"{self._client.base_url}/submodel-descriptors"
187 response = self._session.post(url, json=request_body, timeout=self.
_client.time_out)
188 _logger.debug(f
"Call REST API url '{response.url}'")
190 if response.status_code != STATUS_CODE_201:
191 log_response(response)
194 except requests.exceptions.RequestException
as e:
195 _logger.error(f
"Error call REST API: {e}")
198 content = response.content.decode(
"utf-8")
199 return json.loads(content)
203 """Deletes all Submodel Descriptors.
205 :return: True if deletion was successful, False otherwise
207 url = f
"{self._client.base_url}/submodel-descriptors"
212 response = self._session.delete(url, timeout=self.
_client.time_out)
213 _logger.debug(f
"Call REST API url '{response.url}'")
215 if response.status_code != STATUS_CODE_204:
216 log_response(response)
219 except requests.exceptions.RequestException
as e:
220 _logger.error(f
"Error call REST API: {e}")
227 """Returns the self-describing information of a network resource (ServiceDescription).
229 :return: self-describing information of a network resource
231 url = f
"{self._client.base_url}/description"
236 response = self._session.get(url, timeout=self.
_client.time_out)
237 _logger.debug(f
"Call REST API url '{response.url}'")
239 if response.status_code != STATUS_CODE_200:
240 log_response(response)
243 except requests.exceptions.RequestException
as e:
244 _logger.error(f
"Error call REST API: {e}")
247 content = response.content.decode(
"utf-8")
248 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.