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