# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT
"""
Implement the operation of the Description API.
Every service gives its description at ``/description``, relative to its base URL,
so the function takes the server of whichever service is to be described.
Every public function implements exactly one operation of the specification;
:py:func:`get_self_description_profiles` implements the same operation as
:py:func:`get_self_description`, but keeps the proprietary profiles. 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.
"""
import collections.abc
from typing import Final, FrozenSet, List
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 errors
from aas_client3_2 import http
def _build_get_self_description(base_url: str) -> http.Request:
"""Build the request of ``GetSelfDescription``."""
return _json.build_request("GET", http.build_url(base_url, "/description"))
#: Identifiers of the profiles which Part 2 defines, as aas-core3.2 knows them
_KNOWN_PROFILES: Final[FrozenSet[str]] = frozenset(
literal.value for literal in aas_types.ServiceSpecificationProfileEnum
)
def _drop_the_unknown(jsonable: aas_jsonization.MutableJsonable) -> None:
"""
Drop the proprietary profiles and properties from the parsed description.
The OpenAPI of Part 2 lets a server include proprietary profiles and
properties in its description, and says that the clients "should be designed
to ignore unknown profiles". aas-core3.2 de-serializes the profiles as
the enumeration ``ServiceSpecificationProfileEnum`` of the prose, and refuses
any other property. Hence we drop what it does not know, as the OpenAPI asks.
Anything which is not a description is left as it is, so that
the de-serialization reports it.
"""
if not isinstance(jsonable, collections.abc.MutableMapping):
return
for key in [key for key in jsonable if key != "profiles"]:
del jsonable[key]
# NOTE (mristin):
# The parsed JSON is made of lists, so a JSON array is a mutable sequence.
profiles = jsonable.get("profiles", None)
if not isinstance(profiles, collections.abc.MutableSequence):
return
profiles[:] = [
profile
for profile in profiles
if not isinstance(profile, str) or profile in _KNOWN_PROFILES
]
def _parse_get_self_description(
response: http.Response,
) -> aas_types.ServiceDescription:
"""Parse the response of ``GetSelfDescription``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(
response,
aas_jsonization.service_description_from_jsonable,
_drop_the_unknown,
)
[docs]
def get_self_description(server: http.Server) -> aas_types.ServiceDescription:
"""
Get the description of the service, which lists the profiles it implements.
The profiles are the literals of
:py:class:`aas_core3_2.types.ServiceSpecificationProfileEnum`, such as
``https://admin-shell.io/aas/API/3/2/SubmodelRepositoryServiceSpecification/SSP-001``.
A server may also list proprietary profiles and properties; the client ignores
them, as the OpenAPI of Part 2 asks the clients to. Use
:py:func:`get_self_description_profiles` to get the proprietary profiles too.
:param server: the service to be described
:returns: the description of the service
:implements GetSelfDescription: ``GET /description``
"""
request = _build_get_self_description(server.base_url)
response = http.send(server.transport, request)
return _parse_get_self_description(response)
def _profiles_from_jsonable(jsonable: aas_jsonization.Jsonable) -> List[str]:
"""
De-serialize the profiles of a description as they are, including the unknown.
As the OpenAPI of Part 2 defines it, the description must be an object with
at least one profile, and every profile a string. Other properties are
ignored, since the OpenAPI allows the proprietary ones.
:param jsonable: parsed JSON
:returns: the profiles in the order of the server
:raises:
:py:class:`aas_core3_2.jsonization.DeserializationException` if
:paramref:`jsonable` is not a description
"""
if not isinstance(jsonable, collections.abc.Mapping):
raise aas_jsonization.DeserializationException(
f"Expected a JSON object of a description, but got {type(jsonable)}"
)
if "profiles" not in jsonable:
raise aas_jsonization.DeserializationException(
"The required property 'profiles' is missing"
)
array_like = aas_common.try_to_cast_to_array_like(jsonable["profiles"])
if array_like is None:
raise aas_jsonization.DeserializationException(
f"Expected the profiles to be a JSON array, "
f"but got {type(jsonable['profiles'])}"
)
profiles: List[str] = []
for i, item in enumerate(array_like):
if not isinstance(item, str):
raise aas_jsonization.DeserializationException(
f"Expected the profile at index {i} to be a string, "
f"but got {type(item)}"
)
profiles.append(item)
if len(profiles) == 0:
raise aas_jsonization.DeserializationException(
"Expected at least one profile, but got none"
)
return profiles
def _parse_get_self_description_profiles(response: http.Response) -> List[str]:
"""Parse the response of ``GetSelfDescription`` into all the profiles."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, _profiles_from_jsonable)
[docs]
def get_self_description_profiles(server: http.Server) -> List[str]:
"""
Get all the profiles which the service lists in its description, as strings.
In contrast to :py:func:`get_self_description`, the proprietary profiles,
which Part 2 allows, are kept, such as ``https://basyx.org/...`` of
Eclipse BaSyx. The proprietary properties of the description are ignored.
:param server: the service to be described
:returns: the profiles in the order of the server
:implements GetSelfDescription: ``GET /description``
"""
request = _build_get_self_description(server.base_url)
response = http.send(server.transport, request)
return _parse_get_self_description_profiles(response)