|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from collections.abc import Sequence |
| 5 | +from typing import TYPE_CHECKING, ClassVar, Literal |
| 6 | + |
| 7 | +from cognite.client._api_client import APIClient |
| 8 | +from cognite.client.data_classes.data_modeling.records import RecordId, RecordIdSequence |
| 9 | +from cognite.client.utils._concurrency import RecordsConcurrencyOperation |
| 10 | +from cognite.client.utils._experimental import FeaturePreviewWarning |
| 11 | +from cognite.client.utils._url import interpolate_and_url_encode |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from cognite.client import AsyncCogniteClient |
| 15 | + from cognite.client.config import ClientConfig |
| 16 | + |
| 17 | + |
| 18 | +class RecordsAPI(APIClient): |
| 19 | + def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: AsyncCogniteClient) -> None: |
| 20 | + super().__init__(config, api_version, cognite_client) |
| 21 | + self._warning = FeaturePreviewWarning( |
| 22 | + api_maturity="General Availability", sdk_maturity="alpha", feature_name="Records" |
| 23 | + ) |
| 24 | + |
| 25 | + _OPERATION_TO_RATE_LIMIT: ClassVar[dict[str, RecordsConcurrencyOperation]] = { |
| 26 | + "write": RecordsConcurrencyOperation.WRITE, |
| 27 | + "delete": RecordsConcurrencyOperation.WRITE, |
| 28 | + } |
| 29 | + |
| 30 | + def _get_semaphore(self, operation: Literal["write", "delete"]) -> asyncio.BoundedSemaphore: |
| 31 | + from cognite.client import global_config |
| 32 | + |
| 33 | + return global_config.concurrency_settings.records._semaphore_factory( |
| 34 | + self._OPERATION_TO_RATE_LIMIT[operation], project=self._cognite_client.config.project |
| 35 | + ) |
| 36 | + |
| 37 | + def _records_url(self, stream_id: str, suffix: str = "") -> str: |
| 38 | + return interpolate_and_url_encode("/streams/{}/records{}", stream_id, suffix) |
| 39 | + |
| 40 | + async def delete( |
| 41 | + self, |
| 42 | + items: RecordId | Sequence[RecordId], |
| 43 | + *, |
| 44 | + stream_id: str, |
| 45 | + ignore_unknown_ids: Literal[True] = True, |
| 46 | + ) -> None: |
| 47 | + """`Delete records from a stream <https://api-docs.cognite.com/20230101/tag/Records/operation/deleteRecords>`_. |
| 48 | +
|
| 49 | + Only valid for mutable streams (returns 422 on immutable). Unknown |
| 50 | + ``space + externalId`` pairs are silently ignored. |
| 51 | +
|
| 52 | + Args: |
| 53 | + items (RecordId | Sequence[RecordId]): Records to delete. |
| 54 | + stream_id (str): External ID of the stream to delete from. |
| 55 | + ignore_unknown_ids (Literal[True]): Currently only True is supported |
| 56 | +
|
| 57 | + Examples: |
| 58 | +
|
| 59 | + Delete records: |
| 60 | +
|
| 61 | + >>> from cognite.client import CogniteClient |
| 62 | + >>> from cognite.client.data_classes.data_modeling.records import RecordId |
| 63 | + >>> client = CogniteClient() |
| 64 | + >>> client.data_modeling.records.delete( |
| 65 | + ... stream_id="my-stream", |
| 66 | + ... items=[ |
| 67 | + ... RecordId(space="my-space", external_id="rec-1"), |
| 68 | + ... RecordId(space="my-space", external_id="rec-2"), |
| 69 | + ... ], |
| 70 | + ... ) |
| 71 | + """ |
| 72 | + self._warning.warn() |
| 73 | + await self._delete_multiple( |
| 74 | + identifiers=RecordIdSequence.load(items), |
| 75 | + wrap_ids=True, |
| 76 | + resource_path=self._records_url(stream_id), |
| 77 | + ) |
0 commit comments