AAS HTTP Client Documentation
Loading...
Searching...
No Matches
shell_registry_implementation.py
Go to the documentation of this file.
1"""Shell Registry Implementation Module."""
2
3import json
4import logging
5from typing import TYPE_CHECKING
6
7import requests
8from pydantic import BaseModel
9
10if TYPE_CHECKING:
11 from aas_http_client.classes.client.aas_client import AasHttpClient
12
13from aas_http_client.utilities.constants import LogIntensity
14from aas_http_client.utilities.encoder import encode_base_64
16 STATUS_CODE_200,
17 STATUS_CODE_201,
18 STATUS_CODE_204,
19 STATUS_CODE_404,
20 log_response,
21)
22
23_logger = logging.getLogger(__name__)
24
25
27 """Implementation of Asset Administration Shell Registry related API calls."""
28
29 def __init__(self, client: "AasHttpClient"):
30 """Initializes the ShellRegistryImplementation with the given parameters."""
31 self._client = client
32
33 session = client.get_session()
34 if session is None:
35 raise ValueError(
36 "HTTP session is not initialized in the client. Call 'initialize()' method of the client before creating SubmodelRegistryImplementation instance."
37 )
38
39 self._session: requests.Session = session
40
41 # GET /shell-descriptors/{aasIdentifier}
42 def get_asset_administration_shell_descriptor_by_id(self, aas_identifier: str) -> dict | None:
43 """Returns a specific Asset Administration Shell Descriptor.
44
45 :param aas_identifier: The Asset Administration Shells unique id
46 :return: Asset Administration Shell Descriptor data or None if an error occurred
47 """
48 if not self._client.encoded_ids:
49 aas_identifier = encode_base_64(aas_identifier)
51 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}"
52
53 self._client.set_token()
54
55 try:
56 response = self._session.get(url, timeout=self._client.time_out)
57 _logger.debug(f"Call REST API url '{response.url}'")
58
59 if response.status_code == STATUS_CODE_404:
60 if self._client.get_log_intensity() == LogIntensity.HIGH:
61 _logger.warning(f"Asset Administration Shell Descriptor with id '{aas_identifier}' not found.")
62 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
63 _logger.debug(f"Asset Administration Shell Descriptor with id '{aas_identifier}' not found.")
64
65 return None
66
67 if response.status_code != STATUS_CODE_200:
68 log_response(response)
69 return None
70
71 except requests.exceptions.RequestException as e:
72 _logger.error(f"Error call REST API: {e}")
73 return None
74
75 content = response.content.decode("utf-8")
76 return json.loads(content)
77
78 # PUT /shell-descriptors/{aasIdentifier}
79 def put_asset_administration_shell_descriptor_by_id(self, aas_identifier: str, request_body: dict) -> bool:
80 """Creates or updates an existing Asset Administration Shell Descriptor.
81
82 :param aas_identifier: The Asset Administration Shells unique id
83 :param request_body: Asset Administration Shell Descriptor object
84 :return: Created or updated Asset Administration Shell Descriptor data or None if an error occurred
85 """
86 if not self._client.encoded_ids:
87 aas_identifier = encode_base_64(aas_identifier)
89 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}"
90
91 self._client.set_token()
92
93 try:
94 response = self._session.put(url, json=request_body, timeout=self._client.time_out)
95 _logger.debug(f"Call REST API url '{response.url}'")
96
97 if response.status_code == STATUS_CODE_404:
98 if self._client.get_log_intensity() == LogIntensity.HIGH:
99 _logger.warning(f"Asset Administration Shell Descriptor with id '{aas_identifier}' not found.")
100 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
101 _logger.debug(f"Asset Administration Shell Descriptor with id '{aas_identifier}' not found.")
102
103 return False
104
105 if response.status_code != STATUS_CODE_204:
106 log_response(response)
107 return False
108
109 except requests.exceptions.RequestException as e:
110 _logger.error(f"Error call REST API: {e}")
111 return False
112
113 return True
114
115 # DELETE /shell-descriptors/{aasIdentifier}
116 def delete_asset_administration_shell_descriptor_by_id(self, aas_identifier: str) -> bool:
117 """Deletes an Asset Administration Shell Descriptor, i.e. de-registers an AAS.
118
119 :param aas_identifier: The Asset Administration Shells unique id
120 :return: True if deletion was successful, False otherwise
121 """
122 if not self._client.encoded_ids:
123 aas_identifier = encode_base_64(aas_identifier)
125 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}"
126
127 self._client.set_token()
128
129 try:
130 response = self._session.delete(url, timeout=self._client.time_out)
131 _logger.debug(f"Call REST API url '{response.url}'")
132
133 if response.status_code == STATUS_CODE_404:
134 if self._client.get_log_intensity() == LogIntensity.HIGH:
135 _logger.warning(f"Asset Administration Shell Descriptor with id '{aas_identifier}' not found.")
136 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
137 _logger.debug(f"Asset Administration Shell Descriptor with id '{aas_identifier}' not found.")
138
139 return False
140
141 if response.status_code != STATUS_CODE_204:
142 log_response(response)
143 return False
144
145 except requests.exceptions.RequestException as e:
146 _logger.error(f"Error call REST API: {e}")
147 return False
148
149 return True
150
151 # GET /shell-descriptors/{aasIdentifier}/submodel-descriptors/{submodelIdentifier}
152 def get_submodel_descriptor_by_id_through_superpath(self, aas_identifier: str, submodel_identifier: str) -> dict | None:
153 """Returns a specific Submodel Descriptor.
154
155 :param aas_identifier: The Asset Administration Shells unique id
156 :param submodel_identifier: The Submodels unique id
157 :return: Submodel Descriptor data or None if an error occurred
158 """
159 if not self._client.encoded_ids:
160 aas_identifier = encode_base_64(aas_identifier)
161 submodel_identifier = encode_base_64(submodel_identifier)
162
163 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}/submodel-descriptors/{submodel_identifier}"
164
165 self._client.set_token()
166
167 try:
168 response = self._session.get(url, timeout=self._client.time_out)
169 _logger.debug(f"Call REST API url '{response.url}'")
170
171 if response.status_code == STATUS_CODE_404:
172 if self._client.get_log_intensity() == LogIntensity.HIGH:
173 _logger.warning(f"Submodel Descriptor with id '{submodel_identifier}' or submodel with id '{submodel_identifier}' not found.")
174 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
175 _logger.debug(f"Submodel Descriptor with id '{submodel_identifier}' or submodel with id '{submodel_identifier}' not found.")
176
177 return None
178
179 if response.status_code != STATUS_CODE_200:
180 log_response(response)
181 return None
182
183 except requests.exceptions.RequestException as e:
184 _logger.error(f"Error call REST API: {e}")
185 return None
186
187 content = response.content.decode("utf-8")
188 return json.loads(content)
189
190 # PUT /shell-descriptors/{aasIdentifier}/submodel-descriptors/{submodelIdentifier}
191 def put_submodel_descriptor_by_id_through_superpath(self, aas_identifier: str, submodel_identifier: str, request_body: dict) -> bool:
192 """Creates or updates an existing Submodel Descriptor.
193
194 :param aas_identifier: The Asset Administration Shells unique id
195 :param submodel_identifier: The Submodels unique id
196 :param request_body: Submodel Descriptor object
197 :return: True if creation or update was successful, False otherwise
198 """
199 if not self._client.encoded_ids:
200 aas_identifier = encode_base_64(aas_identifier)
201 submodel_identifier = encode_base_64(submodel_identifier)
202
203 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}/submodel-descriptors/{submodel_identifier}"
204
205 self._client.set_token()
206
207 try:
208 response = self._session.put(url, json=request_body, timeout=self._client.time_out)
209 _logger.debug(f"Call REST API url '{response.url}'")
210
211 if response.status_code == STATUS_CODE_404:
212 if self._client.get_log_intensity() == LogIntensity.HIGH:
213 _logger.warning(f"Submodel Descriptor with id '{submodel_identifier}' or submodel with id '{submodel_identifier}' not found.")
214 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
215 _logger.debug(f"Submodel Descriptor with id '{submodel_identifier}' or submodel with id '{submodel_identifier}' not found.")
216
217 return False
218
219 if response.status_code != STATUS_CODE_204:
220 log_response(response)
221 return False
222
223 except requests.exceptions.RequestException as e:
224 _logger.error(f"Error call REST API: {e}")
225 return False
226
227 return True
228
229 # DELETE /shell-descriptors/{aasIdentifier}/submodel-descriptors/{submodelIdentifier
230 def delete_submodel_descriptor_by_id_through_superpath(self, aas_identifier: str, submodel_identifier: str) -> bool:
231 """Deletes a Submodel Descriptor, i.e. de-registers a submodel.
232
233 :param aas_identifier: The Asset Administration Shells unique id
234 :param submodel_identifier: The Submodels unique id
235 :return: True if deletion was successful, False otherwise
236 """
237 if not self._client.encoded_ids:
238 aas_identifier = encode_base_64(aas_identifier)
239 submodel_identifier = encode_base_64(submodel_identifier)
240
241 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}/submodel-descriptors/{submodel_identifier}"
242
243 self._client.set_token()
244
245 try:
246 response = self._session.delete(url, timeout=self._client.time_out)
247 _logger.debug(f"Call REST API url '{response.url}'")
248
249 if response.status_code == STATUS_CODE_404:
250 if self._client.get_log_intensity() == LogIntensity.HIGH:
251 _logger.warning(f"Submodel Descriptor with id '{submodel_identifier}' or submodel with id '{submodel_identifier}' not found.")
252 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
253 _logger.debug(f"Submodel Descriptor with id '{submodel_identifier}' or submodel with id '{submodel_identifier}' not found.")
254
255 return False
256
257 if response.status_code != STATUS_CODE_204:
258 log_response(response)
259 return False
260
261 except requests.exceptions.RequestException as e:
262 _logger.error(f"Error call REST API: {e}")
263 return False
264
265 return True
266
267 # GET /shell-descriptors
269 self, limit: int = 100, cursor: str = "", asset_kind: str = "", asset_type: str = ""
270 ) -> dict | None:
271 """Returns all Asset Administration Shell Descriptors.
272
273 :param limit: Maximum number of Submodels to return
274 :param cursor: Cursor for pagination
275 :param asset_kind: The Asset's kind (Instance or Type). Available values : Instance, NotApplicable, Type
276 :param asset_type: The Asset's type (UTF8-BASE64-URL-encoded)
277 :return: Asset Administration Shell Descriptors data or None if an error occurred
278 """
279 url = f"{self._client.base_url}/shell-descriptors"
280
281 params: dict[str, str] = {}
282 if asset_kind:
283 params["asset_kind"] = asset_kind
284 if asset_type:
285 params["asset_type"] = asset_type
286 if limit:
287 params["limit"] = str(limit)
288 if cursor:
289 params["cursor"] = cursor
290
291 self._client.set_token()
292
293 try:
294 response = self._session.get(url, params=params, timeout=self._client.time_out)
295 _logger.debug(f"Call REST API url '{response.url}'")
296
297 if response.status_code != STATUS_CODE_200:
298 log_response(response)
299 return None
300
301 except requests.exceptions.RequestException as e:
302 _logger.error(f"Error call REST API: {e}")
303 return None
304
305 content = response.content.decode("utf-8")
306 return json.loads(content)
307
308 # POST /shell-descriptors
309 def post_asset_administration_shell_descriptor(self, request_body: dict) -> dict | None:
310 """Creates a new Asset Administration Shell Descriptor, i.e. registers an AAS.
311
312 :param request_body: Asset Administration Shell Descriptor object
313 :return: Created Asset Administration Shell Descriptor data or None if an error occurred
314 """
315 url = f"{self._client.base_url}/shell-descriptors"
316
317 self._client.set_token()
318
319 try:
320 response = self._session.post(url, json=request_body, timeout=self._client.time_out)
321 _logger.debug(f"Call REST API url '{response.url}'")
322
323 if response.status_code != STATUS_CODE_201:
324 log_response(response)
325 return None
326
327 except requests.exceptions.RequestException as e:
328 _logger.error(f"Error call REST API: {e}")
329 return None
330
331 content = response.content.decode("utf-8")
332 return json.loads(content)
333
334 # DELETE /shell-descriptors
336 """Deletes all Asset Administration Shell Descriptors.
337
338 :return: True if deletion was successful, False otherwise
339 """
340 url = f"{self._client.base_url}/shell-descriptors"
341
342 self._client.set_token()
343
344 try:
345 response = self._session.delete(url, timeout=self._client.time_out)
346 _logger.debug(f"Call REST API url '{response.url}'")
347
348 if response.status_code != STATUS_CODE_204:
349 log_response(response)
350 return False
351
352 except requests.exceptions.RequestException as e:
353 _logger.error(f"Error call REST API: {e}")
354 return False
355
356 return True
357
358 # GET /shell-descriptors/{aasIdentifier}/submodel-descriptors
359 def get_all_submodel_descriptors_through_superpath(self, aas_identifier: str) -> dict | None:
360 """Returns all Submodel Descriptors for a specific Asset Administration Shell.
361
362 :param aas_identifier: The Asset Administration Shells unique id
363 :return: Submodel Descriptors data or None if an error occurred
364 """
365 if not self._client.encoded_ids:
366 aas_identifier = encode_base_64(aas_identifier)
368 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}/submodel-descriptors"
369
370 self._client.set_token()
371
372 try:
373 response = self._session.get(url, timeout=self._client.time_out)
374 _logger.debug(f"Call REST API url '{response.url}'")
375
376 if response.status_code == STATUS_CODE_404:
377 if self._client.get_log_intensity() == LogIntensity.HIGH:
378 _logger.warning(f"Shell Descriptor with id '{aas_identifier}' not found.")
379 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
380 _logger.debug(f"Shell Descriptor with id '{aas_identifier}' not found.")
381
382 return None
383
384 if response.status_code != STATUS_CODE_200:
385 log_response(response)
386 return None
387
388 except requests.exceptions.RequestException as e:
389 _logger.error(f"Error call REST API: {e}")
390 return None
391
392 content = response.content.decode("utf-8")
393 return json.loads(content)
394
395 # POST /shell-descriptors/{aasIdentifier}/submodel-descriptors
396 def post_submodel_descriptor_through_superpath(self, aas_identifier: str, request_body: dict) -> dict | None:
397 """Creates a new Submodel Descriptor, i.e. registers a submodel.
398
399 :param aas_identifier: The Asset Administration Shells unique id
400 :param request_body: Asset Administration Shell Descriptor object
401 :return: Created Asset Administration Shell Descriptor data or None if an error occurred
402 """
403 if not self._client.encoded_ids:
404 aas_identifier = encode_base_64(aas_identifier)
406 url = f"{self._client.base_url}/shell-descriptors/{aas_identifier}/submodel-descriptors"
407
408 self._client.set_token()
409
410 try:
411 response = self._session.post(url, json=request_body, timeout=self._client.time_out)
412 _logger.debug(f"Call REST API url '{response.url}'")
413
414 if response.status_code == STATUS_CODE_404:
415 if self._client.get_log_intensity() == LogIntensity.HIGH:
416 _logger.warning(f"Shell Descriptor with id '{aas_identifier}' not found.")
417 elif self._client.get_log_intensity() == LogIntensity.STANDARD:
418 _logger.debug(f"Shell Descriptor with id '{aas_identifier}' not found.")
419
420 return None
421
422 if response.status_code != STATUS_CODE_201:
423 log_response(response)
424 return None
425
426 except requests.exceptions.RequestException as e:
427 _logger.error(f"Error call REST API: {e}")
428 return None
429
430 content = response.content.decode("utf-8")
431 return json.loads(content)
432
433 # POST /search
434 def search(self, request_body: dict) -> dict | None:
435 """Searches for Asset Administration Shell Descriptors based on the provided query.
436
437 :param request_body:query as a dictionary
438 :return: Search results as a dictionary or None if an error occurred
439 """
440 url = f"{self._client.base_url}/search"
441
442 self._client.set_token()
443
444 try:
445 response = self._session.post(url, json=request_body, timeout=self._client.time_out)
446 _logger.debug(f"Call REST API url '{response.url}'")
447
448 if response.status_code != STATUS_CODE_200:
449 log_response(response)
450 return None
451
452 except requests.exceptions.RequestException as e:
453 _logger.error(f"Error call REST API: {e}")
454 return None
455
456 content = response.content.decode("utf-8")
457 return json.loads(content)
458
459 # GET /description
460 def get_self_description(self) -> dict | None:
461 """Returns the self-describing information of a network resource (ServiceDescription).
462
463 :return: self-describing information of a network resource
464 """
465 url = f"{self._client.base_url}/description"
466
467 self._client.set_token()
468
469 try:
470 response = self._session.get(url, timeout=self._client.time_out)
471 _logger.debug(f"Call REST API url '{response.url}'")
472
473 if response.status_code != STATUS_CODE_200:
474 log_response(response)
475 return None
476
477 except requests.exceptions.RequestException as e:
478 _logger.error(f"Error call REST API: {e}")
479 return None
480
481 content = response.content.decode("utf-8")
482 return json.loads(content)
Implementation of Asset Administration Shell Registry related API calls.
dict|None get_submodel_descriptor_by_id_through_superpath(self, str aas_identifier, str submodel_identifier)
Returns a specific Submodel Descriptor.
dict|None post_submodel_descriptor_through_superpath(self, str aas_identifier, dict request_body)
Creates a new Submodel Descriptor, i.e.
dict|None post_asset_administration_shell_descriptor(self, dict request_body)
Creates a new Asset Administration Shell Descriptor, i.e.
dict|None search(self, dict request_body)
Searches for Asset Administration Shell Descriptors based on the provided query.
bool delete_asset_administration_shell_descriptor_by_id(self, str aas_identifier)
Deletes an Asset Administration Shell Descriptor, i.e.
bool put_asset_administration_shell_descriptor_by_id(self, str aas_identifier, dict request_body)
Creates or updates an existing Asset Administration Shell Descriptor.
bool delete_submodel_descriptor_by_id_through_superpath(self, str aas_identifier, str submodel_identifier)
Deletes a Submodel Descriptor, i.e.
dict|None get_all_submodel_descriptors_through_superpath(self, str aas_identifier)
Returns all Submodel Descriptors for a specific Asset Administration Shell.
dict|None get_self_description(self)
Returns the self-describing information of a network resource (ServiceDescription).
bool delete_all_asset_administration_shell_descriptors(self)
Deletes all Asset Administration Shell Descriptors.
bool put_submodel_descriptor_by_id_through_superpath(self, str aas_identifier, str submodel_identifier, dict request_body)
Creates or updates an existing Submodel Descriptor.
__init__(self, "AasHttpClient" client)
Initializes the ShellRegistryImplementation with the given parameters.
dict|None get_asset_administration_shell_descriptor_by_id(self, str aas_identifier)
Returns a specific Asset Administration Shell Descriptor.
dict|None get_all_asset_administration_shell_descriptors(self, int limit=100, str cursor="", str asset_kind="", str asset_type="")
Returns all Asset Administration Shell Descriptors.