Source code for aas_client3_2.concept_description_repository

# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT

"""
Implement the operations of the Concept Description Repository API.

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.
"""

import datetime
from typing import Optional

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 _query_language
from aas_client3_2 import _signed
from aas_client3_2 import errors
from aas_client3_2 import http
from aas_client3_2 import query_results


def _concept_description_path(cd_identifier: str) -> str:
    """Build the path of the concept description with the given identifier."""
    return "/concept-descriptions/" + http.encode_base64url(cd_identifier)


def _query_of_concept_descriptions(
    id_short: Optional[str],
    is_case_of: Optional[aas_types.Reference],
    data_specification_ref: Optional[aas_types.Reference],
    created_from: Optional[datetime.datetime],
    updated_from: Optional[datetime.datetime],
    limit: Optional[int],
    cursor: Optional[str],
) -> _query.Query:
    """Build the query to filter and page the concept descriptions."""
    query: _query.Query = []

    if id_short is not None:
        query.append(("idShort", id_short))

    # NOTE (mristin):
    # The specification serializes the references in the query to JSON, and
    # encodes them in base64url (see the section "Query Parameters" of Part 2).
    if is_case_of is not None:
        query.append(("isCaseOf", _query.encode_instance(is_case_of)))

    if data_specification_ref is not None:
        query.append(
            ("dataSpecificationRef", _query.encode_instance(data_specification_ref))
        )

    query.extend(_query.time_range(created_from, updated_from))
    query.extend(_query.page(limit, cursor))

    return query


def _build_get_all_concept_descriptions(
    base_url: str,
    *,
    id_short: Optional[str] = None,
    is_case_of: Optional[aas_types.Reference] = None,
    data_specification_ref: Optional[aas_types.Reference] = None,
    created_from: Optional[datetime.datetime] = None,
    updated_from: Optional[datetime.datetime] = None,
    limit: Optional[int] = None,
    cursor: Optional[str] = None,
) -> http.Request:
    """Build the request of ``GetAllConceptDescriptions``."""
    return _json.build_request(
        "GET",
        http.build_url(
            base_url,
            "/concept-descriptions",
            _query_of_concept_descriptions(
                id_short,
                is_case_of,
                data_specification_ref,
                created_from,
                updated_from,
                limit,
                cursor,
            ),
        ),
    )


def _parse_get_all_concept_descriptions(
    response: http.Response,
) -> aas_types.GetConceptDescriptionsResult:
    """Parse the response of ``GetAllConceptDescriptions``."""
    errors.raise_for_unexpected_status(response, {200})
    return _json.parse(
        response, aas_jsonization.get_concept_descriptions_result_from_jsonable
    )


[docs] def get_all_concept_descriptions( server: http.Server, *, id_short: Optional[str] = None, is_case_of: Optional[aas_types.Reference] = None, data_specification_ref: Optional[aas_types.Reference] = None, created_from: Optional[datetime.datetime] = None, updated_from: Optional[datetime.datetime] = None, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> aas_types.GetConceptDescriptionsResult: """ List a page of the concept descriptions in the repository. The operation also implements ``GetAllConceptDescriptionsByIdShort``, ``GetAllConceptDescriptionsByIsCaseOf`` and ``GetAllConceptDescriptionsByDataSpecificationReference`` of Part 2, which differ only in the filter. The filters are combined with "and". :param server: where to send the request :param id_short: if set, only the concept descriptions with this ID-short :param is_case_of: if set, only the concept descriptions with this reference among their ``isCaseOf`` :param data_specification_ref: if set, only the concept descriptions with an embedded data specification of this reference :param created_from: if set, only the concept descriptions created at or after this time :param updated_from: if set, only the concept descriptions updated at or after this time :param limit: maximum number of the concept descriptions in the page :param cursor: where to continue the listing from :returns: a page of the concept descriptions :raises: :py:class:`ValueError` before sending anything if a time has no time zone :implements GetAllConceptDescriptions: ``GET /concept-descriptions`` """ request = _build_get_all_concept_descriptions( server.base_url, id_short=id_short, is_case_of=is_case_of, data_specification_ref=data_specification_ref, created_from=created_from, updated_from=updated_from, limit=limit, cursor=cursor, ) response = http.send(server.transport, request) return _parse_get_all_concept_descriptions(response)
def _build_post_concept_description( base_url: str, concept_description: aas_types.ConceptDescription ) -> http.Request: """Build the request of ``PostConceptDescription``.""" return _json.build_request( "POST", http.build_url(base_url, "/concept-descriptions"), concept_description, ) def _parse_post_concept_description( response: http.Response, ) -> aas_types.ConceptDescription: """Parse the response of ``PostConceptDescription``.""" errors.raise_for_unexpected_status(response, {201}) return _json.parse(response, aas_jsonization.concept_description_from_jsonable)
[docs] def post_concept_description( server: http.Server, concept_description: aas_types.ConceptDescription ) -> aas_types.ConceptDescription: """ Create a new concept description in the repository. :param server: where to send the request :param concept_description: the concept description to be created :returns: the created concept description, as stored by the server :implements PostConceptDescription: ``POST /concept-descriptions`` """ request = _build_post_concept_description(server.base_url, concept_description) response = http.send(server.transport, request) return _parse_post_concept_description(response)
def _build_get_concept_description_by_id( base_url: str, cd_identifier: str ) -> http.Request: """Build the request of ``GetConceptDescriptionById``.""" return _json.build_request( "GET", http.build_url(base_url, _concept_description_path(cd_identifier)) ) def _parse_get_concept_description_by_id( response: http.Response, ) -> aas_types.ConceptDescription: """Parse the response of ``GetConceptDescriptionById``.""" errors.raise_for_unexpected_status(response, {200}) return _json.parse(response, aas_jsonization.concept_description_from_jsonable)
[docs] def get_concept_description_by_id( server: http.Server, cd_identifier: str ) -> aas_types.ConceptDescription: """ Get the concept description with the given identifier. :param server: where to send the request :param cd_identifier: identifier of the concept description :returns: the concept description :implements GetConceptDescriptionById: ``GET /concept-descriptions/{cdIdentifier}`` """ request = _build_get_concept_description_by_id(server.base_url, cd_identifier) response = http.send(server.transport, request) return _parse_get_concept_description_by_id(response)
def _build_put_concept_description_by_id( base_url: str, cd_identifier: str, concept_description: aas_types.ConceptDescription, ) -> http.Request: """Build the request of ``PutConceptDescriptionById``.""" return _json.build_request( "PUT", http.build_url(base_url, _concept_description_path(cd_identifier)), concept_description, ) def _parse_put_concept_description_by_id(response: http.Response) -> None: """ Parse the response of ``PutConceptDescriptionById``. The server responds with 201 and the concept description if it created it, or with 204 if it updated an existing one. We do not parse the concept description, since it is the one which the users sent. """ errors.raise_for_unexpected_status(response, {201, 204})
[docs] def put_concept_description_by_id( server: http.Server, cd_identifier: str, concept_description: aas_types.ConceptDescription, ) -> None: """ Create or replace the concept description with the given identifier. :param server: where to send the request :param cd_identifier: identifier of the concept description :param concept_description: the concept description to be stored :implements PutConceptDescriptionById: ``PUT /concept-descriptions/{cdIdentifier}`` """ request = _build_put_concept_description_by_id( server.base_url, cd_identifier, concept_description ) response = http.send(server.transport, request) _parse_put_concept_description_by_id(response)
def _build_delete_concept_description_by_id( base_url: str, cd_identifier: str ) -> http.Request: """Build the request of ``DeleteConceptDescriptionById``.""" return _json.build_request( "DELETE", http.build_url(base_url, _concept_description_path(cd_identifier)) ) def _parse_delete_concept_description_by_id(response: http.Response) -> None: """Parse the response of ``DeleteConceptDescriptionById``.""" errors.raise_for_unexpected_status(response, {204})
[docs] def delete_concept_description_by_id(server: http.Server, cd_identifier: str) -> None: """ Delete the concept description with the given identifier. :param server: where to send the request :param cd_identifier: identifier of the concept description :implements DeleteConceptDescriptionById: ``DELETE /concept-descriptions/{cdIdentifier}`` """ request = _build_delete_concept_description_by_id(server.base_url, cd_identifier) response = http.send(server.transport, request) _parse_delete_concept_description_by_id(response)
def _build_get_concept_description_by_id_signed( base_url: str, cd_identifier: str ) -> http.Request: """Build the request of ``GetConceptDescriptionByIdSigned``.""" return _signed.build_request( http.build_url(base_url, _concept_description_path(cd_identifier) + "/$signed") ) def _parse_get_concept_description_by_id_signed(response: http.Response) -> str: """Parse the response of ``GetConceptDescriptionByIdSigned``.""" return _signed.parse(response)
[docs] def get_concept_description_by_id_signed( server: http.Server, cd_identifier: str ) -> str: """ Get the concept description with the given identifier, signed by the server. The concept description is the payload of a JSON Web Signature (JWS, RFC 7515), signed with RS256, with the certificate chain of the signing key in the header ``x5c``. The client does not verify the signature, since only the users know which certificates they trust; verifying against the certificate in the JWS itself would prove nothing. Verify it with a JWS library, and parse the verified payload with :py:func:`aas_core3_2.jsonization.concept_description_from_jsonable`. :param server: where to send the request :param cd_identifier: identifier of the concept description :returns: the JWS in the compact serialization :implements GetConceptDescriptionByIdSigned: ``GET /concept-descriptions/{cdIdentifier}/$signed`` """ request = _build_get_concept_description_by_id_signed( server.base_url, cd_identifier ) response = http.send(server.transport, request) return _parse_get_concept_description_by_id_signed(response)
def _build_get_all_concept_descriptions_recent_changes( base_url: str, *, created_from: Optional[datetime.datetime] = None, updated_from: Optional[datetime.datetime] = None, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> http.Request: """Build the request of ``GetAllConceptDescriptionsRecentChanges``.""" query: _query.Query = [] query.extend(_query.time_range(created_from, updated_from)) query.extend(_query.page(limit, cursor)) return _json.build_request( "GET", http.build_url(base_url, "/concept-descriptions/$recent-changes", query), ) def _parse_get_all_concept_descriptions_recent_changes( response: http.Response, ) -> aas_types.GetAllConceptDescriptionsRecentChangesResult: """Parse the response of ``GetAllConceptDescriptionsRecentChanges``.""" errors.raise_for_unexpected_status(response, {200}) return _json.parse( response, aas_jsonization.get_all_concept_descriptions_recent_changes_result_from_jsonable, )
[docs] def get_all_concept_descriptions_recent_changes( server: http.Server, *, created_from: Optional[datetime.datetime] = None, updated_from: Optional[datetime.datetime] = None, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> aas_types.GetAllConceptDescriptionsRecentChangesResult: """ List a page of the changes of the concept descriptions in the repository. A change gives only the identifier of a concept description, and when it was created and last updated, so that also a large number of changes can be listed; get the concept descriptions themselves with :py:func:`get_concept_description_by_id`. The filters :paramref:`created_from` and :paramref:`updated_from` are combined with "or": a concept description is listed if it was created at or after :paramref:`created_from`, *or* updated at or after :paramref:`updated_from`. The operation is experimental in Part 2 V3.2. The times are ``createdAt`` and ``updatedAt`` of the administrative information as the users wrote them. The server does not set them (see https://github.com/eclipse-basyx/basyx-go-components/issues/821), so a concept description without them is not listed. :param server: where to send the request :param created_from: if set, the concept descriptions created at or after this time :param updated_from: if set, the concept descriptions updated at or after this time :param limit: maximum number of the changes in the page :param cursor: where to continue the listing from :returns: a page of the changes :raises: :py:class:`ValueError` before sending anything if a time has no time zone :implements GetAllConceptDescriptionsRecentChanges: ``GET /concept-descriptions/$recent-changes`` """ request = _build_get_all_concept_descriptions_recent_changes( server.base_url, created_from=created_from, updated_from=updated_from, limit=limit, cursor=cursor, ) response = http.send(server.transport, request) return _parse_get_all_concept_descriptions_recent_changes(response)
def _build_query_concept_descriptions( base_url: str, query: aas_types.Query, *, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> http.Request: """Build the request of ``QueryConceptDescriptions`` for the full objects.""" _query_language.check_selects_objects(query, "query_concept_description_ids") return _query_language.build_request( base_url, "/query/concept-descriptions", query, limit, cursor ) def _parse_query_concept_descriptions( response: http.Response, ) -> aas_types.GetConceptDescriptionsResult: """Parse the response of ``QueryConceptDescriptions`` with the full objects.""" errors.raise_for_unexpected_status(response, {200}) return _json.parse( response, aas_jsonization.get_concept_descriptions_result_from_jsonable, _query_language.make_page_adapter( "ConceptDescription", paging_metadata_required=True ), )
[docs] def query_concept_descriptions( server: http.Server, query: aas_types.Query, *, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> aas_types.GetConceptDescriptionsResult: """ List a page of the concept descriptions which match :paramref:`query`. The :paramref:`query` must not select the identifiers (``"$select": "id"``); use :py:func:`query_concept_description_ids` for that. Both functions implement the same operation, but give different results. The filters of the :paramref:`query` (``$filters``) may remove parts of the concept descriptions, so the concept descriptions in the page are not necessarily complete. :param server: where to send the request :param query: query in the AAS Query Language :param limit: maximum number of the concept descriptions in the page :param cursor: where to continue the listing from :returns: a page of the matching concept descriptions :raises: :py:class:`ValueError` before sending anything if :paramref:`query` selects the identifiers, or can not be serialized to JSON (*e.g.*, a number literal is not finite) :implements QueryConceptDescriptions: ``POST /query/concept-descriptions`` """ request = _build_query_concept_descriptions( server.base_url, query, limit=limit, cursor=cursor ) response = http.send(server.transport, request) return _parse_query_concept_descriptions(response)
def _build_query_concept_description_ids( base_url: str, query: aas_types.Query, *, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> http.Request: """Build the request of ``QueryConceptDescriptions`` for the identifiers.""" _query_language.check_selects_ids(query, "query_concept_descriptions") return _query_language.build_request( base_url, "/query/concept-descriptions", query, limit, cursor ) def _parse_query_concept_description_ids( response: http.Response, ) -> query_results.IdsResult: """Parse the response of ``QueryConceptDescriptions`` with the identifiers.""" errors.raise_for_unexpected_status(response, {200}) return _json.parse( response, _json.ids_result_from_jsonable, _query_language.make_page_adapter( _query_language.IDS_RESULT_TYPE, paging_metadata_required=True ), )
[docs] def query_concept_description_ids( server: http.Server, query: aas_types.Query, *, limit: Optional[int] = None, cursor: Optional[str] = None, ) -> query_results.IdsResult: """ List a page of the identifiers of the concept descriptions which match :paramref:`query`. The :paramref:`query` must select the identifiers (``"$select": "id"``); use :py:func:`query_concept_descriptions` for the full concept descriptions. Both functions implement the same operation, but give different results. :param server: where to send the request :param query: query in the AAS Query Language :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 matching concept descriptions :raises: :py:class:`ValueError` before sending anything if :paramref:`query` does not select the identifiers, or can not be serialized to JSON (*e.g.*, a number literal is not finite) :implements QueryConceptDescriptions: ``POST /query/concept-descriptions`` """ request = _build_query_concept_description_ids( server.base_url, query, limit=limit, cursor=cursor ) response = http.send(server.transport, request) return _parse_query_concept_description_ids(response)