# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT
"""
Implement the operations of the Asset Administration Shell API.
The Asset Administration Shell API is the interface of a single shell. Its paths
are relative to the endpoint of the shell, as a registry lists it in
``protocolInformation.href`` of a descriptor with the interface ``AAS-3.2``. Hence
the base URL of the :py:class:`~aas_client3_2.http.Server` is the endpoint itself,
such as ``https://example.com/shells/dXJuOmV4YW1wbGU6YWFzOjE``, and the paths are
appended without the prefix ``/aas`` of the OpenAPI. Part 2 says that the client just
uses the endpoint as provided in the registry, so that it can not tell a single shell
from a repository, and Eclipse BaSyx Go components serve the shell under it, too.
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.jsonization as aas_jsonization
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 _json
from aas_client3_2 import _query
from aas_client3_2 import _submodel_operations
from aas_client3_2 import errors
from aas_client3_2 import http
#: Path of the shell relative to its endpoint, which is the shell itself
_SHELL_PATH: Final[str] = ""
#: Path of the thumbnail relative to the endpoint of the shell
_THUMBNAIL_PATH: Final[str] = "/asset-information/thumbnail"
def _submodel_path(submodel_identifier: str) -> str:
"""Build the path of the submodel through the shell, relative to its endpoint."""
return "/submodels/" + http.encode_base64url(submodel_identifier)
def _build_get_asset_administration_shell(base_url: str) -> http.Request:
"""Build the request of ``GetAssetAdministrationShell``."""
return _json.build_request("GET", http.build_url(base_url, _SHELL_PATH))
def _parse_get_asset_administration_shell(
response: http.Response,
) -> aas_types.AssetAdministrationShell:
"""Parse the response of ``GetAssetAdministrationShell``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(
response, aas_jsonization.asset_administration_shell_from_jsonable
)
[docs]
def get_asset_administration_shell(
server: http.Server,
) -> aas_types.AssetAdministrationShell:
"""
Get the Asset Administration Shell.
:param server: where to send the request
:returns: the shell
:implements GetAssetAdministrationShell: ``GET /aas``
"""
request = _build_get_asset_administration_shell(server.base_url)
response = http.send(server.transport, request)
return _parse_get_asset_administration_shell(response)
def _build_put_asset_administration_shell(
base_url: str,
asset_administration_shell: aas_types.AssetAdministrationShell,
) -> http.Request:
"""Build the request of ``PutAssetAdministrationShell``."""
return _json.build_request(
"PUT",
http.build_url(base_url, _SHELL_PATH),
asset_administration_shell,
)
def _parse_put_asset_administration_shell(response: http.Response) -> None:
"""
Parse the response of ``PutAssetAdministrationShell``.
The server responds with 201 and the shell if it created the shell, or with
204 if it updated an existing one. We do not parse the shell, since it is
the one which the users sent.
"""
errors.raise_for_unexpected_status(response, {201, 204})
[docs]
def put_asset_administration_shell(
server: http.Server,
asset_administration_shell: aas_types.AssetAdministrationShell,
) -> None:
"""
Create or replace the Asset Administration Shell.
:param server: where to send the request
:param asset_administration_shell: the shell to be stored
:implements PutAssetAdministrationShell: ``PUT /aas``
"""
request = _build_put_asset_administration_shell(
server.base_url, asset_administration_shell
)
response = http.send(server.transport, request)
_parse_put_asset_administration_shell(response)
def _build_get_asset_administration_shell_reference(base_url: str) -> http.Request:
"""Build the request of ``GetAssetAdministrationShell-Reference``."""
return _json.build_request(
"GET", http.build_url(base_url, _SHELL_PATH + "/$reference")
)
def _parse_get_asset_administration_shell_reference(
response: http.Response,
) -> aas_types.Reference:
"""Parse the response of ``GetAssetAdministrationShell-Reference``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, aas_jsonization.reference_from_jsonable)
[docs]
def get_asset_administration_shell_reference(
server: http.Server,
) -> aas_types.Reference:
"""
Get a reference to the Asset Administration Shell.
:param server: where to send the request
:returns: the reference to the shell
:implements GetAssetAdministrationShell-Reference: ``GET /aas/$reference``
"""
request = _build_get_asset_administration_shell_reference(server.base_url)
response = http.send(server.transport, request)
return _parse_get_asset_administration_shell_reference(response)
def _build_get_asset_information(base_url: str) -> http.Request:
"""Build the request of ``GetAssetInformation``."""
return _json.build_request(
"GET",
http.build_url(base_url, _SHELL_PATH + "/asset-information"),
)
def _parse_get_asset_information(
response: http.Response,
) -> aas_types.AssetInformation:
"""Parse the response of ``GetAssetInformation``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, aas_jsonization.asset_information_from_jsonable)
def _build_put_asset_information(
base_url: str, asset_information: aas_types.AssetInformation
) -> http.Request:
"""Build the request of ``PutAssetInformation``."""
return _json.build_request(
"PUT",
http.build_url(base_url, _SHELL_PATH + "/asset-information"),
asset_information,
)
def _parse_put_asset_information(response: http.Response) -> None:
"""Parse the response of ``PutAssetInformation``."""
errors.raise_for_unexpected_status(response, {204})
def _build_get_all_submodel_references(
base_url: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelReferences``."""
return _json.build_request(
"GET",
http.build_url(
base_url,
_SHELL_PATH + "/submodel-refs",
_query.page(limit, cursor),
),
)
def _parse_get_all_submodel_references(
response: http.Response,
) -> aas_types.GetReferencesResult:
"""Parse the response of ``GetAllSubmodelReferences``."""
errors.raise_for_unexpected_status(response, {200})
return _json.parse(response, aas_jsonization.get_references_result_from_jsonable)
[docs]
def get_all_submodel_references(
server: http.Server,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> aas_types.GetReferencesResult:
"""
List a page of the submodel references of the Asset Administration Shell.
This is not to be confused with ``get_all_submodels_reference`` of the Submodel
Repository API, which lists a reference for each submodel in a repository.
: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 submodel references
:implements GetAllSubmodelReferences: ``GET /aas/submodel-refs``
"""
request = _build_get_all_submodel_references(
server.base_url, limit=limit, cursor=cursor
)
response = http.send(server.transport, request)
return _parse_get_all_submodel_references(response)
def _build_post_submodel_reference(
base_url: str, submodel_reference: aas_types.Reference
) -> http.Request:
"""Build the request of ``PostSubmodelReference``."""
return _json.build_request(
"POST",
http.build_url(base_url, _SHELL_PATH + "/submodel-refs"),
submodel_reference,
)
def _parse_post_submodel_reference(response: http.Response) -> aas_types.Reference:
"""Parse the response of ``PostSubmodelReference``."""
errors.raise_for_unexpected_status(response, {201})
return _json.parse(response, aas_jsonization.reference_from_jsonable)
[docs]
def post_submodel_reference(
server: http.Server, submodel_reference: aas_types.Reference
) -> aas_types.Reference:
"""
Add a submodel reference to the Asset Administration Shell.
:param server: where to send the request
:param submodel_reference: reference to the submodel
:returns: the created submodel reference, as stored by the server
:implements PostSubmodelReference: ``POST /aas/submodel-refs``
"""
request = _build_post_submodel_reference(server.base_url, submodel_reference)
response = http.send(server.transport, request)
return _parse_post_submodel_reference(response)
def _build_delete_submodel_reference(
base_url: str, submodel_identifier: str
) -> http.Request:
"""Build the request of ``DeleteSubmodelReference``."""
return _json.build_request(
"DELETE",
http.build_url(
base_url,
_SHELL_PATH
+ "/submodel-refs/"
+ http.encode_base64url(submodel_identifier),
),
)
def _parse_delete_submodel_reference(response: http.Response) -> None:
"""Parse the response of ``DeleteSubmodelReference``."""
errors.raise_for_unexpected_status(response, {204})
[docs]
def delete_submodel_reference(server: http.Server, submodel_identifier: str) -> None:
"""
Remove the reference to the submodel from the Asset Administration Shell.
The submodel itself is not deleted.
:param server: where to send the request
:param submodel_identifier: identifier of the referenced submodel
:implements DeleteSubmodelReference: ``DELETE /aas/submodel-refs/{submodelIdentifier}``
"""
request = _build_delete_submodel_reference(server.base_url, submodel_identifier)
response = http.send(server.transport, request)
_parse_delete_submodel_reference(response)
def _build_get_thumbnail(base_url: str) -> http.Request:
"""Build the request of ``GetThumbnail``."""
return _files.build_download_request(http.build_url(base_url, _THUMBNAIL_PATH))
[docs]
def get_thumbnail(server: http.Server) -> ContextManager[http.Download]:
"""
Download the thumbnail of the asset of the Asset Administration Shell.
The content is streamed, so use the result as a context manager:
.. code-block:: python
with aas.get_thumbnail(server) as download:
with open("thumbnail.png", "wb") as fid:
download.write_to(fid)
The connection is released when the context is exited. If the thumbnail 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
:returns: a context manager which gives the download
:raises: :py:class:`~aas_client3_2.errors.NotFound` if there is no such
Asset Administration Shell, or it has no thumbnail
:implements GetThumbnail: ``GET /aas/asset-information/thumbnail``
"""
request = _build_get_thumbnail(server.base_url)
return _files.download(server, request, {200})
def _build_put_thumbnail(
base_url: str,
content: Union[bytes, IO[bytes]],
*,
file_name: Optional[str] = None,
content_type: Optional[str] = None,
) -> http.Request:
"""Build the request of ``PutThumbnail``."""
return _files.build_upload_request(
"PUT",
http.build_url(base_url, _THUMBNAIL_PATH),
content,
file_name,
content_type,
)
def _parse_put_thumbnail(response: http.Response) -> None:
"""Parse the response of ``PutThumbnail``."""
errors.raise_for_unexpected_status(response, {204})
[docs]
def put_thumbnail(
server: http.Server,
content: Union[bytes, IO[bytes]],
*,
file_name: Optional[str] = None,
content_type: Optional[str] = None,
) -> None:
"""
Upload the thumbnail of the asset of the Asset Administration Shell.
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. It must be seekable so that the client
knows the length of the body up-front, and can send the content again, for
example, after a refreshed token. The file object must not be modified during
the upload.
:param server: where to send the request
:param content: content of the thumbnail
:param file_name: if set, the name of the file, such as ``thumbnail.png``
:param content_type:
if set, the content type of the thumbnail, such as ``image/png``,
otherwise ``application/octet-stream``. The servers may take it as
the new content type of the thumbnail.
: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
:implements PutThumbnail: ``PUT /aas/asset-information/thumbnail``
"""
request = _build_put_thumbnail(
server.base_url,
content,
file_name=file_name,
content_type=content_type,
)
response = http.send(server.transport, request)
_parse_put_thumbnail(response)
def _build_delete_thumbnail(base_url: str) -> http.Request:
"""Build the request of ``DeleteThumbnail``."""
return _json.build_request("DELETE", http.build_url(base_url, _THUMBNAIL_PATH))
def _parse_delete_thumbnail(response: http.Response) -> None:
"""Parse the response of ``DeleteThumbnail``."""
# NOTE (mristin):
# The OpenAPI gives 204, and still 200 as deprecated.
errors.raise_for_unexpected_status(response, {200, 204})
[docs]
def delete_thumbnail(server: http.Server) -> None:
"""
Delete the thumbnail of the asset of the Asset Administration Shell.
:param server: where to send the request
:implements DeleteThumbnail: ``DELETE /aas/asset-information/thumbnail``
"""
request = _build_delete_thumbnail(server.base_url)
response = http.send(server.transport, request)
_parse_delete_thumbnail(response)
def _build_get_submodel_by_id(
base_url: str,
submodel_identifier: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelById_AAS``."""
return _submodel_operations.build_get_submodel_by_id(
base_url,
_submodel_path(submodel_identifier),
level=level,
extent=extent,
)
def _parse_get_submodel_by_id(response: http.Response) -> aas_types.Submodel:
"""Parse the response of ``GetSubmodelById_AAS``."""
return _submodel_operations.parse_get_submodel_by_id(response)
[docs]
def get_submodel_by_id(
server: http.Server,
submodel_identifier: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.Submodel:
"""
Get the submodel with the given identifier.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param level: depth of the returned submodel
:param extent: whether to include the values of blobs
:returns: the submodel
:implements GetSubmodelById_AAS: ``GET /aas/submodels/{submodelIdentifier}``
"""
request = _build_get_submodel_by_id(
server.base_url, submodel_identifier, level=level, extent=extent
)
response = http.send(server.transport, request)
return _parse_get_submodel_by_id(response)
def _build_put_submodel_by_id(
base_url: str,
submodel_identifier: str,
submodel: aas_types.Submodel,
) -> http.Request:
"""Build the request of ``PutSubmodelById_AAS``."""
return _submodel_operations.build_put_submodel_by_id(
base_url, _submodel_path(submodel_identifier), submodel
)
def _parse_put_submodel_by_id(response: http.Response) -> None:
"""
Parse the response of ``PutSubmodelById_AAS``.
Unlike in the Asset Administration Shell 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_by_id(
server: http.Server,
submodel_identifier: str,
submodel: aas_types.Submodel,
) -> None:
"""
Replace the submodel with the given identifier.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param submodel: the submodel to be stored
:implements PutSubmodelById_AAS: ``PUT /aas/submodels/{submodelIdentifier}``
"""
request = _build_put_submodel_by_id(server.base_url, submodel_identifier, submodel)
response = http.send(server.transport, request)
_parse_put_submodel_by_id(response)
def _build_patch_submodel_by_id(
base_url: str,
submodel_identifier: str,
submodel: aas_types.Submodel,
) -> http.Request:
"""Build the request of ``PatchSubmodelById_AAS``."""
return _submodel_operations.build_patch_submodel_by_id(
base_url, _submodel_path(submodel_identifier), submodel
)
def _parse_patch_submodel_by_id(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodelById_AAS``."""
return _submodel_operations.parse_patch_submodel_by_id(response)
[docs]
def patch_submodel_by_id(
server: http.Server,
submodel_identifier: str,
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_identifier: identifier of the submodel
:param submodel: the submodel with the updates
:implements PatchSubmodelById_AAS: ``PATCH /aas/submodels/{submodelIdentifier}``
"""
request = _build_patch_submodel_by_id(
server.base_url, submodel_identifier, submodel
)
response = http.send(server.transport, request)
_parse_patch_submodel_by_id(response)
def _build_delete_submodel_by_id(
base_url: str, submodel_identifier: str
) -> http.Request:
"""Build the request of ``DeleteSubmodelById_AAS``."""
return _submodel_operations.build_delete_submodel_by_id(
base_url, _submodel_path(submodel_identifier)
)
def _parse_delete_submodel_by_id(response: http.Response) -> None:
"""Parse the response of ``DeleteSubmodelById_AAS``."""
return _submodel_operations.parse_delete_submodel_by_id(response)
[docs]
def delete_submodel_by_id(server: http.Server, submodel_identifier: str) -> None:
"""
Delete the submodel with the given identifier.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:implements DeleteSubmodelById_AAS: ``DELETE /aas/submodels/{submodelIdentifier}``
"""
request = _build_delete_submodel_by_id(server.base_url, submodel_identifier)
response = http.send(server.transport, request)
_parse_delete_submodel_by_id(response)
def _build_get_submodel_by_id_metadata(
base_url: str, submodel_identifier: str
) -> http.Request:
"""Build the request of ``GetSubmodelById-Metadata_AAS``."""
return _submodel_operations.build_get_submodel_by_id_metadata(
base_url, _submodel_path(submodel_identifier)
)
def _parse_get_submodel_by_id_metadata(
response: http.Response,
) -> aas_types.SubmodelMetadata:
"""Parse the response of ``GetSubmodelById-Metadata_AAS``."""
return _submodel_operations.parse_get_submodel_by_id_metadata(response)
def _build_patch_submode_by_id_metadata(
base_url: str,
submodel_identifier: str,
submodel_metadata: aas_types.SubmodelMetadata,
) -> http.Request:
"""Build the request of ``PatchSubmodeByIdMetadata_AAS``."""
return _submodel_operations.build_patch_submodel_by_id_metadata(
base_url, _submodel_path(submodel_identifier), submodel_metadata
)
def _parse_patch_submode_by_id_metadata(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodeByIdMetadata_AAS``."""
return _submodel_operations.parse_patch_submodel_by_id_metadata(response)
def _build_get_submodel_by_id_value_only(
base_url: str,
submodel_identifier: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelById-ValueOnly_AAS``."""
return _submodel_operations.build_get_submodel_by_id_value_only(
base_url,
_submodel_path(submodel_identifier),
level=level,
extent=extent,
)
def _parse_get_submodel_by_id_value_only(
response: http.Response,
) -> aas_types.JsonObject:
"""Parse the response of ``GetSubmodelById-ValueOnly_AAS``."""
return _submodel_operations.parse_get_submodel_by_id_value_only(response)
[docs]
def get_submodel_by_id_value_only(
server: http.Server,
submodel_identifier: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> aas_types.JsonObject:
"""
Get the values of the submodel with the given identifier.
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_by_id_metadata`, since the ValueOnly serialization drops
the model types: a range ``{"min": 1, "max": 5}`` looks exactly like
a collection with the properties ``min`` and ``max``.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param level: depth of the returned values
:param extent: whether to include the values of blobs
:returns: the values of the submodel
:implements GetSubmodelById-ValueOnly_AAS: ``GET /aas/submodels/{submodelIdentifier}/$value``
"""
request = _build_get_submodel_by_id_value_only(
server.base_url, submodel_identifier, level=level, extent=extent
)
response = http.send(server.transport, request)
return _parse_get_submodel_by_id_value_only(response)
def _build_patch_submodel_by_id_value_only(
base_url: str,
submodel_identifier: str,
submodel: aas_types.Submodel,
) -> http.Request:
"""Build the request of ``PatchSubmodelById-ValueOnly_AAS``."""
return _submodel_operations.build_patch_submodel_by_id_value_only(
base_url, _submodel_path(submodel_identifier), submodel
)
def _parse_patch_submodel_by_id_value_only(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodelById-ValueOnly_AAS``."""
return _submodel_operations.parse_patch_submodel_by_id_value_only(response)
[docs]
def patch_submodel_by_id_value_only(
server: http.Server,
submodel_identifier: str,
submodel: aas_types.Submodel,
) -> None:
"""
Update the values of the submodel with the given identifier.
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_identifier: identifier of the submodel
: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 PatchSubmodelById-ValueOnly_AAS: ``PATCH /aas/submodels/{submodelIdentifier}/$value``
"""
request = _build_patch_submodel_by_id_value_only(
server.base_url, submodel_identifier, submodel
)
response = http.send(server.transport, request)
_parse_patch_submodel_by_id_value_only(response)
def _build_get_submodel_by_id_reference(
base_url: str, submodel_identifier: str
) -> http.Request:
"""Build the request of ``GetSubmodelById-Reference_AAS``."""
return _submodel_operations.build_get_submodel_by_id_reference(
base_url, _submodel_path(submodel_identifier)
)
def _parse_get_submodel_by_id_reference(response: http.Response) -> aas_types.Reference:
"""Parse the response of ``GetSubmodelById-Reference_AAS``."""
return _submodel_operations.parse_get_submodel_by_id_reference(response)
[docs]
def get_submodel_by_id_reference(
server: http.Server, submodel_identifier: str
) -> aas_types.Reference:
"""
Get a reference to the submodel with the given identifier.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:returns: the reference to the submodel
:implements GetSubmodelById-Reference_AAS: ``GET /aas/submodels/{submodelIdentifier}/$reference``
"""
request = _build_get_submodel_by_id_reference(server.base_url, submodel_identifier)
response = http.send(server.transport, request)
return _parse_get_submodel_by_id_reference(response)
def _build_get_submodel_by_id_path(
base_url: str,
submodel_identifier: str,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelById-Path_AAS``."""
return _submodel_operations.build_get_submodel_by_id_path(
base_url, _submodel_path(submodel_identifier), level=level
)
def _parse_get_submodel_by_id_path(response: http.Response) -> List[str]:
"""Parse the response of ``GetSubmodelById-Path_AAS``."""
return _submodel_operations.parse_get_submodel_by_id_path(response)
[docs]
def get_submodel_by_id_path(
server: http.Server,
submodel_identifier: str,
*,
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 submodel_identifier: identifier of the submodel
:param level: depth of the returned paths
:returns: the paths
:implements GetSubmodelById-Path_AAS: ``GET /aas/submodels/{submodelIdentifier}/$path``
"""
request = _build_get_submodel_by_id_path(
server.base_url, submodel_identifier, level=level
)
response = http.send(server.transport, request)
return _parse_get_submodel_by_id_path(response)
def _build_get_all_submodel_elements(
base_url: str,
submodel_identifier: 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_AAS``."""
return _submodel_operations.build_get_all_submodel_elements(
base_url,
_submodel_path(submodel_identifier),
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_AAS``."""
return _submodel_operations.parse_get_all_submodel_elements(response)
[docs]
def get_all_submodel_elements(
server: http.Server,
submodel_identifier: str,
*,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements``
"""
request = _build_get_all_submodel_elements(
server.base_url,
submodel_identifier,
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_identifier: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PostSubmodelElement_AAS``."""
return _submodel_operations.build_post_submodel_element(
base_url, _submodel_path(submodel_identifier), submodel_element
)
def _parse_post_submodel_element(response: http.Response) -> aas_types.SubmodelElement:
"""Parse the response of ``PostSubmodelElement_AAS``."""
return _submodel_operations.parse_post_submodel_element(response)
[docs]
def post_submodel_element(
server: http.Server,
submodel_identifier: str,
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_identifier: identifier of the submodel
:param submodel_element: the submodel element to be created
:returns: the created submodel element, as stored by the server
:implements PostSubmodelElement_AAS: ``POST /aas/submodels/{submodelIdentifier}/submodel-elements``
"""
request = _build_post_submodel_element(
server.base_url, submodel_identifier, submodel_element
)
response = http.send(server.transport, request)
return _parse_post_submodel_element(response)
def _build_get_all_submodel_elements_metadata(
base_url: str,
submodel_identifier: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElements-Metadata_AAS``."""
return _submodel_operations.build_get_all_submodel_elements_metadata(
base_url,
_submodel_path(submodel_identifier),
limit=limit,
cursor=cursor,
)
def _parse_get_all_submodel_elements_metadata(
response: http.Response,
) -> aas_types.GetSubmodelElementsMetadataResult:
"""Parse the response of ``GetAllSubmodelElements-Metadata_AAS``."""
return _submodel_operations.parse_get_all_submodel_elements_metadata(response)
def _build_get_all_submodel_elements_value_only(
base_url: str,
submodel_identifier: 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_AAS``."""
return _submodel_operations.build_get_all_submodel_elements_value_only(
base_url,
_submodel_path(submodel_identifier),
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_AAS``."""
return _submodel_operations.parse_get_all_submodel_elements_value_only(response)
[docs]
def get_all_submodel_elements_value_only(
server: http.Server,
submodel_identifier: str,
*,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/$value``
"""
request = _build_get_all_submodel_elements_value_only(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElementsReference_AAS``."""
return _submodel_operations.build_get_all_submodel_elements_reference(
base_url,
_submodel_path(submodel_identifier),
limit=limit,
cursor=cursor,
)
def _parse_get_all_submodel_elements_reference(
response: http.Response,
) -> aas_types.GetReferencesResult:
"""Parse the response of ``GetAllSubmodelElementsReference_AAS``."""
return _submodel_operations.parse_get_all_submodel_elements_reference(response)
[docs]
def get_all_submodel_elements_reference(
server: http.Server,
submodel_identifier: str,
*,
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 submodel_identifier: identifier of the submodel
: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 GetAllSubmodelElementsReference_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/$reference``
"""
request = _build_get_all_submodel_elements_reference(
server.base_url, submodel_identifier, 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,
submodel_identifier: str,
*,
limit: Optional[int] = None,
cursor: Optional[str] = None,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetAllSubmodelElementsPath_AAS``."""
return _submodel_operations.build_get_all_submodel_elements_path(
base_url,
_submodel_path(submodel_identifier),
limit=limit,
cursor=cursor,
level=level,
)
def _parse_get_all_submodel_elements_path(
response: http.Response,
) -> aas_types.GetPathItemsResult:
"""Parse the response of ``GetAllSubmodelElementsPath_AAS``."""
return _submodel_operations.parse_get_all_submodel_elements_path(response)
[docs]
def get_all_submodel_elements_path(
server: http.Server,
submodel_identifier: str,
*,
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 submodel_identifier: identifier of the submodel
: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 GetAllSubmodelElementsPath_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/$path``
"""
request = _build_get_all_submodel_elements_path(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
extent: Optional[aas_types.Extent] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath_AAS``."""
return _submodel_operations.build_get_submodel_element_by_path(
base_url,
_submodel_path(submodel_identifier),
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_AAS``."""
return _submodel_operations.parse_get_submodel_element_by_path(response)
[docs]
def get_submodel_element_by_path(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}``
"""
request = _build_get_submodel_element_by_path(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PostSubmodelElementByPath_AAS``."""
return _submodel_operations.build_post_submodel_element_by_path(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
submodel_element,
)
def _parse_post_submodel_element_by_path(
response: http.Response,
) -> aas_types.SubmodelElement:
"""Parse the response of ``PostSubmodelElementByPath_AAS``."""
return _submodel_operations.parse_post_submodel_element_by_path(response)
[docs]
def post_submodel_element_by_path(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``POST /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}``
"""
request = _build_post_submodel_element_by_path(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> http.Request:
"""Build the request of ``PutSubmodelElementByPath_AAS``."""
return _submodel_operations.build_put_submodel_element_by_path(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
submodel_element,
)
def _parse_put_submodel_element_by_path(response: http.Response) -> None:
"""Parse the response of ``PutSubmodelElementByPath_AAS``."""
return _submodel_operations.parse_put_submodel_element_by_path(response)
[docs]
def put_submodel_element_by_path(
server: http.Server,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
) -> None:
"""
Create or replace the submodel element at the given ID-short path.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the element
:param submodel_element: the submodel element to be stored
:implements PutSubmodelElementByPath_AAS: ``PUT /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}``
"""
request = _build_put_submodel_element_by_path(
server.base_url,
submodel_identifier,
id_short_path,
submodel_element,
)
response = http.send(server.transport, request)
_parse_put_submodel_element_by_path(response)
def _build_patch_submodel_element_value_by_path(
base_url: str,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``PatchSubmodelElementValueByPath_AAS``."""
return _submodel_operations.build_patch_submodel_element_by_path(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
submodel_element,
level=level,
)
def _parse_patch_submodel_element_value_by_path(response: http.Response) -> None:
"""Parse the response of ``PatchSubmodelElementValueByPath_AAS``."""
return _submodel_operations.parse_patch_submodel_element_by_path(response)
[docs]
def patch_submodel_element_value_by_path(
server: http.Server,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
*,
level: Optional[aas_types.Level] = None,
) -> 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.
Unlike the Submodel Repository API, the OpenAPI allows both values of
the query parameter ``level`` here.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the element
:param submodel_element: the submodel element with the updates
:param level: depth of the update
:implements PatchSubmodelElementValueByPath_AAS: ``PATCH /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}``
"""
request = _build_patch_submodel_element_value_by_path(
server.base_url,
submodel_identifier,
id_short_path,
submodel_element,
level=level,
)
response = http.send(server.transport, request)
_parse_patch_submodel_element_value_by_path(response)
def _build_delete_submodel_element_by_path(
base_url: str, submodel_identifier: str, id_short_path: str
) -> http.Request:
"""Build the request of ``DeleteSubmodelElementByPath_AAS``."""
return _submodel_operations.build_delete_submodel_element_by_path(
base_url, _submodel_path(submodel_identifier), id_short_path
)
def _parse_delete_submodel_element_by_path(response: http.Response) -> None:
"""Parse the response of ``DeleteSubmodelElementByPath_AAS``."""
return _submodel_operations.parse_delete_submodel_element_by_path(response)
[docs]
def delete_submodel_element_by_path(
server: http.Server,
submodel_identifier: str,
id_short_path: str,
) -> None:
"""
Delete the submodel element at the given ID-short path.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the element
:implements DeleteSubmodelElementByPath_AAS: ``DELETE /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}``
"""
request = _build_delete_submodel_element_by_path(
server.base_url, submodel_identifier, 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, submodel_identifier: str, id_short_path: str
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-Metadata_AAS``."""
return _submodel_operations.build_get_submodel_element_by_path_metadata(
base_url, _submodel_path(submodel_identifier), id_short_path
)
def _parse_get_submodel_element_by_path_metadata(
response: http.Response,
) -> aas_types.SubmodelElementMetadata:
"""Parse the response of ``GetSubmodelElementByPath-Metadata_AAS``."""
return _submodel_operations.parse_get_submodel_element_by_path_metadata(response)
def _build_patch_submodel_element_value_by_path_metadata(
base_url: str,
submodel_identifier: str,
id_short_path: str,
submodel_element_metadata: aas_types.SubmodelElementMetadata,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``PatchSubmodelElementValueByPath-Metadata_AAS``."""
return _submodel_operations.build_patch_submodel_element_by_path_metadata(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
submodel_element_metadata,
level=level,
)
def _parse_patch_submodel_element_value_by_path_metadata(
response: http.Response,
) -> None:
"""Parse the response of ``PatchSubmodelElementValueByPath-Metadata_AAS``."""
return _submodel_operations.parse_patch_submodel_element_by_path_metadata(response)
def _build_get_submodel_element_by_path_value_only(
base_url: str,
submodel_identifier: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-ValueOnly_AAS``."""
return _submodel_operations.build_get_submodel_element_by_path_value_only(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
level=level,
)
def _parse_get_submodel_element_by_path_value_only(
response: http.Response,
) -> aas_types.JsonValue:
"""Parse the response of ``GetSubmodelElementByPath-ValueOnly_AAS``."""
return _submodel_operations.parse_get_submodel_element_by_path_value_only(response)
[docs]
def get_submodel_element_by_path_value_only(
server: http.Server,
submodel_identifier: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = 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 submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the element, such as ``Motor.Speed``
:param level: depth of the returned value
:returns: the value of the submodel element
:implements GetSubmodelElementByPath-ValueOnly_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/$value``
"""
request = _build_get_submodel_element_by_path_value_only(
server.base_url,
submodel_identifier,
id_short_path,
level=level,
)
response = http.send(server.transport, request)
return _parse_get_submodel_element_by_path_value_only(response)
def _build_patch_submodel_element_value_by_path_value_only(
base_url: str,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``PatchSubmodelElementValueByPathValueOnly_AAS``."""
return _submodel_operations.build_patch_submodel_element_by_path_value_only(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
submodel_element,
level=level,
)
def _parse_patch_submodel_element_value_by_path_value_only(
response: http.Response,
) -> None:
"""Parse the response of ``PatchSubmodelElementValueByPathValueOnly_AAS``."""
return _submodel_operations.parse_patch_submodel_element_by_path_value_only(
response
)
[docs]
def patch_submodel_element_value_by_path_value_only(
server: http.Server,
submodel_identifier: str,
id_short_path: str,
submodel_element: aas_types.SubmodelElement,
*,
level: Optional[aas_types.Level] = None,
) -> 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.
Unlike the Submodel Repository API, the OpenAPI allows both values of
the query parameter ``level`` here.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the element
:param submodel_element: the submodel element with the new value
:param level: depth of the update
: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 PatchSubmodelElementValueByPathValueOnly_AAS: ``PATCH /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/$value``
"""
request = _build_patch_submodel_element_value_by_path_value_only(
server.base_url,
submodel_identifier,
id_short_path,
submodel_element,
level=level,
)
response = http.send(server.transport, request)
_parse_patch_submodel_element_value_by_path_value_only(response)
def _build_get_submodel_element_by_path_reference(
base_url: str, submodel_identifier: str, id_short_path: str
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-Reference_AAS``."""
return _submodel_operations.build_get_submodel_element_by_path_reference(
base_url, _submodel_path(submodel_identifier), id_short_path
)
def _parse_get_submodel_element_by_path_reference(
response: http.Response,
) -> aas_types.Reference:
"""Parse the response of ``GetSubmodelElementByPath-Reference_AAS``."""
return _submodel_operations.parse_get_submodel_element_by_path_reference(response)
[docs]
def get_submodel_element_by_path_reference(
server: http.Server,
submodel_identifier: str,
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``.
The deprecated query parameter ``level`` is omitted, since it allows only
the value ``core``.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the element, such as ``Motor.Speed``
:returns: the reference to the submodel element
:implements GetSubmodelElementByPath-Reference_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/$reference``
"""
request = _build_get_submodel_element_by_path_reference(
server.base_url, submodel_identifier, 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,
submodel_identifier: str,
id_short_path: str,
*,
level: Optional[aas_types.Level] = None,
) -> http.Request:
"""Build the request of ``GetSubmodelElementByPath-Path_AAS``."""
return _submodel_operations.build_get_submodel_element_by_path_path(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
level=level,
)
def _parse_get_submodel_element_by_path_path(response: http.Response) -> List[str]:
"""Parse the response of ``GetSubmodelElementByPath-Path_AAS``."""
return _submodel_operations.parse_get_submodel_element_by_path_path(response)
[docs]
def get_submodel_element_by_path_path(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/$path``
"""
request = _build_get_submodel_element_by_path_path(
server.base_url, submodel_identifier, 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, submodel_identifier: str, id_short_path: str
) -> http.Request:
"""Build the request of ``GetFileByPath_AAS``."""
return _submodel_operations.build_get_file_by_path(
base_url, _submodel_path(submodel_identifier), id_short_path
)
[docs]
def get_file_by_path(
server: http.Server,
submodel_identifier: str,
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 aas.get_file_by_path(
server, "urn:example:submodel:1", "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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/attachment``
"""
request = _build_get_file_by_path(
server.base_url, submodel_identifier, id_short_path
)
return _files.download(server, request, {200})
def _build_put_file_by_path(
base_url: str,
submodel_identifier: 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_AAS``."""
return _submodel_operations.build_put_file_by_path(
base_url,
_submodel_path(submodel_identifier),
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_AAS``."""
return _submodel_operations.parse_put_file_by_path(response)
[docs]
def put_file_by_path(
server: http.Server,
submodel_identifier: str,
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. It must be seekable so that the client
knows the length of the body up-front, and can send the content again, for
example, after a refreshed token. The file object must not be modified during
the upload.
:param server: where to send the request
:param submodel_identifier: identifier of the submodel
: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_AAS: ``PUT /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/attachment``
"""
request = _build_put_file_by_path(
server.base_url,
submodel_identifier,
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, submodel_identifier: str, id_short_path: str
) -> http.Request:
"""Build the request of ``DeleteFileByPath_AAS``."""
return _submodel_operations.build_delete_file_by_path(
base_url, _submodel_path(submodel_identifier), id_short_path
)
def _parse_delete_file_by_path(response: http.Response) -> None:
"""Parse the response of ``DeleteFileByPath_AAS``."""
return _submodel_operations.parse_delete_file_by_path(response)
[docs]
def delete_file_by_path(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
:param id_short_path: ID-short path of the file element, such as ``Manual``
:implements DeleteFileByPath_AAS: ``DELETE /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/attachment``
"""
request = _build_delete_file_by_path(
server.base_url, submodel_identifier, id_short_path
)
response = http.send(server.transport, request)
_parse_delete_file_by_path(response)
def _build_invoke_operation_sync(
base_url: str,
submodel_identifier: str,
id_short_path: str,
operation_request: aas_types.OperationRequest,
) -> http.Request:
"""Build the request of ``InvokeOperationSync_AAS``."""
return _submodel_operations.build_invoke_operation(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
operation_request,
)
def _parse_invoke_operation_sync(response: http.Response) -> aas_types.OperationResult:
"""Parse the response of ``InvokeOperationSync_AAS``."""
return _submodel_operations.parse_invoke_operation(response)
[docs]
def invoke_operation_sync(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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 InvokeOperationSync_AAS: ``POST /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/invoke``
"""
request = _build_invoke_operation_sync(
server.base_url,
submodel_identifier,
id_short_path,
operation_request,
)
response = http.send(server.transport, request)
return _parse_invoke_operation_sync(response)
def _build_invoke_operation_sync_value_only(
base_url: str,
submodel_identifier: str,
id_short_path: str,
operation_request: aas_types.OperationRequestValueOnly,
) -> http.Request:
"""Build the request of ``InvokeOperationSync-ValueOnly_AAS``."""
return _submodel_operations.build_invoke_operation_value_only(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
operation_request,
)
def _parse_invoke_operation_sync_value_only(
response: http.Response,
) -> aas_types.OperationResultValueOnly:
"""Parse the response of ``InvokeOperationSync-ValueOnly_AAS``."""
return _submodel_operations.parse_invoke_operation_value_only(response)
[docs]
def invoke_operation_sync_value_only(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``POST /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/invoke/$value``
"""
request = _build_invoke_operation_sync_value_only(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
id_short_path: str,
operation_request: aas_types.OperationRequestAsync,
) -> http.Request:
"""Build the request of ``InvokeOperationAsync_AAS``."""
return _submodel_operations.build_invoke_operation_async(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
operation_request,
)
def _parse_invoke_operation_async(response: http.Response) -> aas_types.OperationHandle:
"""Parse the response of ``InvokeOperationAsync_AAS``."""
return _submodel_operations.parse_invoke_operation_async(response)
[docs]
def invoke_operation_async(
server: http.Server,
submodel_identifier: str,
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 = aas.invoke_operation_async(
server,
"urn:example:submodel:1",
"Control.Start",
aas_types.OperationRequestAsync(client_timeout_duration="PT60S"),
)
while True:
status = aas.get_operation_async_status(
server, "urn:example:submodel:1", "Control.Start", handle.handle_id
)
if status.execution_state not in (
aas_types.ExecutionState.INITIATED,
aas_types.ExecutionState.RUNNING,
):
break
time.sleep(1)
result = aas.get_operation_async_result(
server, "urn:example:submodel:1", "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 submodel_identifier: identifier of the submodel
: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_AAS: ``POST /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/invoke-async``
"""
request = _build_invoke_operation_async(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
id_short_path: str,
operation_request: aas_types.OperationRequestAsyncValueOnly,
) -> http.Request:
"""Build the request of ``InvokeOperationAsync-ValueOnly_AAS``."""
return _submodel_operations.build_invoke_operation_async_value_only(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
operation_request,
)
def _parse_invoke_operation_async_value_only(
response: http.Response,
) -> aas_types.OperationHandle:
"""Parse the response of ``InvokeOperationAsync-ValueOnly_AAS``."""
return _submodel_operations.parse_invoke_operation_async_value_only(response)
[docs]
def invoke_operation_async_value_only(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``POST /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/invoke-async/$value``
"""
request = _build_invoke_operation_async_value_only(
server.base_url,
submodel_identifier,
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,
submodel_identifier: str,
id_short_path: str,
handle_id: str,
) -> http.Request:
"""Build the request of ``GetOperationAsyncStatus_AAS``."""
return _submodel_operations.build_get_operation_async_status(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
handle_id,
)
def _parse_get_operation_async_status(
response: http.Response,
) -> Union[aas_types.BaseOperationResult, str]:
"""Parse the response of ``GetOperationAsyncStatus_AAS``."""
return _submodel_operations.parse_get_operation_async_status(response)
[docs]
def get_operation_async_status(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/operation-status/{handleId}``
"""
request = _build_get_operation_async_status(
server.base_url, submodel_identifier, 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,
submodel_identifier: str,
id_short_path: str,
handle_id: str,
) -> http.Request:
"""Build the request of ``GetOperationAsyncResult_AAS``."""
return _submodel_operations.build_get_operation_async_result(
base_url,
_submodel_path(submodel_identifier),
id_short_path,
handle_id,
)
def _parse_get_operation_async_result(
response: http.Response,
) -> aas_types.OperationResult:
"""Parse the response of ``GetOperationAsyncResult_AAS``."""
return _submodel_operations.parse_get_operation_async_result(response)
[docs]
def get_operation_async_result(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/operation-results/{handleId}``
"""
request = _build_get_operation_async_result(
server.base_url, submodel_identifier, 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,
submodel_identifier: str,
id_short_path: str,
handle_id: str,
) -> http.Request:
"""Build the request of ``GetOperationAsyncResult-ValueOnly_AAS``."""
return _submodel_operations.build_get_operation_async_result_value_only(
base_url,
_submodel_path(submodel_identifier),
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_AAS``."""
return _submodel_operations.parse_get_operation_async_result_value_only(response)
[docs]
def get_operation_async_result_value_only(
server: http.Server,
submodel_identifier: str,
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 submodel_identifier: identifier of the submodel
: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_AAS: ``GET /aas/submodels/{submodelIdentifier}/submodel-elements/{idShortPath}/operation-results/{handleId}/$value``
"""
request = _build_get_operation_async_result_value_only(
server.base_url, submodel_identifier, id_short_path, handle_id
)
response = http.send(server.transport, request)
return _parse_get_operation_async_result_value_only(response)