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