# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT
"""
Implement the operations of the Submodel Registry API.
The module also implements the Async Bulk Submodel Registry API. The status and
the result of the bulk operations are in :py:mod:`aas_client3_2.async_bulk`, since
both registries share them.
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, Sequence
import aas_core3_2.jsonization as aas_jsonization
import aas_core3_2.types as aas_types
from aas_client3_2 import _bulk
from aas_client3_2 import _json
from aas_client3_2 import _query
from aas_client3_2 import _query_language
from aas_client3_2 import errors
from aas_client3_2 import http
from aas_client3_2 import query_results
def _submodel_descriptor_path(submodel_identifier: str) -> str:
"""Build the path of the descriptor of the submodel with the given identifier."""
return "/submodel-descriptors/" + http.encode_base64url(submodel_identifier)
def _build_get_all_submodel_descriptors(
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 ``GetAllSubmodelDescriptors``."""
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, "/submodel-descriptors", query)
)
def _parse_get_all_submodel_descriptors(
response: http.Response,
) -> aas_types.GetSubmodelDescriptorsResult:
"""Parse the response of ``GetAllSubmodelDescriptors``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(
response, aas_jsonization.get_submodel_descriptors_result_from_jsonable
)
[docs]
def get_all_submodel_descriptors(
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.GetSubmodelDescriptorsResult:
"""
List a page of the submodel descriptors in the registry.
:param server: where to send the request
:param created_from: if set, only the descriptors created at or after this time
:param updated_from: if set, only the descriptors updated at or after this time
:param limit: maximum number of the descriptors in the page
:param cursor: where to continue the listing from
:returns: a page of the descriptors
:raises: :py:class:`ValueError` before sending anything if a time has no time
zone
:implements GetAllSubmodelDescriptors: ``GET /submodel-descriptors``
"""
request = _build_get_all_submodel_descriptors(
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_submodel_descriptors(response)
def _build_post_submodel_descriptor(
base_url: str, submodel_descriptor: aas_types.SubmodelDescriptor
) -> http.Request:
"""Build the request of ``PostSubmodelDescriptor``."""
return _json.build_request(
"POST",
http.build_url(base_url, "/submodel-descriptors"),
submodel_descriptor,
)
def _parse_post_submodel_descriptor(
response: http.Response,
) -> aas_types.SubmodelDescriptor:
"""Parse the response of ``PostSubmodelDescriptor``."""
errors.raise_for_unexpected_status(response, {201})
return _json.parse(response, aas_jsonization.submodel_descriptor_from_jsonable)
[docs]
def post_submodel_descriptor(
server: http.Server, submodel_descriptor: aas_types.SubmodelDescriptor
) -> aas_types.SubmodelDescriptor:
"""
Register a submodel with a new descriptor.
:param server: where to send the request
:param submodel_descriptor: the descriptor to be created
:returns: the created descriptor, as stored by the server
:implements PostSubmodelDescriptor: ``POST /submodel-descriptors``
"""
request = _build_post_submodel_descriptor(server.base_url, submodel_descriptor)
response = http.send(server.transport, request)
return _parse_post_submodel_descriptor(response)
def _build_get_submodel_descriptor_by_id(
base_url: str, submodel_identifier: str
) -> http.Request:
"""Build the request of ``GetSubmodelDescriptorById``."""
return _json.build_request(
"GET", http.build_url(base_url, _submodel_descriptor_path(submodel_identifier))
)
def _parse_get_submodel_descriptor_by_id(
response: http.Response,
) -> aas_types.SubmodelDescriptor:
"""Parse the response of ``GetSubmodelDescriptorById``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, aas_jsonization.submodel_descriptor_from_jsonable)
[docs]
def get_submodel_descriptor_by_id(
server: http.Server, submodel_identifier: str
) -> aas_types.SubmodelDescriptor:
"""
Get the descriptor of the submodel with the given identifier.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:returns: the descriptor
:implements GetSubmodelDescriptorById: ``GET /submodel-descriptors/{submodelIdentifier}``
"""
request = _build_get_submodel_descriptor_by_id(server.base_url, submodel_identifier)
response = http.send(server.transport, request)
return _parse_get_submodel_descriptor_by_id(response)
def _build_put_submodel_descriptor_by_id(
base_url: str,
submodel_identifier: str,
submodel_descriptor: aas_types.SubmodelDescriptor,
) -> http.Request:
"""Build the request of ``PutSubmodelDescriptorById``."""
return _json.build_request(
"PUT",
http.build_url(base_url, _submodel_descriptor_path(submodel_identifier)),
submodel_descriptor,
)
def _parse_put_submodel_descriptor_by_id(response: http.Response) -> None:
"""
Parse the response of ``PutSubmodelDescriptorById``.
The server responds with 201 and the descriptor if it created the descriptor,
or with 204 if it replaced an existing one. We do not parse the descriptor,
since it is the one which the users sent.
"""
errors.raise_for_unexpected_status(response, {201, 204})
[docs]
def put_submodel_descriptor_by_id(
server: http.Server,
submodel_identifier: str,
submodel_descriptor: aas_types.SubmodelDescriptor,
) -> None:
"""
Create or replace the descriptor of the submodel with the given identifier.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param submodel_descriptor: the descriptor to be stored
:implements PutSubmodelDescriptorById: ``PUT /submodel-descriptors/{submodelIdentifier}``
"""
request = _build_put_submodel_descriptor_by_id(
server.base_url, submodel_identifier, submodel_descriptor
)
response = http.send(server.transport, request)
_parse_put_submodel_descriptor_by_id(response)
def _build_delete_submodel_descriptor_by_id(
base_url: str, submodel_identifier: str
) -> http.Request:
"""Build the request of ``DeleteSubmodelDescriptorById``."""
return _json.build_request(
"DELETE",
http.build_url(base_url, _submodel_descriptor_path(submodel_identifier)),
)
def _parse_delete_submodel_descriptor_by_id(response: http.Response) -> None:
"""Parse the response of ``DeleteSubmodelDescriptorById``."""
errors.raise_for_unexpected_status(response, {204})
[docs]
def delete_submodel_descriptor_by_id(
server: http.Server, submodel_identifier: str
) -> None:
"""
De-register the submodel by deleting its descriptor.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:implements DeleteSubmodelDescriptorById: ``DELETE /submodel-descriptors/{submodelIdentifier}``
"""
request = _build_delete_submodel_descriptor_by_id(
server.base_url, submodel_identifier
)
response = http.send(server.transport, request)
_parse_delete_submodel_descriptor_by_id(response)
def _build_query_submodel_descriptors(
base_url: str,
query: aas_types.Query,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``QuerySubmodelDescriptors`` for the descriptors."""
_query_language.check_selects_objects(query, "query_submodel_descriptor_ids")
return _query_language.build_request(
base_url, "/query/submodel-descriptors", query, limit, cursor
)
def _parse_query_submodel_descriptors(
response: http.Response,
) -> aas_types.GetSubmodelDescriptorsResult:
"""Parse the response of ``QuerySubmodelDescriptors`` with the descriptors."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(
response,
aas_jsonization.get_submodel_descriptors_result_from_jsonable,
_query_language.make_page_adapter(
"SubmodelDescriptor", paging_metadata_required=True
),
)
[docs]
def query_submodel_descriptors(
server: http.Server,
query: aas_types.Query,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> aas_types.GetSubmodelDescriptorsResult:
"""
List a page of the submodel descriptors which match :paramref:`query`.
The :paramref:`query` must not select the identifiers (``"$select": "id"``);
use :py:func:`query_submodel_descriptor_ids` for that. 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 descriptors in the page
:param cursor: where to continue the listing from
:returns: a page of the matching descriptors
: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 QuerySubmodelDescriptors: ``POST /query/submodel-descriptors``
"""
request = _build_query_submodel_descriptors(
server.base_url, query, limit=limit, cursor=cursor
)
response = http.send(server.transport, request)
return _parse_query_submodel_descriptors(response)
def _build_query_submodel_descriptor_ids(
base_url: str,
query: aas_types.Query,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``QuerySubmodelDescriptors`` for the identifiers."""
_query_language.check_selects_ids(query, "query_submodel_descriptors")
return _query_language.build_request(
base_url, "/query/submodel-descriptors", query, limit, cursor
)
def _parse_query_submodel_descriptor_ids(
response: http.Response,
) -> query_results.IdsResult:
"""Parse the response of ``QuerySubmodelDescriptors`` 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_submodel_descriptor_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 submodels whose descriptors match
:paramref:`query`.
The :paramref:`query` must select the identifiers (``"$select": "id"``); use
:py:func:`query_submodel_descriptors` for the descriptors. 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 submodels
: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 QuerySubmodelDescriptors: ``POST /query/submodel-descriptors``
"""
request = _build_query_submodel_descriptor_ids(
server.base_url, query, limit=limit, cursor=cursor
)
response = http.send(server.transport, request)
return _parse_query_submodel_descriptor_ids(response)
def _build_bulk_post_submodel(
base_url: str, submodel_descriptors: Sequence[aas_types.SubmodelDescriptor]
) -> http.Request:
"""Build the request of ``BulkPostSubmodel``."""
return _bulk.build_descriptors_request(
"POST",
http.build_url(base_url, "/bulk/submodel-descriptors"),
submodel_descriptors,
)
def _parse_bulk_post_submodel(response: http.Response) -> aas_types.OperationHandle:
"""Parse the response of ``BulkPostSubmodel``."""
return _bulk.parse_accepted(response)
[docs]
def bulk_post_submodel(
server: http.Server, submodel_descriptors: Sequence[aas_types.SubmodelDescriptor]
) -> aas_types.OperationHandle:
"""
Start registering the submodels with new descriptors at once.
The operation ID of the OpenAPI speaks of submodels, but the operation creates
their descriptors.
The server processes the bulk request asynchronously. Follow its progress with
:py:func:`aas_client3_2.async_bulk.bulk_get_async_status`, and fetch its result
with :py:func:`aas_client3_2.async_bulk.bulk_get_async_result`. The server may
accept a bulk request which it later finds invalid; only the result tells.
:param server: where to send the request
:param submodel_descriptors: the descriptors to be created
:returns: the handle of the bulk operation
:raises: :py:class:`ValueError` before sending anything if
:paramref:`submodel_descriptors` is empty
:implements BulkPostSubmodel: ``POST /bulk/submodel-descriptors``
"""
request = _build_bulk_post_submodel(server.base_url, submodel_descriptors)
response = http.send(server.transport, request)
return _parse_bulk_post_submodel(response)
def _build_bulk_put_submodel_descriptors_by_id(
base_url: str, submodel_descriptors: Sequence[aas_types.SubmodelDescriptor]
) -> http.Request:
"""Build the request of ``BulkPutSubmodelDescriptorsById``."""
return _bulk.build_descriptors_request(
"PUT",
http.build_url(base_url, "/bulk/submodel-descriptors"),
submodel_descriptors,
)
def _parse_bulk_put_submodel_descriptors_by_id(
response: http.Response,
) -> aas_types.OperationHandle:
"""Parse the response of ``BulkPutSubmodelDescriptorsById``."""
return _bulk.parse_accepted(response)
[docs]
def bulk_put_submodel_descriptors_by_id(
server: http.Server, submodel_descriptors: Sequence[aas_types.SubmodelDescriptor]
) -> aas_types.OperationHandle:
"""
Start creating or replacing the submodel descriptors at once.
The descriptors are matched to the stored ones by their ``id``. See
:py:func:`bulk_post_submodel` for how to follow the bulk operation.
:param server: where to send the request
:param submodel_descriptors: the descriptors to be stored
:returns: the handle of the bulk operation
:raises: :py:class:`ValueError` before sending anything if
:paramref:`submodel_descriptors` is empty
:implements BulkPutSubmodelDescriptorsById: ``PUT /bulk/submodel-descriptors``
"""
request = _build_bulk_put_submodel_descriptors_by_id(
server.base_url, submodel_descriptors
)
response = http.send(server.transport, request)
return _parse_bulk_put_submodel_descriptors_by_id(response)
def _build_bulk_delete_submodel_descriptors_by_id(
base_url: str, submodel_identifiers: Sequence[str]
) -> http.Request:
"""Build the request of ``BulkDeleteSubmodelDescriptorsById``."""
return _bulk.build_identifiers_request(
http.build_url(base_url, "/bulk/submodel-descriptors"), submodel_identifiers
)
def _parse_bulk_delete_submodel_descriptors_by_id(
response: http.Response,
) -> aas_types.OperationHandle:
"""Parse the response of ``BulkDeleteSubmodelDescriptorsById``."""
return _bulk.parse_accepted(response)
[docs]
def bulk_delete_submodel_descriptors_by_id(
server: http.Server, submodel_identifiers: Sequence[str]
) -> aas_types.OperationHandle:
"""
Start de-registering the submodels at once.
See :py:func:`bulk_post_submodel` for how to follow the bulk operation.
:param server: where to send the request
:param submodel_identifiers: identifiers of the submodels whose descriptors are
deleted
:returns: the handle of the bulk operation
:raises: :py:class:`ValueError` before sending anything if
:paramref:`submodel_identifiers` is empty
:implements BulkDeleteSubmodelDescriptorsById: ``DELETE /bulk/submodel-descriptors``
"""
request = _build_bulk_delete_submodel_descriptors_by_id(
server.base_url, submodel_identifiers
)
response = http.send(server.transport, request)
return _parse_bulk_delete_submodel_descriptors_by_id(response)