# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT
"""
Implement the operations of the Asset Administration Shell Basic Discovery API.
The discovery links the identifiers of the Asset Administration Shells to the IDs
of their assets: the global asset ID, given as the specific asset ID with the name
``globalAssetId`` (Constraint AASd-116), and the other specific asset IDs.
Every public function implements exactly one operation of the specification. The
private functions ``_build_*`` and ``_parse_*`` build the request and parse
the response of the operation. They do no I/O, and constitute a semi-public
interface for the testing client, which is released in lockstep with this client.
"""
from typing import List, Optional, Sequence
import aas_core3_2.common as aas_common
import aas_core3_2.jsonization as aas_jsonization
import aas_core3_2.types as aas_types
from aas_client3_2 import _json
from aas_client3_2 import _query
from aas_client3_2 import errors
from aas_client3_2 import http
from aas_client3_2 import query_results
def _asset_links_path(aas_identifier: str) -> str:
"""Build the path of the asset links of the shell with the given identifier."""
return "/lookup/shells/" + http.encode_base64url(aas_identifier)
def _specific_asset_id_at(
index: int, jsonable: aas_jsonization.Jsonable
) -> aas_types.SpecificAssetID:
"""De-serialize the specific asset ID at :paramref:`index` of an array."""
try:
return aas_jsonization.specific_asset_id_from_jsonable(jsonable)
except aas_jsonization.DeserializationException as exception:
raise aas_jsonization.DeserializationException(
f"Invalid specific asset ID at index {index}: {exception}"
) from exception
def _specific_asset_ids_from_jsonable(
jsonable: aas_jsonization.Jsonable,
) -> List[aas_types.SpecificAssetID]:
"""
De-serialize a bare JSON array of specific asset IDs.
:raises:
:py:class:`aas_core3_2.jsonization.DeserializationException` if
:paramref:`jsonable` is not an array of specific asset IDs
"""
array_like = aas_common.try_to_cast_to_array_like(jsonable)
if array_like is None:
raise aas_jsonization.DeserializationException(
f"Expected a JSON array of specific asset IDs, but got {type(jsonable)}"
)
return [_specific_asset_id_at(i, item) for i, item in enumerate(array_like)]
def _build_get_all_asset_administration_shell_ids_by_asset_link(
base_url: str,
*,
asset_ids: Optional[Sequence[aas_types.SpecificAssetID]] = None,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``GetAllAssetAdministrationShellIdsByAssetLink``."""
query: _query.Query = []
# NOTE (mristin):
# As for the shells, the specification repeats the parameter for every
# specific asset ID, serialized to JSON and encoded in base64url.
if asset_ids is not None:
query.extend(
("assetIds", _query.encode_instance(asset_id)) for asset_id in asset_ids
)
query.extend(_query.page(limit, cursor))
return _json.build_request("GET", http.build_url(base_url, "/lookup/shells", query))
def _parse_get_all_asset_administration_shell_ids_by_asset_link(
response: http.Response,
) -> query_results.IdsResult:
"""Parse the response of ``GetAllAssetAdministrationShellIdsByAssetLink``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, _json.ids_result_from_jsonable)
[docs]
def get_all_asset_administration_shell_ids_by_asset_link(
server: http.Server,
*,
asset_ids: Optional[Sequence[aas_types.SpecificAssetID]] = None,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> query_results.IdsResult:
"""
List a page of the identifiers of the shells linked to all the given asset IDs.
Give the global asset ID as a specific asset ID with the name ``globalAssetId``.
:param server: where to send the request
:param asset_ids: if set, only the shells linked to all these asset IDs
:param limit: maximum number of the identifiers in the page
:param cursor: where to continue the listing from
:returns: a page of the identifiers of the shells
:implements GetAllAssetAdministrationShellIdsByAssetLink: ``GET /lookup/shells``
"""
request = _build_get_all_asset_administration_shell_ids_by_asset_link(
server.base_url, asset_ids=asset_ids, limit=limit, cursor=cursor
)
response = http.send(server.transport, request)
return _parse_get_all_asset_administration_shell_ids_by_asset_link(response)
def _build_search_all_asset_administration_shell_ids_by_asset_link(
base_url: str,
asset_links: Sequence[aas_types.AssetLink],
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``SearchAllAssetAdministrationShellIdsByAssetLink``."""
return _json.build_raw_request(
"POST",
http.build_url(
base_url, "/lookup/shellsByAssetLink", _query.page(limit, cursor)
),
_json.dump(
[aas_jsonization.to_jsonable(asset_link) for asset_link in asset_links]
),
)
def _parse_search_all_asset_administration_shell_ids_by_asset_link(
response: http.Response,
) -> query_results.IdsResult:
"""Parse the response of ``SearchAllAssetAdministrationShellIdsByAssetLink``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, _json.ids_result_from_jsonable)
[docs]
def search_all_asset_administration_shell_ids_by_asset_link(
server: http.Server,
asset_links: Sequence[aas_types.AssetLink],
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> query_results.IdsResult:
"""
List a page of the identifiers of the shells linked to all :paramref:`asset_links`.
This is the same search as
:py:func:`get_all_asset_administration_shell_ids_by_asset_link`, but with
the asset links in the body instead of the query, so that also many links fit.
Give the global asset ID as an asset link with the name ``globalAssetId``.
:param server: where to send the request
:param asset_links: the asset links which all must match
:param limit: maximum number of the identifiers in the page
:param cursor: where to continue the listing from
:returns: a page of the identifiers of the shells
:implements SearchAllAssetAdministrationShellIdsByAssetLink: ``POST /lookup/shellsByAssetLink``
"""
request = _build_search_all_asset_administration_shell_ids_by_asset_link(
server.base_url, asset_links, limit=limit, cursor=cursor
)
response = http.send(server.transport, request)
return _parse_search_all_asset_administration_shell_ids_by_asset_link(response)
def _build_get_all_asset_links_by_id(
base_url: str, aas_identifier: str
) -> http.Request:
"""Build the request of ``GetAllAssetLinksById``."""
return _json.build_request(
"GET", http.build_url(base_url, _asset_links_path(aas_identifier))
)
def _parse_get_all_asset_links_by_id(
response: http.Response,
) -> List[aas_types.SpecificAssetID]:
"""Parse the response of ``GetAllAssetLinksById``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, _specific_asset_ids_from_jsonable)
[docs]
def get_all_asset_links_by_id(
server: http.Server, aas_identifier: str
) -> List[aas_types.SpecificAssetID]:
"""
Get the asset IDs linked to the shell with the given identifier.
The global asset ID is given as the specific asset ID with the name
``globalAssetId``.
:param server: where to send the request
:param aas_identifier: identifier of the Asset Administration Shell
:returns: the linked asset IDs
:implements GetAllAssetLinksById: ``GET /lookup/shells/{aasIdentifier}``
"""
request = _build_get_all_asset_links_by_id(server.base_url, aas_identifier)
response = http.send(server.transport, request)
return _parse_get_all_asset_links_by_id(response)
def _build_post_all_asset_links_by_id(
base_url: str,
aas_identifier: str,
specific_asset_ids: Sequence[aas_types.SpecificAssetID],
) -> http.Request:
"""Build the request of ``PostAllAssetLinksById``."""
return _json.build_raw_request(
"POST",
http.build_url(base_url, _asset_links_path(aas_identifier)),
_json.dump(
[
aas_jsonization.to_jsonable(specific_asset_id)
for specific_asset_id in specific_asset_ids
]
),
)
def _parse_post_all_asset_links_by_id(
response: http.Response,
) -> List[aas_types.SpecificAssetID]:
"""Parse the response of ``PostAllAssetLinksById``."""
errors.raise_for_unexpected_status(response, {201})
return _json.parse(response, _specific_asset_ids_from_jsonable)
[docs]
def post_all_asset_links_by_id(
server: http.Server,
aas_identifier: str,
specific_asset_ids: Sequence[aas_types.SpecificAssetID],
) -> List[aas_types.SpecificAssetID]:
"""
Create or replace all the asset IDs linked to the shell with the given identifier.
Give the global asset ID as the specific asset ID with the name
``globalAssetId``, and without ``externalSubjectId``, which Part 2 forbids for
it. The server ignores the semantic IDs of the specific asset IDs.
:param server: where to send the request
:param aas_identifier: identifier of the Asset Administration Shell
:param specific_asset_ids: the asset IDs to be linked
:returns: the linked asset IDs, as stored by the server
:implements PostAllAssetLinksById: ``POST /lookup/shells/{aasIdentifier}``
"""
request = _build_post_all_asset_links_by_id(
server.base_url, aas_identifier, specific_asset_ids
)
response = http.send(server.transport, request)
return _parse_post_all_asset_links_by_id(response)
def _build_delete_all_asset_links_by_id(
base_url: str, aas_identifier: str
) -> http.Request:
"""Build the request of ``DeleteAllAssetLinksById``."""
return _json.build_request(
"DELETE", http.build_url(base_url, _asset_links_path(aas_identifier))
)
def _parse_delete_all_asset_links_by_id(response: http.Response) -> None:
"""Parse the response of ``DeleteAllAssetLinksById``."""
errors.raise_for_unexpected_status(response, {204})
[docs]
def delete_all_asset_links_by_id(server: http.Server, aas_identifier: str) -> None:
"""
Delete all the asset IDs linked to the shell with the given identifier.
:param server: where to send the request
:param aas_identifier: identifier of the Asset Administration Shell
:implements DeleteAllAssetLinksById: ``DELETE /lookup/shells/{aasIdentifier}``
"""
request = _build_delete_all_asset_links_by_id(server.base_url, aas_identifier)
response = http.send(server.transport, request)
_parse_delete_all_asset_links_by_id(response)