-
Notifications
You must be signed in to change notification settings - Fork 37
feat(streams): Streams API #2534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6829245
add streams api
andersfylling 3225b62
cleanup
andersfylling 193403c
use correct client property during testing
andersfylling b65d523
fix sync import
andersfylling 7efcb0b
fix sync mock client
andersfylling cfbca3f
remove incorrect streams attribute
andersfylling ed6ed65
make resource writeable
andersfylling 5a52db6
fix: set chunking limits for Streams API create and delete operations
andersfylling 1e39c1b
regenerate sync API for Streams after adding chunking limits
andersfylling b25a668
fix: remove unused imports in sync data modeling API init
andersfylling 04f1ec5
Merge branch 'master' into andersfylling/cognite-sdk/streams-api
andersfylling 71c5d9e
Merge branch 'master' into andersfylling/cognite-sdk/streams-api
andersfylling 2c50de3
refactor(streams): clean up docstrings and remove StreamTemplate.version
andersfylling 8cee723
test: remove version field assertions from StreamTemplate test
andersfylling ec2959d
refactor(streams): address review feedback from haakonvt
andersfylling 1b71d9d
address review comments
andersfylling 1e6937c
fix sphinx doc generation
andersfylling 987b7d1
ensure we run tests on docstrings
andersfylling 3695a86
Merge branch 'master' into andersfylling/cognite-sdk/streams-api
andersfylling 934d0e1
remove support for dict type
andersfylling 6027ab1
use _list helper
andersfylling 11ce01c
allow limit args
andersfylling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import TYPE_CHECKING, overload | ||
|
|
||
| from cognite.client._api_client import APIClient | ||
| from cognite.client.data_classes.data_modeling.streams import ( | ||
| Stream, | ||
| StreamList, | ||
| StreamWrite, | ||
| ) | ||
| from cognite.client.utils._experimental import FeaturePreviewWarning | ||
| from cognite.client.utils._identifier import Identifier, IdentifierSequence | ||
| from cognite.client.utils.useful_types import SequenceNotStr | ||
|
|
||
| if TYPE_CHECKING: | ||
| from cognite.client import AsyncCogniteClient | ||
| from cognite.client.config import ClientConfig | ||
|
|
||
|
|
||
| class StreamsAPI(APIClient): | ||
| _RESOURCE_PATH = "/streams" | ||
|
|
||
| def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: AsyncCogniteClient) -> None: | ||
| super().__init__(config, api_version, cognite_client) | ||
| self._CREATE_LIMIT = 1 | ||
| self._DELETE_LIMIT = 1 | ||
| self._warning = FeaturePreviewWarning( | ||
| api_maturity="General Availability", sdk_maturity="alpha", feature_name="Streams" | ||
| ) | ||
|
|
||
| @overload | ||
| async def create(self, items: StreamWrite) -> Stream: ... | ||
|
|
||
| @overload | ||
| async def create(self, items: Sequence[StreamWrite]) -> StreamList: ... | ||
|
|
||
| async def create(self, items: StreamWrite | Sequence[StreamWrite]) -> Stream | StreamList: | ||
| """`Create streams <https://api-docs.cognite.com/20230101/tag/Streams/operation/createStream>`_. | ||
|
|
||
| Args: | ||
| items (StreamWrite | Sequence[StreamWrite]): One or more streams to create. | ||
|
|
||
| Returns: | ||
| Stream | StreamList: The created stream or streams. | ||
|
andersfylling marked this conversation as resolved.
|
||
|
|
||
| Examples: | ||
|
|
||
| Create a single stream from a template: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> from cognite.client.data_classes.data_modeling.streams import ( | ||
| ... StreamWrite, | ||
| ... StreamTemplate, | ||
| ... StreamTemplateWriteSettings, | ||
| ... ) | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> res = client.data_modeling.streams.create( | ||
| ... StreamWrite( | ||
| ... external_id="my-stream", | ||
| ... settings=StreamTemplateWriteSettings( | ||
| ... template=StreamTemplate(name="ImmutableTestStream"), | ||
| ... ), | ||
| ... ) | ||
| ... ) | ||
| """ | ||
| self._warning.warn() | ||
| return await self._create_multiple( | ||
| items=items, | ||
| list_cls=StreamList, | ||
| resource_cls=Stream, | ||
| input_resource_cls=StreamWrite, | ||
| ) | ||
|
|
||
| async def list(self) -> StreamList: | ||
| """`List streams <https://api-docs.cognite.com/20230101/tag/Streams/operation/listStreams>`_. | ||
|
|
||
| Note: | ||
| There is no paging limit parameter: the endpoint returns all streams in the project | ||
| (projects are expected to have few streams). | ||
|
|
||
| Returns: | ||
| StreamList: The streams in the project. | ||
|
andersfylling marked this conversation as resolved.
|
||
|
|
||
| Examples: | ||
|
|
||
| List all streams in the project: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> res = client.data_modeling.streams.list() | ||
| """ | ||
| self._warning.warn() | ||
| return await self._list(method="GET", list_cls=StreamList, resource_cls=Stream) | ||
|
|
||
| async def retrieve(self, external_id: str, include_statistics: bool | None = None) -> Stream | None: | ||
| """`Retrieve a stream <https://api-docs.cognite.com/20230101/tag/Streams/operation/getStream>`_. | ||
|
|
||
| Args: | ||
| external_id (str): External ID of the stream to retrieve. | ||
| include_statistics (bool | None): When ``True``, usage statistics will be returned together | ||
| with stream settings. Computing statistics can be expensive. | ||
|
|
||
| Returns: | ||
| Stream | None: The stream metadata (and optionally statistics), or ``None`` if not found. | ||
|
|
||
| Examples: | ||
|
|
||
| Retrieve a stream by external ID: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> res = client.data_modeling.streams.retrieve("my-stream") | ||
|
|
||
| Retrieve a stream with usage statistics: | ||
|
|
||
| >>> res = client.data_modeling.streams.retrieve( | ||
| ... "my-stream", | ||
| ... include_statistics=True, | ||
| ... ) | ||
| """ | ||
| self._warning.warn() | ||
| return await self._retrieve( | ||
| cls=Stream, | ||
| identifier=Identifier(external_id), | ||
| params={"includeStatistics": include_statistics} if include_statistics is not None else None, | ||
| ) | ||
|
|
||
| async def delete(self, external_id: str | SequenceNotStr[str]) -> None: | ||
| """`Delete streams <https://api-docs.cognite.com/20230101/tag/Streams/operation/deleteStreams>`_. | ||
|
|
||
| Note: | ||
| Deletion is a soft delete that retains capacity for an extended period; | ||
| prefer deleting only when necessary. | ||
|
|
||
| Args: | ||
| external_id (str | SequenceNotStr[str]): External ID or list of external IDs of | ||
| streams to delete. | ||
|
|
||
| Examples: | ||
|
|
||
| Delete a single stream: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> client.data_modeling.streams.delete("my-stream") | ||
|
|
||
| Delete multiple streams: | ||
|
|
||
| >>> client.data_modeling.streams.delete(["stream-a", "stream-b"]) | ||
| """ | ||
| self._warning.warn() | ||
| await self._delete_multiple( | ||
| identifiers=IdentifierSequence.load(external_ids=external_id), | ||
| wrap_ids=True, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| """ | ||
| =============================================================================== | ||
| 3bfd805fbceb341bb437635ac632d5ad | ||
| This file is auto-generated from the Async API modules, - do not edit manually! | ||
| =============================================================================== | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import TYPE_CHECKING, overload | ||
|
|
||
| from cognite.client import AsyncCogniteClient | ||
| from cognite.client._sync_api_client import SyncAPIClient | ||
| from cognite.client.data_classes.data_modeling.streams import Stream, StreamList, StreamWrite | ||
| from cognite.client.utils._async_helpers import run_sync | ||
| from cognite.client.utils.useful_types import SequenceNotStr | ||
|
|
||
| if TYPE_CHECKING: | ||
| from cognite.client import AsyncCogniteClient | ||
|
|
||
|
|
||
| class SyncStreamsAPI(SyncAPIClient): | ||
| """Auto-generated, do not modify manually.""" | ||
|
|
||
| def __init__(self, async_client: AsyncCogniteClient) -> None: | ||
| self.__async_client = async_client | ||
|
|
||
| @overload | ||
| def create(self, items: StreamWrite) -> Stream: ... | ||
|
|
||
| @overload | ||
| def create(self, items: Sequence[StreamWrite]) -> StreamList: ... | ||
|
|
||
| def create(self, items: StreamWrite | Sequence[StreamWrite]) -> Stream | StreamList: | ||
| """ | ||
| `Create streams <https://api-docs.cognite.com/20230101/tag/Streams/operation/createStream>`_. | ||
|
|
||
| Args: | ||
| items (StreamWrite | Sequence[StreamWrite]): One or more streams to create. | ||
|
|
||
| Returns: | ||
| Stream | StreamList: The created stream or streams. | ||
|
|
||
| Examples: | ||
|
|
||
| Create a single stream from a template: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> from cognite.client.data_classes.data_modeling.streams import ( | ||
| ... StreamWrite, | ||
| ... StreamTemplate, | ||
| ... StreamTemplateWriteSettings, | ||
| ... ) | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> res = client.data_modeling.streams.create( | ||
| ... StreamWrite( | ||
| ... external_id="my-stream", | ||
| ... settings=StreamTemplateWriteSettings( | ||
| ... template=StreamTemplate(name="ImmutableTestStream"), | ||
| ... ), | ||
| ... ) | ||
| ... ) | ||
| """ | ||
| return run_sync(self.__async_client.data_modeling.streams.create(items=items)) | ||
|
|
||
| def list(self) -> StreamList: | ||
| """ | ||
| `List streams <https://api-docs.cognite.com/20230101/tag/Streams/operation/listStreams>`_. | ||
|
|
||
| Note: | ||
| There is no paging limit parameter: the endpoint returns all streams in the project | ||
| (projects are expected to have few streams). | ||
|
|
||
| Returns: | ||
| StreamList: The streams in the project. | ||
|
|
||
| Examples: | ||
|
|
||
| List all streams in the project: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> res = client.data_modeling.streams.list() | ||
| """ | ||
| return run_sync(self.__async_client.data_modeling.streams.list()) | ||
|
|
||
| def retrieve(self, external_id: str, include_statistics: bool | None = None) -> Stream | None: | ||
| """ | ||
| `Retrieve a stream <https://api-docs.cognite.com/20230101/tag/Streams/operation/getStream>`_. | ||
|
|
||
| Args: | ||
| external_id (str): External ID of the stream to retrieve. | ||
| include_statistics (bool | None): When ``True``, usage statistics will be returned together | ||
| with stream settings. Computing statistics can be expensive. | ||
|
|
||
| Returns: | ||
| Stream | None: The stream metadata (and optionally statistics), or ``None`` if not found. | ||
|
|
||
| Examples: | ||
|
|
||
| Retrieve a stream by external ID: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> res = client.data_modeling.streams.retrieve("my-stream") | ||
|
|
||
| Retrieve a stream with usage statistics: | ||
|
|
||
| >>> res = client.data_modeling.streams.retrieve( | ||
| ... "my-stream", | ||
| ... include_statistics=True, | ||
| ... ) | ||
| """ | ||
| return run_sync( | ||
| self.__async_client.data_modeling.streams.retrieve( | ||
| external_id=external_id, include_statistics=include_statistics | ||
| ) | ||
| ) | ||
|
|
||
| def delete(self, external_id: str | SequenceNotStr[str]) -> None: | ||
| """ | ||
| `Delete streams <https://api-docs.cognite.com/20230101/tag/Streams/operation/deleteStreams>`_. | ||
|
|
||
| Note: | ||
| Deletion is a soft delete that retains capacity for an extended period; | ||
| prefer deleting only when necessary. | ||
|
|
||
| Args: | ||
| external_id (str | SequenceNotStr[str]): External ID or list of external IDs of | ||
| streams to delete. | ||
|
|
||
| Examples: | ||
|
|
||
| Delete a single stream: | ||
|
|
||
| >>> from cognite.client import CogniteClient | ||
| >>> client = CogniteClient() | ||
| >>> # async_client = AsyncCogniteClient() # another option | ||
| >>> client.data_modeling.streams.delete("my-stream") | ||
|
|
||
| Delete multiple streams: | ||
|
|
||
| >>> client.data_modeling.streams.delete(["stream-a", "stream-b"]) | ||
| """ | ||
| return run_sync(self.__async_client.data_modeling.streams.delete(external_id=external_id)) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.