Skip to content
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

add support for the simulators endpoint #2066

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4826ed3
first draft
abdullah-cognite Dec 17, 2024
5dcc0c8
add SimulatorsApi
abdullah-cognite Dec 17, 2024
ae25ff0
add simulator test
abdullah-cognite Dec 17, 2024
f6beb03
add simulators api to mock
abdullah-cognite Dec 17, 2024
efa9fa0
remove simulators from retryable apis
abdullah-cognite Dec 17, 2024
a22e52b
update tests
abdullah-cognite Dec 17, 2024
6559c2d
add retryable endpoint test
abdullah-cognite Dec 17, 2024
6fbc679
move fixtures to conftest
abdullah-cognite Dec 18, 2024
98a1388
add integrations
abdullah-cognite Dec 18, 2024
ddd34a4
fix MagicMock error
abdullah-cognite Dec 18, 2024
f35fc50
add delete functionality to integrations
abdullah-cognite Dec 18, 2024
e6e76c0
fix typo
abdullah-cognite Dec 18, 2024
00eaafd
update __init__
abdullah-cognite Dec 18, 2024
1f694cc
update dataset id
abdullah-cognite Dec 18, 2024
27ab947
address comments
abdullah-cognite Jan 5, 2025
f2983cf
Merge branch 'master' into simulators-sdk-first
abdullah-cognite Jan 5, 2025
4c839b3
data-set creation update
abdullah-cognite Jan 5, 2025
7bee982
data-set creation update
abdullah-cognite Jan 5, 2025
7f6ae3f
data-set creation update
abdullah-cognite Jan 5, 2025
640c110
data-set creation update
abdullah-cognite Jan 5, 2025
1475737
replace datasetids with externalid
abdullah-cognite Jan 5, 2025
76423ab
Update cognite/client/data_classes/simulators/simulators.py
abdullah-cognite Jan 6, 2025
9af1932
Update cognite/client/data_classes/simulators/simulators.py
abdullah-cognite Jan 6, 2025
20594b2
Update cognite/client/data_classes/simulators/simulators.py
abdullah-cognite Jan 6, 2025
f1c34ac
reverting last change
abdullah-cognite Jan 6, 2025
aa6f596
fixing issue with types
abdullah-cognite Jan 7, 2025
c62826b
Merge branch 'master' into simulators-sdk-first
abdullah-cognite Jan 7, 2025
7a703e4
remove unused import
abdullah-cognite Jan 7, 2025
551656d
change file_extension_types type
abdullah-cognite Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [Unreleased]
### Added
- Support for the `/simulators` and `/simulators/integration` API endpoints.

## [7.71.2] - 2025-01-07
### Added
- Instance ID is now supported for `retrieve_latest` in the datapoints API.
Expand All @@ -34,6 +38,7 @@ Changes are grouped as follows
### Added
- Support for InstanceReferences filter for Data Modeling


## [7.70.7] - 2024-12-20
### Fixed
- Passing a valid but empty string as external_id no longer raises an error for certain SDK methods
Expand Down
47 changes: 47 additions & 0 deletions cognite/client/_api/simulators/__init__.py
abdullah-cognite marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cognite.client._api.simulators.integrations import SimulatorIntegrationsAPI
from cognite.client._api_client import APIClient
from cognite.client._constants import DEFAULT_LIMIT_READ
from cognite.client.data_classes.simulators.simulators import Simulator, SimulatorList
from cognite.client.utils._experimental import FeaturePreviewWarning

if TYPE_CHECKING:
from cognite.client import CogniteClient
from cognite.client.config import ClientConfig


class SimulatorsAPI(APIClient):
_RESOURCE_PATH = "/simulators"

def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: CogniteClient) -> None:
super().__init__(config, api_version, cognite_client)
self.integrations = SimulatorIntegrationsAPI(config, api_version, cognite_client)
self._warning = FeaturePreviewWarning(
api_maturity="General Availability", sdk_maturity="alpha", feature_name="Simulators"
)

def list(self, limit: int = DEFAULT_LIMIT_READ) -> SimulatorList:
"""`List simulators <https://developer.cognite.com/api#tag/Simulators/operation/filter_simulators_simulators_list_post>`_

List simulators

Args:
limit (int): Maximum number of results to return. Defaults to 25. Set to -1, float(“inf”) or None to return all items.

Returns:
SimulatorList: List of simulators

Examples:

List simulators:

>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.list()
Comment on lines +39 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to add __call__ & __iter__? I do think almost all our APIs support the "iterative" approach.


"""
self._warning.warn()
return self._list(method="POST", limit=limit, resource_cls=Simulator, list_cls=SimulatorList)
88 changes: 88 additions & 0 deletions cognite/client/_api/simulators/integrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING

from cognite.client._api_client import APIClient
from cognite.client._constants import DEFAULT_LIMIT_READ
from cognite.client.data_classes._base import CogniteFilter
from cognite.client.data_classes.simulators.filters import SimulatorIntegrationFilter
from cognite.client.data_classes.simulators.simulators import (
SimulatorIntegration,
SimulatorIntegrationList,
)
from cognite.client.utils._experimental import FeaturePreviewWarning
from cognite.client.utils._identifier import IdentifierSequence
from cognite.client.utils.useful_types import SequenceNotStr

if TYPE_CHECKING:
from cognite.client import CogniteClient
from cognite.client.config import ClientConfig


class SimulatorIntegrationsAPI(APIClient):
_RESOURCE_PATH = "/simulators/integrations"

def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: CogniteClient) -> None:
super().__init__(config, api_version, cognite_client)
self._DELETE_LIMIT = 1
self._warning = FeaturePreviewWarning(
api_maturity="General Availability", sdk_maturity="alpha", feature_name="Simulators"
)

def list(
self,
limit: int = DEFAULT_LIMIT_READ,
filter: SimulatorIntegrationFilter | None = None,
) -> SimulatorIntegrationList:
"""`Filter simulator integrations <https://developer.cognite.com/api#tag/Simulator-Integrations/operation/filter_simulator_integrations_simulators_integrations_list_post>`_
Retrieves a list of simulator integrations that match the given criteria
Args:
limit (int): The maximum number of simulator integrations to return.
filter (SimulatorIntegrationFilter | None): Filter to apply.
Returns:
SimulatorIntegrationList: List of simulator integrations
Examples:
List simulator integrations:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.integrations.list()

Filter integrations by active status:
>>> from cognite.client.data_classes.simulators.filters import SimulatorIntegrationFilter
>>> res = client.simulators.integrations.list(
... filter=SimulatorIntegrationFilter(active=True))
"""

self._warning.warn()
return self._list(
method="POST",
limit=limit,
resource_cls=SimulatorIntegration,
list_cls=SimulatorIntegrationList,
filter=filter.dump() if isinstance(filter, CogniteFilter) else None,
abdullah-cognite marked this conversation as resolved.
Show resolved Hide resolved
)

def delete(
self,
id: int | Sequence[int] | None = None,
external_ids: str | SequenceNotStr[str] | SequenceNotStr[str] | None = None,
) -> None:
"""`Delete one or more integrations <https://developer.cognite.com/api#tag/Simulator-Integrations/operation/delete_simulator_integrations_simulators_integrations_delete_post>`_

Args:
id (int | Sequence[int] | None): Id or list of ids
external_ids (str | SequenceNotStr[str] | SequenceNotStr[str] | None): No description.

Examples:

Delete integrations by id or external id:

>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> client.simulators.integrations.delete(id=[1,2,3], external_id="foo")
"""
self._delete_multiple(
identifiers=IdentifierSequence.load(ids=id, external_ids=external_ids),
wrap_ids=True,
)
1 change: 1 addition & 0 deletions cognite/client/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class APIClient:
"geospatial/(compute|crs/byids|featuretypes/(byids|list))",
"geospatial/featuretypes/[A-Za-z][A-Za-z0-9_]{0,31}/features/(aggregate|list|byids|search|search-streaming|[A-Za-z][A-Za-z0-9_]{0,255}/rasters/[A-Za-z][A-Za-z0-9_]{0,31})",
"transformations/(filter|byids|jobs/byids|schedules/byids|query/run)",
"simulators/list",
"extpipes/(list|byids|runs/list)",
"workflows/.*",
"hostedextractors/.*",
Expand Down
3 changes: 3 additions & 0 deletions cognite/client/_cognite_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from cognite.client._api.raw import RawAPI
from cognite.client._api.relationships import RelationshipsAPI
from cognite.client._api.sequences import SequencesAPI
from cognite.client._api.simulators import SimulatorsAPI
from cognite.client._api.templates import TemplatesAPI
from cognite.client._api.three_d import ThreeDAPI
from cognite.client._api.time_series import TimeSeriesAPI
Expand Down Expand Up @@ -83,6 +84,8 @@ def __init__(self, config: ClientConfig | None = None) -> None:
self.documents = DocumentsAPI(self._config, self._API_VERSION, self)
self.workflows = WorkflowAPI(self._config, self._API_VERSION, self)
self.units = UnitAPI(self._config, self._API_VERSION, self)
self.simulators = SimulatorsAPI(self._config, self._API_VERSION, self)

# APIs just using base_url:
self._api_client = APIClient(self._config, api_version=None, cognite_client=self)

Expand Down
22 changes: 22 additions & 0 deletions cognite/client/data_classes/simulators/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
from __future__ import annotations

from cognite.client.data_classes.simulators.simulators import (
Simulator,
SimulatorIntegration,
SimulatorIntegrationList,
SimulatorList,
SimulatorStep,
SimulatorStepField,
SimulatorStepOption,
SimulatorUnitEntry,
)

__all__ = [
"Simulator",
"SimulatorIntegration",
"SimulatorIntegrationList",
"SimulatorList",
"SimulatorStep",
"SimulatorStepField",
"SimulatorStepOption",
"SimulatorUnitEntry",
]
14 changes: 14 additions & 0 deletions cognite/client/data_classes/simulators/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from cognite.client.data_classes._base import CogniteFilter
from cognite.client.utils.useful_types import SequenceNotStr


class SimulatorIntegrationFilter(CogniteFilter):
def __init__(
self,
simulator_external_ids: SequenceNotStr[str] | None = None,
active: bool | None = None,
) -> None:
self.simulator_external_ids = simulator_external_ids
self.active = active
Loading
Loading