Source code for aas_client3_2.async_bulk

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

"""
Implement the operations of the Async Bulk Status API and the Async Bulk Result API.

Both registries serve these endpoints for their bulk operations (see
:py:mod:`aas_client3_2.aas_registry` and :py:mod:`aas_client3_2.submodel_registry`),
so the functions take the server of the registry which accepted the bulk request.

Every public function implements exactly one operation of the specification. The
private functions ``_build_*`` and ``_parse_*`` build the request and parse
the response of the operation. They do no I/O, and constitute a semi-public
interface for the testing client, which is released in lockstep with this client.
"""

import dataclasses
from typing import Optional

import aas_core3_2.jsonization as aas_jsonization
import aas_core3_2.types as aas_types

from aas_client3_2 import _bulk
from aas_client3_2 import _invocation
from aas_client3_2 import _json
from aas_client3_2 import errors
from aas_client3_2 import http


[docs] @dataclasses.dataclass(frozen=True) class BulkStatus: """Represent the status of a bulk operation.""" #: Status as given by the server while the bulk operation runs, or None once it #: is finished running: Optional[aas_types.BaseOperationResult] #: Seconds to wait before asking again, if the server says so while the bulk #: operation runs retry_after: Optional[int] = None @property def finished(self) -> bool: """Tell whether the bulk operation is finished, so its result can be fetched.""" return self.running is None
def _parse_retry_after(response: http.Response) -> Optional[int]: """ Parse the header ``Retry-After`` of :paramref:`response`, if any. The OpenAPI defines it as an integer, the number of seconds. :raises: :py:class:`~aas_client3_2.errors.UnexpectedResponse` if the header is not a non-negative integer """ text = http.find_header(response.headers, "Retry-After") if text is None: return None stripped = text.strip() if not stripped.isdigit() or not stripped.isascii(): exception = ValueError( f"Expected the header Retry-After as a non-negative integer of seconds, " f"but got: {text!r}" ) raise errors.UnexpectedResponse(response, exception) return int(stripped) def _build_bulk_get_async_status(base_url: str, handle_id: str) -> http.Request: """ Build the request of ``BulkGetAsyncStatus``. The request must not follow the redirect to the result, since the server may delete the result once it has been fetched (Eclipse BaSyx Go components do), and the users could then not fetch it afterwards. """ request = _json.build_request( "GET", http.build_url( base_url, _bulk.STATUS_SEGMENT + _invocation.encode_handle_id(handle_id) ), ) return dataclasses.replace(request, follow_redirects=False) def _parse_bulk_get_async_status(response: http.Response) -> BulkStatus: """Parse the response of ``BulkGetAsyncStatus``.""" errors.raise_for_unexpected_status(response, {200, 302}) if response.status_code == 302: if http.find_header(response.headers, "Location") is None: exception = ValueError( "Expected the header Location with the result in the redirect, " "but got none" ) raise errors.UnexpectedResponse(response, exception) return BulkStatus(running=None) return BulkStatus( running=_json.parse( response, aas_jsonization.base_operation_result_from_jsonable ), retry_after=_parse_retry_after(response), )
[docs] def bulk_get_async_status(server: http.Server, handle_id: str) -> BulkStatus: """ Get the status of the bulk operation with the given handle. While the bulk operation runs, the server gives its status, and possibly how long to wait before asking again. Once it is finished, the server redirects to the result, and the status is :py:attr:`BulkStatus.finished`; fetch the result with :py:func:`bulk_get_async_result` to learn whether it succeeded. The client does not follow the redirect, and asks the transport not to follow it either, since the server may delete the result once it has been fetched. .. code-block:: python import time handle = aas_registry.bulk_post_asset_administration_shell_descriptor( server, descriptors ) while True: status = async_bulk.bulk_get_async_status(server, handle.handle_id) if status.finished: break time.sleep(status.retry_after if status.retry_after is not None else 1) # Raises errors.BadRequest with the messages if the bulk operation failed. async_bulk.bulk_get_async_result(server, handle.handle_id) :param server: the registry which accepted the bulk request :param handle_id: handle as given by the bulk operation :returns: the status of the bulk operation :raises: :py:class:`~aas_client3_2.errors.NotFound` if there is no such bulk operation; :py:class:`~aas_client3_2.errors.UnexpectedResponse` if the transport followed the redirect anyway :implements BulkGetAsyncStatus: ``GET /bulk/status/{handleId}`` """ request = _build_bulk_get_async_status(server.base_url, handle_id) response = http.send(server.transport, request) return _parse_bulk_get_async_status(response)
def _build_bulk_get_async_result(base_url: str, handle_id: str) -> http.Request: """Build the request of ``BulkGetAsyncResult``.""" return _json.build_request( "GET", http.build_url( base_url, _bulk.RESULT_SEGMENT + _invocation.encode_handle_id(handle_id) ), ) def _parse_bulk_get_async_result(response: http.Response) -> None: """ Parse the response of ``BulkGetAsyncResult``. The server responds with 204 if all the items were processed. If the bulk request or any of its items failed, it responds with 400 and the messages, which raises :py:class:`~aas_client3_2.errors.BadRequest`. """ errors.raise_for_unexpected_status(response, {204})
[docs] def bulk_get_async_result(server: http.Server, handle_id: str) -> None: """ Get the result of the finished bulk operation with the given handle. A bulk operation is atomic: either all of its items are processed, or none of them. Hence a successful result has no content, and this function returns nothing. A failed bulk operation raises :py:class:`~aas_client3_2.errors.BadRequest`, whose :py:attr:`~aas_client3_2.errors.HttpError.result` gives at least one message for every failed item, as far as the server got before it stopped. The server may delete the result once it has been fetched, or after a while, and then gives :py:class:`~aas_client3_2.errors.NotFound`. :param server: the registry which accepted the bulk request :param handle_id: handle as given by the bulk operation :raises: :py:class:`~aas_client3_2.errors.BadRequest` if the bulk operation failed; :py:class:`~aas_client3_2.errors.NotFound` if there is no such result (anymore) :implements BulkGetAsyncResult: ``GET /bulk/result/{handleId}`` """ request = _build_bulk_get_async_result(server.base_url, handle_id) response = http.send(server.transport, request) _parse_bulk_get_async_result(response)