# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT
"""
Implement the operations of the Submodel API.
The Submodel API is the interface of a single submodel. Its paths are relative to
the endpoint of the submodel, as a registry lists it in ``protocolInformation.href``
of a descriptor with the interface ``SUBMODEL-3.2``. Hence the base URL of
the :py:class:`~aas_client3_2.http.Server` is the endpoint itself, such as
``https://example.com/submodels/dXJuOmV4YW1wbGU6c206MQ``, and the paths are appended
without the prefix ``/submodel`` of the OpenAPI. Part 2 has the clients use
the endpoint from the registry as it is, so that a submodel server and a submodel
repository can not be told apart.
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 ContextManager, Final, IO, List, Optional, Union
import aas_core3_2.types as aas_types
from aas_client3_2 import _files
from aas_client3_2 import _invocation
from aas_client3_2 import _submodel_operations
from aas_client3_2 import errors
from aas_client3_2 import http
#: Path of the submodel relative to its endpoint, which is the submodel itself
_SUBMODEL_PATH: Final[str] = ""
def _build_get_submodel(
base_url: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodel``."""
return _submodel_operations.build_get_submodel_by_id(
base_url, _SUBMODEL_PATH, level=level, extent=extent
)
def _parse_get_submodel(response: http.Response) -> aas_types.Submodel:
"""Parse the response of ``GetSubmodel``."""
return _submodel_operations.parse_get_submodel_by_id(response)
[docs]
def get_submodel(
server: http.Server,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.Submodel:
"""
Get the submodel.
:param server: where to send the request
:param level: depth of the returned submodel
:param extent: whether to include the values of blobs
:returns: the submodel
:implements GetSubmodel: ``GET /submodel``
"""
request = _build_get_submodel(server.base_url, level=level, extent=extent)
response = http.send(server.transport, request)
return _parse_get_submodel(response)
def _build_put_submodel(base_url: str, submodel: aas_types.Submodel) -> http.Request:
"""Build the request of ``PutSubmodel``."""
return _submodel_operations.build_put_submodel_by_id(
base_url, _SUBMODEL_PATH, submodel
)
def _parse_put_submodel(response: http.Response) -> None:
"""
Parse the response of ``PutSubmodel``.
Unlike in the Submodel Repository, the submodel can only be replaced, not
created, so the OpenAPI gives only 204.
"""
errors.raise_for_unexpected_status(response, {204})
[docs]
def put_submodel(server: http.Server, submodel: aas_types.Submodel) -> None:
"""
Replace the submodel.
:param server: where to send the request
:param submodel: the submodel to be stored
:implements PutSubmodel: ``PUT /submodel``
"""
request = _build_put_submodel(server.base_url, submodel)
response = http.send(server.transport, request)
_parse_put_submodel(response)
def _build_patch_submodel(base_url: str, submodel: aas_types.Submodel) -> http.Request:
"""Build the request of ``PatchSubmodel``."""
return _submodel_operations.build_patch_submodel_by_id(
base_url, _SUBMODEL_PATH, submodel
)
def _parse_patch_submodel(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodel``."""
return _submodel_operations.parse_patch_submodel_by_id(response)
[docs]
def patch_submodel(server: http.Server, submodel: aas_types.Submodel) -> None:
"""
Update the existing submodel with the given identifier.
Part 2 replaces the elements of the stored submodel one by one with
the elements of :paramref:`submodel`, which must all exist on the server.
The query parameter ``level`` is omitted, since it allows only the value
``core``.
:param server: where to send the request
:param submodel: the submodel with the updates
:implements PatchSubmodel: ``PATCH /submodel``
"""
request = _build_patch_submodel(server.base_url, submodel)
response = http.send(server.transport, request)
_parse_patch_submodel(response)
def _build_get_submodel_metadata(base_url: str) -> http.Request:
"""Build the request of ``GetSubmodel-Metadata``."""
return _submodel_operations.build_get_submodel_by_id_metadata(
base_url, _SUBMODEL_PATH
)
def _parse_get_submodel_metadata(
response: http.Response,
) -> aas_types.SubmodelMetadata:
"""Parse the response of ``GetSubmodel-Metadata``."""
return _submodel_operations.parse_get_submodel_by_id_metadata(response)
def _build_patch_submodel_metadata(
base_url: str,
submodel_metadata: aas_types.SubmodelMetadata,
) -> http.Request:
"""Build the request of ``PatchSubmodel-Metadata``."""
return _submodel_operations.build_patch_submodel_by_id_metadata(
base_url, _SUBMODEL_PATH, submodel_metadata
)
def _parse_patch_submodel_metadata(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodel-Metadata``."""
return _submodel_operations.parse_patch_submodel_by_id_metadata(response)
def _build_get_submodel_reference(base_url: str) -> http.Request:
"""Build the request of ``GetSubmodel-Reference``."""
return _submodel_operations.build_get_submodel_by_id_reference(
base_url, _SUBMODEL_PATH
)
def _parse_get_submodel_reference(response: http.Response) -> aas_types.Reference:
"""Parse the response of ``GetSubmodel-Reference``."""
return _submodel_operations.parse_get_submodel_by_id_reference(response)
[docs]
def get_submodel_reference(server: http.Server) -> aas_types.Reference:
"""
Get a reference to the submodel.
:param server: where to send the request
:returns: the reference to the submodel
:implements GetSubmodel-Reference: ``GET /submodel/$reference``
"""
request = _build_get_submodel_reference(server.base_url)
response = http.send(server.transport, request)
return _parse_get_submodel_reference(response)
def _build_get_submodel_value_only(
base_url: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodel-ValueOnly``."""
return _submodel_operations.build_get_submodel_by_id_value_only(
base_url, _SUBMODEL_PATH, level=level, extent=extent
)
def _parse_get_submodel_value_only(
response: http.Response,
) -> aas_types.JsonObject:
"""Parse the response of ``GetSubmodel-ValueOnly``."""
return _submodel_operations.parse_get_submodel_by_id_value_only(response)
[docs]
def get_submodel_value_only(
server: http.Server,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.JsonObject:
"""
Get the values of the submodel.
The values are in the ValueOnly serialization, a JSON object keyed by
the ID-shorts of the submodel elements. They can not be parsed further without
knowing the structure of the submodel, for example, from
:py:func:`get_submodel_metadata`, since the same JSON can stand for different kinds
of elements; for example, a range looks exactly like a collection with
the properties ``min`` and ``max``.
:param server: where to send the request
:param level: depth of the returned values
:param extent: whether to include the values of blobs
:returns: the values of the submodel
:implements GetSubmodel-ValueOnly: ``GET /submodel/$value``
"""
request = _build_get_submodel_value_only(
server.base_url, level=level, extent=extent
)
response = http.send(server.transport, request)
return _parse_get_submodel_value_only(response)
def _build_patch_submodel_value_only(
base_url: str, submodel: aas_types.Submodel
) -> http.Request:
"""Build the request of ``PatchSubmodel-ValueOnly``."""
return _submodel_operations.build_patch_submodel_by_id_value_only(
base_url, _SUBMODEL_PATH, submodel
)
def _parse_patch_submodel_value_only(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodel-ValueOnly``."""
return _submodel_operations.parse_patch_submodel_by_id_value_only(response)
[docs]
def patch_submodel_value_only(
server: http.Server, submodel: aas_types.Submodel
) -> None:
"""
Update the values of the submodel.
The client sends only the values of :paramref:`submodel`, in the ValueOnly
serialization. Part 2 replaces the values of the stored elements one by one,
and the elements must all exist on the server. Everything else, such as
the semantic IDs, remains unchanged.
The query parameter ``level`` is omitted, since it allows only the value
``core``.
:param server: where to send the request
:param submodel: the submodel with the new values
:raises:
:py:class:`ValueError` if a value of :paramref:`submodel` can not be
serialized, such as a property ``xs:int`` with the value ``"abc"``
:implements PatchSubmodel-ValueOnly: ``PATCH /submodel/$value``
"""
request = _build_patch_submodel_value_only(server.base_url, submodel)
response = http.send(server.transport, request)
_parse_patch_submodel_value_only(response)
def _build_get_submodel_path(
base_url: str,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetSubmodel-Path``."""
return _submodel_operations.build_get_submodel_by_id_path(
base_url, _SUBMODEL_PATH, level=level
)
def _parse_get_submodel_path(response: http.Response) -> List[str]:
"""Parse the response of ``GetSubmodel-Path``."""
return _submodel_operations.parse_get_submodel_by_id_path(response)
[docs]
def get_submodel_path(
server: http.Server,
*,
level: Optional[aas_types.Level] = None,
) -> List[str]:
"""
List the ID-short paths of the elements of the submodel.
The paths start at the top-level elements, such as ``Motor`` and
``Motor.Speed``, since the submodel itself has no path. With
:paramref:`level` set to core, only the paths of the top-level elements are
listed.
:param server: where to send the request
:param level: depth of the returned paths
:returns: the paths
:implements GetSubmodel-Path: ``GET /submodel/$path``
"""
request = _build_get_submodel_path(server.base_url, level=level)
response = http.send(server.transport, request)
return _parse_get_submodel_path(response)
def _build_get_all_submodel_elements(
base_url: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElements``."""
return _submodel_operations.build_get_all_submodel_elements(
base_url,
_SUBMODEL_PATH,
limit=limit,
cursor=cursor,
level=level,
extent=extent,
)
def _parse_get_all_submodel_elements(
response: http.Response,
) -> aas_types.GetSubmodelElementsResult:
"""Parse the response of ``GetAllSubmodelElements``."""
return _submodel_operations.parse_get_all_submodel_elements(response)
[docs]
def get_all_submodel_elements(
server: http.Server,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.GetSubmodelElementsResult:
"""
List a page of the top-level submodel elements of the submodel.
:param server: where to send the request
:param limit: maximum number of the submodel elements in the page
:param cursor: where to continue the listing from
:param level: depth of the returned submodel elements
:param extent: whether to include the values of blobs
:returns: a page of the submodel elements
:implements GetAllSubmodelElements: ``GET /submodel/submodel-elements``
"""
request = _build_get_all_submodel_elements(
server.base_url,
limit=limit,
cursor=cursor,
level=level,
extent=extent,
)
response = http.send(server.transport, request)
return _parse_get_all_submodel_elements(response)
def _build_post_submodel_element(
base_url: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PostSubmodelElement``."""
return _submodel_operations.build_post_submodel_element(
base_url, _SUBMODEL_PATH, submodel_element
)
def _parse_post_submodel_element(response: http.Response) -> aas_types.SubmodelElement:
"""Parse the response of ``PostSubmodelElement``."""
return _submodel_operations.parse_post_submodel_element(response)
[docs]
def post_submodel_element(
server: http.Server,
submodel_element: aas_types.SubmodelElement,
) -> aas_types.SubmodelElement:
"""
Add a new top-level submodel element to the submodel.
:param server: where to send the request
:param submodel_element: the submodel element to be created
:returns: the created submodel element, as stored by the server
:implements PostSubmodelElement: ``POST /submodel/submodel-elements``
"""
request = _build_post_submodel_element(server.base_url, submodel_element)
response = http.send(server.transport, request)
return _parse_post_submodel_element(response)
def _build_get_all_submodel_elements_metadata(
base_url: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElements-Metadata``."""
return _submodel_operations.build_get_all_submodel_elements_metadata(
base_url, _SUBMODEL_PATH, limit=limit, cursor=cursor
)
def _parse_get_all_submodel_elements_metadata(
response: http.Response,
) -> aas_types.GetSubmodelElementsMetadataResult:
"""Parse the response of ``GetAllSubmodelElements-Metadata``."""
return _submodel_operations.parse_get_all_submodel_elements_metadata(response)
def _build_get_all_submodel_elements_value_only(
base_url: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElements-ValueOnly``."""
return _submodel_operations.build_get_all_submodel_elements_value_only(
base_url,
_SUBMODEL_PATH,
limit=limit,
cursor=cursor,
level=level,
extent=extent,
)
def _parse_get_all_submodel_elements_value_only(
response: http.Response,
) -> aas_types.GetSubmodelElementsValueResult:
"""Parse the response of ``GetAllSubmodelElements-ValueOnly``."""
return _submodel_operations.parse_get_all_submodel_elements_value_only(response)
[docs]
def get_all_submodel_elements_value_only(
server: http.Server,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.GetSubmodelElementsValueResult:
"""
List a page of the values of the top-level submodel elements of the submodel.
Each item of the page is a JSON object with a single key, the ID-short of
the element, whose value is the ValueOnly serialization of the element. Unlike
the OpenAPI, which types the page as a single JSON object, aas-core3.2 follows
Eclipse BaSyx Go components and the prose of Part 2 here.
:param server: where to send the request
:param limit: maximum number of the submodel elements in the page
:param cursor: where to continue the listing from
:param level: depth of the returned values
:param extent: whether to include the values of blobs
:returns: a page of the values of the submodel elements
:implements GetAllSubmodelElements-ValueOnly: ``GET /submodel/submodel-elements/$value``
"""
request = _build_get_all_submodel_elements_value_only(
server.base_url,
limit=limit,
cursor=cursor,
level=level,
extent=extent,
)
response = http.send(server.transport, request)
return _parse_get_all_submodel_elements_value_only(response)
def _build_get_all_submodel_elements_reference(
base_url: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElements-Reference``."""
return _submodel_operations.build_get_all_submodel_elements_reference(
base_url, _SUBMODEL_PATH, limit=limit, cursor=cursor
)
def _parse_get_all_submodel_elements_reference(
response: http.Response,
) -> aas_types.GetReferencesResult:
"""Parse the response of ``GetAllSubmodelElements-Reference``."""
return _submodel_operations.parse_get_all_submodel_elements_reference(response)
[docs]
def get_all_submodel_elements_reference(
server: http.Server,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> aas_types.GetReferencesResult:
"""
List a page of the references to the top-level submodel elements of the submodel.
:param server: where to send the request
:param limit: maximum number of the references in the page
:param cursor: where to continue the listing from
:returns: a page of the references
:implements GetAllSubmodelElements-Reference: ``GET /submodel/submodel-elements/$reference``
"""
request = _build_get_all_submodel_elements_reference(
server.base_url, limit=limit, cursor=cursor
)
response = http.send(server.transport, request)
return _parse_get_all_submodel_elements_reference(response)
def _build_get_all_submodel_elements_path(
base_url: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElements-Path``."""
return _submodel_operations.build_get_all_submodel_elements_path(
base_url,
_SUBMODEL_PATH,
limit=limit,
cursor=cursor,
level=level,
)
def _parse_get_all_submodel_elements_path(
response: http.Response,
) -> aas_types.GetPathItemsResult:
"""Parse the response of ``GetAllSubmodelElements-Path``."""
return _submodel_operations.parse_get_all_submodel_elements_path(response)
[docs]
def get_all_submodel_elements_path(
server: http.Server,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
) -> aas_types.GetPathItemsResult:
"""
List a page of the ID-short paths of the elements of the submodel.
The limit applies to the paths, not to the top-level elements.
:param server: where to send the request
:param limit: maximum number of the paths in the page
:param cursor: where to continue the listing from
:param level: depth of the returned paths
:returns: a page of the paths
:implements GetAllSubmodelElements-Path: ``GET /submodel/submodel-elements/$path``
"""
request = _build_get_all_submodel_elements_path(
server.base_url, limit=limit, cursor=cursor, level=level
)
response = http.send(server.transport, request)
return _parse_get_all_submodel_elements_path(response)
def _build_get_submodel_element_by_path(
base_url: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath``."""
return _submodel_operations.build_get_submodel_element_by_path(
base_url,
_SUBMODEL_PATH,
id_short_path,
level=level,
extent=extent,
)
def _parse_get_submodel_element_by_path(
response: http.Response,
) -> aas_types.SubmodelElement:
"""Parse the response of ``GetSubmodelElementByPath``."""
return _submodel_operations.parse_get_submodel_element_by_path(response)
[docs]
def get_submodel_element_by_path(
server: http.Server,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.SubmodelElement:
"""
Get the submodel element at the given ID-short path.
:param server: where to send the request
:param id_short_path: ID-short path of the element, such as ``Motor.Speed``
:param level: depth of the returned element
:param extent: whether to include the values of blobs
:returns: the submodel element
:implements GetSubmodelElementByPath: ``GET /submodel/submodel-elements/{idShortPath}``
"""
request = _build_get_submodel_element_by_path(
server.base_url, id_short_path, level=level, extent=extent
)
response = http.send(server.transport, request)
return _parse_get_submodel_element_by_path(response)
def _build_post_submodel_element_by_path(
base_url: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PostSubmodelElementByPath``."""
return _submodel_operations.build_post_submodel_element_by_path(
base_url, _SUBMODEL_PATH, id_short_path, submodel_element
)
def _parse_post_submodel_element_by_path(
response: http.Response,
) -> aas_types.SubmodelElement:
"""Parse the response of ``PostSubmodelElementByPath``."""
return _submodel_operations.parse_post_submodel_element_by_path(response)
[docs]
def post_submodel_element_by_path(
server: http.Server,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> aas_types.SubmodelElement:
"""
Add a new submodel element as a child of the element at the given ID-short path.
The element at :paramref:`id_short_path` must be able to hold children, such as
a submodel element collection, a submodel element list or an entity.
:param server: where to send the request
:param id_short_path: ID-short path of the parent element
:param submodel_element: the submodel element to be created
:returns: the created submodel element, as stored by the server
:implements PostSubmodelElementByPath: ``POST /submodel/submodel-elements/{idShortPath}``
"""
request = _build_post_submodel_element_by_path(
server.base_url, id_short_path, submodel_element
)
response = http.send(server.transport, request)
return _parse_post_submodel_element_by_path(response)
def _build_put_submodel_element_by_path(
base_url: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PutSubmodelElementByPath``."""
return _submodel_operations.build_put_submodel_element_by_path(
base_url, _SUBMODEL_PATH, id_short_path, submodel_element
)
def _parse_put_submodel_element_by_path(response: http.Response) -> None:
"""Parse the response of ``PutSubmodelElementByPath``.
The server responds with 201 and the element if it created the element, or
with 204 if it updated an existing one. We do not parse the element, since it
is the one which the users sent."""
return _submodel_operations.parse_put_submodel_element_by_path(response)
[docs]
def put_submodel_element_by_path(
server: http.Server,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> None:
"""
Create or replace the submodel element at the given ID-short path.
The query parameter ``level`` is omitted, since it allows only the value
``deep``.
:param server: where to send the request
:param id_short_path: ID-short path of the element
:param submodel_element: the submodel element to be stored
:implements PutSubmodelElementByPath: ``PUT /submodel/submodel-elements/{idShortPath}``
"""
request = _build_put_submodel_element_by_path(
server.base_url, id_short_path, submodel_element
)
response = http.send(server.transport, request)
_parse_put_submodel_element_by_path(response)
def _build_patch_submodel_element_by_path(
base_url: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PatchSubmodelElementByPath``."""
return _submodel_operations.build_patch_submodel_element_by_path(
base_url, _SUBMODEL_PATH, id_short_path, submodel_element
)
def _parse_patch_submodel_element_by_path(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodelElementByPath``."""
return _submodel_operations.parse_patch_submodel_element_by_path(response)
[docs]
def patch_submodel_element_by_path(
server: http.Server,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> None:
"""
Update the existing submodel element at the given ID-short path.
Part 2 replaces the stored element and its children one by one with
:paramref:`submodel_element` and its children, which must all exist on
the server.
The query parameter ``level`` is omitted, since it allows only the value
``core``.
:param server: where to send the request
:param id_short_path: ID-short path of the element
:param submodel_element: the submodel element with the updates
:implements PatchSubmodelElementByPath: ``PATCH /submodel/submodel-elements/{idShortPath}``
"""
request = _build_patch_submodel_element_by_path(
server.base_url, id_short_path, submodel_element
)
response = http.send(server.transport, request)
_parse_patch_submodel_element_by_path(response)
def _build_delete_submodel_element_by_path(
base_url: str, id_short_path: str
) -> http.Request:
"""Build the request of ``DeleteSubmodelElementByPath``."""
return _submodel_operations.build_delete_submodel_element_by_path(
base_url, _SUBMODEL_PATH, id_short_path
)
def _parse_delete_submodel_element_by_path(response: http.Response) -> None:
"""Parse the response of ``DeleteSubmodelElementByPath``."""
return _submodel_operations.parse_delete_submodel_element_by_path(response)
[docs]
def delete_submodel_element_by_path(server: http.Server, id_short_path: str) -> None:
"""
Delete the submodel element at the given ID-short path.
:param server: where to send the request
:param id_short_path: ID-short path of the element
:implements DeleteSubmodelElementByPath: ``DELETE /submodel/submodel-elements/{idShortPath}``
"""
request = _build_delete_submodel_element_by_path(server.base_url, id_short_path)
response = http.send(server.transport, request)
_parse_delete_submodel_element_by_path(response)
def _build_get_submodel_element_by_path_metadata(
base_url: str, id_short_path: str
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-Metadata``."""
return _submodel_operations.build_get_submodel_element_by_path_metadata(
base_url, _SUBMODEL_PATH, id_short_path
)
def _parse_get_submodel_element_by_path_metadata(
response: http.Response,
) -> aas_types.SubmodelElementMetadata:
"""Parse the response of ``GetSubmodelElementByPath-Metadata``."""
return _submodel_operations.parse_get_submodel_element_by_path_metadata(response)
def _build_patch_submodel_element_by_path_metadata(
base_url: str,
id_short_path: str,
submodel_element_metadata: aas_types.SubmodelElementMetadata,
) -> http.Request:
"""Build the request of ``PatchSubmodelElementByPath-Metadata``."""
return _submodel_operations.build_patch_submodel_element_by_path_metadata(
base_url,
_SUBMODEL_PATH,
id_short_path,
submodel_element_metadata,
)
def _parse_patch_submodel_element_by_path_metadata(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodelElementByPath-Metadata``."""
return _submodel_operations.parse_patch_submodel_element_by_path_metadata(response)
def _build_get_submodel_element_by_path_value_only(
base_url: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-ValueOnly``."""
return _submodel_operations.build_get_submodel_element_by_path_value_only(
base_url,
_SUBMODEL_PATH,
id_short_path,
level=level,
extent=extent,
)
def _parse_get_submodel_element_by_path_value_only(
response: http.Response,
) -> aas_types.JsonValue:
"""Parse the response of ``GetSubmodelElementByPath-ValueOnly``."""
return _submodel_operations.parse_get_submodel_element_by_path_value_only(response)
[docs]
def get_submodel_element_by_path_value_only(
server: http.Server,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.JsonValue:
"""
Get the value of the submodel element at the given ID-short path.
The value is in the ValueOnly serialization, without the ID-short of
the element; for example, ``5`` for a property ``xs:int``, or ``{"min": 1,
"max": 5}`` for a range. The value can not be parsed further without knowing
the kind of the element, since the ValueOnly serialization drops the model
types; for example, the range above looks exactly like a collection with
the properties ``min`` and ``max``. If you know the kind, you can parse some
kinds with aas-core3.2, for example, a range with
:py:func:`aas_core3_2.jsonization.range_value_from_jsonable`.
:param server: where to send the request
:param id_short_path: ID-short path of the element, such as ``Motor.Speed``
:param level: depth of the returned value
:param extent: whether to include the values of blobs
:returns: the value of the submodel element
:implements GetSubmodelElementByPath-ValueOnly: ``GET /submodel/submodel-elements/{idShortPath}/$value``
"""
request = _build_get_submodel_element_by_path_value_only(
server.base_url, id_short_path, level=level, extent=extent
)
response = http.send(server.transport, request)
return _parse_get_submodel_element_by_path_value_only(response)
def _build_patch_submodel_element_by_path_value_only(
base_url: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PatchSubmodelElementByPath-ValueOnly``."""
return _submodel_operations.build_patch_submodel_element_by_path_value_only(
base_url, _SUBMODEL_PATH, id_short_path, submodel_element
)
def _parse_patch_submodel_element_by_path_value_only(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodelElementByPath-ValueOnly``."""
return _submodel_operations.parse_patch_submodel_element_by_path_value_only(
response
)
[docs]
def patch_submodel_element_by_path_value_only(
server: http.Server,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> None:
"""
Update the value of the submodel element at the given ID-short path.
The client sends only the value of :paramref:`submodel_element`, in
the ValueOnly serialization. Part 2 replaces the value of the stored element,
and of its children one by one, which must all exist on the server. Everything
else, such as the semantic ID, remains unchanged.
The query parameter ``level`` is omitted, since it allows only the value
``core``.
:param server: where to send the request
:param id_short_path: ID-short path of the element
:param submodel_element: the submodel element with the new value
:raises:
:py:class:`ValueError` if :paramref:`submodel_element` is an operation or
a capability, which have no value, or if its value can not be serialized,
such as a property ``xs:int`` with the value ``"abc"``
:implements PatchSubmodelElementByPath-ValueOnly: ``PATCH /submodel/submodel-elements/{idShortPath}/$value``
"""
request = _build_patch_submodel_element_by_path_value_only(
server.base_url, id_short_path, submodel_element
)
response = http.send(server.transport, request)
_parse_patch_submodel_element_by_path_value_only(response)
def _build_get_submodel_element_by_path_reference(
base_url: str, id_short_path: str
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-Reference``."""
return _submodel_operations.build_get_submodel_element_by_path_reference(
base_url, _SUBMODEL_PATH, id_short_path
)
def _parse_get_submodel_element_by_path_reference(
response: http.Response,
) -> aas_types.Reference:
"""Parse the response of ``GetSubmodelElementByPath-Reference``."""
return _submodel_operations.parse_get_submodel_element_by_path_reference(response)
[docs]
def get_submodel_element_by_path_reference(
server: http.Server, id_short_path: str
) -> aas_types.Reference:
"""
Get a reference to the submodel element at the given ID-short path.
The reference is a model reference with a key for the submodel, followed by
a key for each element along the path. The key of an element in a list holds
its index, such as ``0``.
:param server: where to send the request
:param id_short_path: ID-short path of the element, such as ``Motor.Speed``
:returns: the reference to the submodel element
:implements GetSubmodelElementByPath-Reference: ``GET /submodel/submodel-elements/{idShortPath}/$reference``
"""
request = _build_get_submodel_element_by_path_reference(
server.base_url, id_short_path
)
response = http.send(server.transport, request)
return _parse_get_submodel_element_by_path_reference(response)
def _build_get_submodel_element_by_path_path(
base_url: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-Path``."""
return _submodel_operations.build_get_submodel_element_by_path_path(
base_url, _SUBMODEL_PATH, id_short_path, level=level
)
def _parse_get_submodel_element_by_path_path(response: http.Response) -> List[str]:
"""Parse the response of ``GetSubmodelElementByPath-Path``."""
return _submodel_operations.parse_get_submodel_element_by_path_path(response)
[docs]
def get_submodel_element_by_path_path(
server: http.Server,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
) -> List[str]:
"""
List the ID-short paths of the submodel element and of its descendants.
The paths include the path of the element itself. With :paramref:`level` set
to core, only the paths of the element and of its direct children are listed.
:param server: where to send the request
:param id_short_path: ID-short path of the element, such as ``Motor.Speed``
:param level: depth of the returned paths
:returns: the paths
:implements GetSubmodelElementByPath-Path: ``GET /submodel/submodel-elements/{idShortPath}/$path``
"""
request = _build_get_submodel_element_by_path_path(
server.base_url, id_short_path, level=level
)
response = http.send(server.transport, request)
return _parse_get_submodel_element_by_path_path(response)
def _build_get_file_by_path(base_url: str, id_short_path: str) -> http.Request:
"""Build the request of ``GetFileByPath``."""
return _submodel_operations.build_get_file_by_path(
base_url, _SUBMODEL_PATH, id_short_path
)
[docs]
def get_file_by_path(
server: http.Server, id_short_path: str
) -> ContextManager[http.Download]:
"""
Download the content of the file element at the given ID-short path.
The content is streamed, so use the result as a context manager:
.. code-block:: python
with submodel.get_file_by_path(server, "Documentation.Manual") as download:
with open("manual.pdf", "wb") as fid:
download.write_to(fid)
The connection is released when the context is exited. If the file element
refers to an external resource, the server may redirect to it; whether
the redirect is followed is up to the transport.
:param server: where to send the request
:param id_short_path: ID-short path of the file element, such as ``Manual``
:returns: a context manager which gives the download
:raises: :py:class:`~aas_client3_2.errors.MethodNotAllowed` if the element is
not a file element
:implements GetFileByPath: ``GET /submodel/submodel-elements/{idShortPath}/attachment``
"""
request = _build_get_file_by_path(server.base_url, id_short_path)
return _files.download(server, request, {200})
def _build_put_file_by_path(
base_url: str,
id_short_path: str,
content: Union[bytes, IO[bytes]],
*,
file_name: Optional[str] = None,
content_type: Optional[str] = None,
) -> http.Request:
"""Build the request of ``PutFileByPath``."""
return _submodel_operations.build_put_file_by_path(
base_url,
_SUBMODEL_PATH,
id_short_path,
content,
file_name=file_name,
content_type=content_type,
)
def _parse_put_file_by_path(response: http.Response) -> None:
"""Parse the response of ``PutFileByPath``."""
return _submodel_operations.parse_put_file_by_path(response)
[docs]
def put_file_by_path(
server: http.Server,
id_short_path: str,
content: Union[bytes, IO[bytes]],
*,
file_name: Optional[str] = None,
content_type: Optional[str] = None,
) -> None:
"""
Upload the content of the file element at the given ID-short path.
The content is either ``bytes``, or a binary file object which is readable and
seekable; it is read from its current position to its end, in chunks, so that
a large file is never held in memory. The file object must not be modified during
the upload.
:param server: where to send the request
:param id_short_path: ID-short path of the file element, such as ``Manual``
:param content: content of the file
:param file_name: if set, the name of the file
:param content_type:
if set, the content type of the file, such as ``application/pdf``,
otherwise ``application/octet-stream``. The servers may take it as
the new content type of the file element.
:raises: :py:class:`TypeError` before sending anything if the content is neither
bytes nor a binary file object; :py:class:`ValueError` before sending anything
if the content can not be read again, or if the file name or the content type
contain double quotes, backslashes or control characters;
:py:class:`~aas_client3_2.errors.MethodNotAllowed` if the element is not a file
element
:implements PutFileByPath: ``PUT /submodel/submodel-elements/{idShortPath}/attachment``
"""
request = _build_put_file_by_path(
server.base_url,
id_short_path,
content,
file_name=file_name,
content_type=content_type,
)
response = http.send(server.transport, request)
_parse_put_file_by_path(response)
def _build_delete_file_by_path(base_url: str, id_short_path: str) -> http.Request:
"""Build the request of ``DeleteFileByPath``."""
return _submodel_operations.build_delete_file_by_path(
base_url, _SUBMODEL_PATH, id_short_path
)
def _parse_delete_file_by_path(response: http.Response) -> None:
"""Parse the response of ``DeleteFileByPath``."""
return _submodel_operations.parse_delete_file_by_path(response)
[docs]
def delete_file_by_path(server: http.Server, id_short_path: str) -> None:
"""
Delete the content of the file element at the given ID-short path.
The file element itself stays.
:param server: where to send the request
:param id_short_path: ID-short path of the file element, such as ``Manual``
:implements DeleteFileByPath: ``DELETE /submodel/submodel-elements/{idShortPath}/attachment``
"""
request = _build_delete_file_by_path(server.base_url, id_short_path)
response = http.send(server.transport, request)
_parse_delete_file_by_path(response)
def _build_invoke_operation(
base_url: str,
id_short_path: str,
operation_request: aas_types.OperationRequest,
) -> http.Request:
"""Build the request of ``InvokeOperation``."""
return _submodel_operations.build_invoke_operation(
base_url, _SUBMODEL_PATH, id_short_path, operation_request
)
def _parse_invoke_operation(response: http.Response) -> aas_types.OperationResult:
"""Parse the response of ``InvokeOperation``."""
return _submodel_operations.parse_invoke_operation(response)
[docs]
def invoke_operation(
server: http.Server,
id_short_path: str,
operation_request: aas_types.OperationRequest,
) -> aas_types.OperationResult:
"""
Invoke the operation at the given ID-short path, and wait for its result.
The connection stays open until the server has the result. Set
``client_timeout_duration`` of :paramref:`operation_request`, such as ``PT30S``,
to tell the server how long the client waits; the timeout of the transport
should be longer.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param operation_request: the input and in-output arguments
:returns: the result of the operation; check its ``execution_state`` and
``success``, since a failed operation still gives a result
:raises: :py:class:`~aas_client3_2.errors.MethodNotAllowed` if the element is
not an operation
:implements InvokeOperation: ``POST /submodel/submodel-elements/{idShortPath}/invoke``
"""
request = _build_invoke_operation(server.base_url, id_short_path, operation_request)
response = http.send(server.transport, request)
return _parse_invoke_operation(response)
def _build_invoke_operation_sync_value_only(
base_url: str,
id_short_path: str,
operation_request: aas_types.OperationRequestValueOnly,
) -> http.Request:
"""Build the request of ``InvokeOperationSync-ValueOnly``."""
return _submodel_operations.build_invoke_operation_value_only(
base_url, _SUBMODEL_PATH, id_short_path, operation_request
)
def _parse_invoke_operation_sync_value_only(
response: http.Response,
) -> aas_types.OperationResultValueOnly:
"""Parse the response of ``InvokeOperationSync-ValueOnly``."""
return _submodel_operations.parse_invoke_operation_value_only(response)
[docs]
def invoke_operation_sync_value_only(
server: http.Server,
id_short_path: str,
operation_request: aas_types.OperationRequestValueOnly,
) -> aas_types.OperationResultValueOnly:
"""
Invoke the operation with the arguments in Value-Only, and wait for its result.
The arguments map the ID-shorts of the variables to their values, such as
``{"speed": 5}``; the numbers in the result are floats, as everywhere in
Value-Only, since JSON knows a single numeric type. The value types of
the variables tell which of the numbers are integers.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param operation_request: the input and in-output arguments
:returns: the result of the operation
:raises: :py:class:`~aas_client3_2.errors.MethodNotAllowed` if the element is
not an operation
:implements InvokeOperationSync-ValueOnly: ``POST /submodel/submodel-elements/{idShortPath}/invoke/$value``
"""
request = _build_invoke_operation_sync_value_only(
server.base_url, id_short_path, operation_request
)
response = http.send(server.transport, request)
return _parse_invoke_operation_sync_value_only(response)
def _build_invoke_operation_async(
base_url: str,
id_short_path: str,
operation_request: aas_types.OperationRequestAsync,
) -> http.Request:
"""Build the request of ``InvokeOperationAsync``."""
return _submodel_operations.build_invoke_operation_async(
base_url, _SUBMODEL_PATH, id_short_path, operation_request
)
def _parse_invoke_operation_async(response: http.Response) -> aas_types.OperationHandle:
"""Parse the response of ``InvokeOperationAsync``."""
return _submodel_operations.parse_invoke_operation_async(response)
[docs]
def invoke_operation_async(
server: http.Server,
id_short_path: str,
operation_request: aas_types.OperationRequestAsync,
) -> aas_types.OperationHandle:
"""
Start the operation at the given ID-short path on the server, and return at once.
Poll :py:func:`get_operation_async_status` with the handle until the operation
is finished, and then fetch :py:func:`get_operation_async_result`:
.. code-block:: python
handle = submodel.invoke_operation_async(
server,
"Control.Start",
aas_types.OperationRequestAsync(client_timeout_duration="PT60S"),
)
while True:
status = submodel.get_operation_async_status(
server, "Control.Start", handle.handle_id
)
if status.execution_state not in (
aas_types.ExecutionState.INITIATED,
aas_types.ExecutionState.RUNNING,
):
break
time.sleep(1)
result = submodel.get_operation_async_result(
server, "Control.Start", handle.handle_id
)
Note that the server may accept an invocation which fails later. The failure
then shows in the result.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param operation_request: the arguments, and how long the client waits at most
:returns: the handle of the invocation, as given in the header ``Location``
:raises: :py:class:`~aas_client3_2.errors.MethodNotAllowed` if the element is
not an operation
:implements InvokeOperationAsync: ``POST /submodel/submodel-elements/{idShortPath}/invoke-async``
"""
request = _build_invoke_operation_async(
server.base_url, id_short_path, operation_request
)
response = http.send(server.transport, request)
return _parse_invoke_operation_async(response)
def _build_invoke_operation_async_value_only(
base_url: str,
id_short_path: str,
operation_request: aas_types.OperationRequestAsyncValueOnly,
) -> http.Request:
"""Build the request of ``InvokeOperationAsync-ValueOnly``."""
return _submodel_operations.build_invoke_operation_async_value_only(
base_url, _SUBMODEL_PATH, id_short_path, operation_request
)
def _parse_invoke_operation_async_value_only(
response: http.Response,
) -> aas_types.OperationHandle:
"""Parse the response of ``InvokeOperationAsync-ValueOnly``."""
return _submodel_operations.parse_invoke_operation_async_value_only(response)
[docs]
def invoke_operation_async_value_only(
server: http.Server,
id_short_path: str,
operation_request: aas_types.OperationRequestAsyncValueOnly,
) -> aas_types.OperationHandle:
"""
Start the operation with the arguments in Value-Only, and return at once.
See :py:func:`invoke_operation_async` for how to poll the status; fetch
the result with :py:func:`get_operation_async_result_value_only`.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param operation_request: the arguments, and how long the client waits at most
:returns: the handle of the invocation, as given in the header ``Location``
:implements InvokeOperationAsync-ValueOnly: ``POST /submodel/submodel-elements/{idShortPath}/invoke-async/$value``
"""
request = _build_invoke_operation_async_value_only(
server.base_url, id_short_path, operation_request
)
response = http.send(server.transport, request)
return _parse_invoke_operation_async_value_only(response)
def _build_get_operation_async_status(
base_url: str, id_short_path: str, handle_id: str
) -> http.Request:
"""Build the request of ``GetOperationAsyncStatus``."""
return _submodel_operations.build_get_operation_async_status(
base_url, _SUBMODEL_PATH, id_short_path, handle_id
)
def _parse_get_operation_async_status(
response: http.Response,
) -> Union[aas_types.BaseOperationResult, str]:
"""Parse the response of ``GetOperationAsyncStatus``.
:returns: the status, or the location of the result if the transport did not
follow the redirect to it"""
return _submodel_operations.parse_get_operation_async_status(response)
[docs]
def get_operation_async_status(
server: http.Server, id_short_path: str, handle_id: str
) -> aas_types.BaseOperationResult:
"""
Get the status of the asynchronous invocation with the given handle.
While the operation runs, the server gives the status. Once it is finished,
the server redirects to the result. The transport usually follows the redirect
(the one based on `requests` does); if not, the client follows it, but only to
the origin of the server. Either way, the status is taken from the result, so
its ``execution_state`` tells whether the operation completed, failed or timed
out.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param handle_id: handle as given by :py:func:`invoke_operation_async`
:returns: the status of the invocation
:raises: :py:class:`~aas_client3_2.errors.NotFound` if there is no such
invocation; :py:class:`~aas_client3_2.errors.UnexpectedResponse` if
the server redirects to another origin
:implements GetOperationAsyncStatus: ``GET /submodel/submodel-elements/{idShortPath}/operation-status/{handleId}``
"""
request = _build_get_operation_async_status(
server.base_url, id_short_path, handle_id
)
response = http.send(server.transport, request)
status = _parse_get_operation_async_status(response)
if not isinstance(status, str):
return status
try:
url = _invocation.resolve_result_url(server.base_url, request.url, status)
except ValueError as exception:
raise errors.UnexpectedResponse(response, exception) from exception
followed = http.send(server.transport, _invocation.build_follow_request(url))
return _invocation.parse_followed(followed)
def _build_get_operation_async_result(
base_url: str, id_short_path: str, handle_id: str
) -> http.Request:
"""Build the request of ``GetOperationAsyncResult``."""
return _submodel_operations.build_get_operation_async_result(
base_url, _SUBMODEL_PATH, id_short_path, handle_id
)
def _parse_get_operation_async_result(
response: http.Response,
) -> aas_types.OperationResult:
"""Parse the response of ``GetOperationAsyncResult``."""
return _submodel_operations.parse_get_operation_async_result(response)
[docs]
def get_operation_async_result(
server: http.Server, id_short_path: str, handle_id: str
) -> aas_types.OperationResult:
"""
Get the result of the finished asynchronous invocation with the given handle.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param handle_id: handle as given by :py:func:`invoke_operation_async`
:returns: the result of the operation; a failed operation still gives a result,
with ``execution_state`` set to ``Failed``
:raises: :py:class:`~aas_client3_2.errors.NotFound` if there is no such
invocation
:implements GetOperationAsyncResult: ``GET /submodel/submodel-elements/{idShortPath}/operation-results/{handleId}``
"""
request = _build_get_operation_async_result(
server.base_url, id_short_path, handle_id
)
response = http.send(server.transport, request)
return _parse_get_operation_async_result(response)
def _build_get_operation_async_result_value_only(
base_url: str, id_short_path: str, handle_id: str
) -> http.Request:
"""Build the request of ``GetOperationAsyncResult-ValueOnly``."""
return _submodel_operations.build_get_operation_async_result_value_only(
base_url, _SUBMODEL_PATH, id_short_path, handle_id
)
def _parse_get_operation_async_result_value_only(
response: http.Response,
) -> aas_types.OperationResultValueOnly:
"""Parse the response of ``GetOperationAsyncResult-ValueOnly``."""
return _submodel_operations.parse_get_operation_async_result_value_only(response)
[docs]
def get_operation_async_result_value_only(
server: http.Server, id_short_path: str, handle_id: str
) -> aas_types.OperationResultValueOnly:
"""
Get the result of the finished asynchronous invocation in Value-Only.
:param server: where to send the request
:param id_short_path: ID-short path of the operation, such as ``Control.Start``
:param handle_id: handle as given by :py:func:`invoke_operation_async` or
:py:func:`invoke_operation_async_value_only`
:returns: the result of the operation
:raises: :py:class:`~aas_client3_2.errors.NotFound` if there is no such
invocation
:implements GetOperationAsyncResult-ValueOnly: ``GET /submodel/submodel-elements/{idShortPath}/operation-results/{handleId}/$value``
"""
request = _build_get_operation_async_result_value_only(
server.base_url, id_short_path, handle_id
)
response = http.send(server.transport, request)
return _parse_get_operation_async_result_value_only(response)