Source code for aas_client3_2.serialization

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

"""
Implement the operation of the Serialization API.

The public function implements exactly one operation of the specification. The
private functions ``_build_*`` give the request 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 enum
from typing import ContextManager, Optional, Sequence

from aas_client3_2 import _files
from aas_client3_2 import _query
from aas_client3_2 import http


[docs] class SerializationFormat(enum.Enum): """ Give the formats of a serialization, as the media types of the header ``Accept``. Part 2 lists JSON and XML as the values of ``SerializationFormat``, with XML as the default. The OpenAPI gives the AASX package as a third format, with its own spelling of the media type, which the client follows, although the table of the HTTP API and the AASX File Server API spell it differently. """ #: Environment serialized to JSON JSON = "application/json" #: Environment serialized to XML XML = "application/xml" #: AASX package, as the OpenAPI spells its media type AASX = "application/aas+zip"
def _build_generate_serialization_by_ids( base_url: str, *, aas_ids: Optional[Sequence[str]] = None, submodel_ids: Optional[Sequence[str]] = None, include_concept_descriptions: Optional[bool] = None, serialization_format: SerializationFormat = SerializationFormat.XML, ) -> http.Request: """Build the request of ``GenerateSerializationByIds``.""" query: _query.Query = [] # NOTE (mristin): # The specification repeats the parameters for every identifier, encoded in # base64url as the identifiers in the paths. if aas_ids is not None: query.extend(("aasIds", http.encode_base64url(an_id)) for an_id in aas_ids) if submodel_ids is not None: query.extend( ("submodelIds", http.encode_base64url(an_id)) for an_id in submodel_ids ) if include_concept_descriptions is not None: query.append( ( "includeConceptDescriptions", "true" if include_concept_descriptions else "false", ) ) return _files.build_download_request( http.build_url(base_url, "/serialization", query), accept=serialization_format.value, )
[docs] def generate_serialization_by_ids( server: http.Server, *, aas_ids: Optional[Sequence[str]] = None, submodel_ids: Optional[Sequence[str]] = None, include_concept_descriptions: Optional[bool] = None, serialization_format: SerializationFormat = SerializationFormat.XML, ) -> ContextManager[http.Download]: """ Download the serialization of the given shells and submodels as an environment. The serialization can be large, so it is always streamed, whatever its format; use the result as a context manager. For example, to parse the JSON with aas-core3.2: .. code-block:: python import json import aas_core3_2.jsonization as aas_jsonization with serialization.generate_serialization_by_ids( server, aas_ids=["urn:example:aas:1"], serialization_format=serialization.SerializationFormat.JSON, ) as download: environment = aas_jsonization.environment_from_jsonable( json.loads(b"".join(download.iter_chunks())) ) The client asks for the format with the header ``Accept``, and always sends it, so that the result does not depend on a default of the server. :param server: where to send the request :param aas_ids: if set, the identifiers of the shells to be included :param submodel_ids: if set, the identifiers of the submodels to be included :param include_concept_descriptions: if set, whether to include the concept descriptions which the submodels refer to; the default of the OpenAPI is to include them :param serialization_format: the format of the serialization :returns: a context manager which gives the download :implements GenerateSerializationByIds: ``GET /serialization`` """ request = _build_generate_serialization_by_ids( server.base_url, aas_ids=aas_ids, submodel_ids=submodel_ids, include_concept_descriptions=include_concept_descriptions, serialization_format=serialization_format, ) return _files.download(server, request, {200})