Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullah-cognite committed Dec 18, 2024
1 parent 6559c2d commit f016a1c
Show file tree
Hide file tree
Showing 11 changed files with 744 additions and 18 deletions.
2 changes: 2 additions & 0 deletions cognite/client/_api/simulators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING

from cognite.client._api.simulators.simulator_models import SimulatorModelsAPI
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
Expand All @@ -20,6 +21,7 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
self._warning = FeaturePreviewWarning(
api_maturity="General Availability", sdk_maturity="alpha", feature_name="Simulators"
)
self.models = SimulatorModelsAPI(config, api_version, cognite_client)

def list(self, limit: int = DEFAULT_LIMIT_READ) -> SimulatorList:
"""`Filter simulators <https://developer.cognite.com/api#tag/Simulators/operation/filter_simulators_simulators_list_post>`_
Expand Down
169 changes: 169 additions & 0 deletions cognite/client/_api/simulators/simulator_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, overload

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 SimulatorModelRevisionsFilter, SimulatorModelsFilter
from cognite.client.data_classes.simulators.models import (
CreatedTimeSort,
SimulatorModel,
SimulatorModelList,
SimulatorModelRevision,
SimulatorModelRevisionList,
)
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 ClientConfig, CogniteClient


class SimulatorModelRevisionsAPI(APIClient):
_RESOURCE_PATH = "/simulators/models/revisions"

def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: CogniteClient) -> None:
super().__init__(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,
sort: CreatedTimeSort | None = None,
filter: SimulatorModelRevisionsFilter | dict[str, Any] | None = None,
) -> SimulatorModelRevisionList:
"""`Filter simulator model revisions <https://developer.cognite.com/api#tag/Simulator-Models/operation/filter_simulator_model_revisions_simulators_models_revisions_list_post>`_
Retrieves a list of simulator model revisions that match the given criteria
Args:
limit (int): Maximum number of results to return. Defaults to 25. Set to -1, float(“inf”) or None to return all items. sort (CreatedTimeSort | None): Sort order for the results.
sort (CreatedTimeSort | None): No description.
filter (SimulatorModelRevisionsFilter | dict[str, Any] | None): Filter to apply.
Returns:
SimulatorModelRevisionList: List of simulator model revisions
Examples:
List simulator model revisions:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.models.revisions.list()
"""
self._warning.warn()
return self._list(
method="POST",
limit=limit,
resource_cls=SimulatorModelRevision,
list_cls=SimulatorModelRevisionList,
sort=[CreatedTimeSort.load(sort).dump()] if sort else None,
filter=filter.dump(camel_case=True) if isinstance(filter, CogniteFilter) else None,
)

@overload
def retrieve(
self,
external_id: str,
id: int | None = None,
) -> SimulatorModelRevision | None: ...

@overload
def retrieve(self, external_id: SequenceNotStr[str], id: int | None = None) -> SimulatorModelRevisionList: ...

def retrieve(
self, external_id: str | SequenceNotStr[str], id: int | None = None
) -> SimulatorModelRevision | SimulatorModelRevisionList | None:
"""`Retrieve simulator model revision <https://developer.cognite.com/api#tag/Simulator-Models/operation/retrieve_simulator_model_revisions_simulators_models_revisions_byids_post>`_
Retrieve a simulator model revision by ID or external ID
Args:
external_id (str | SequenceNotStr[str]): The external id of the simulator model revision.
id (int | None): The id of the simulator model revision.
Returns:
SimulatorModelRevision | SimulatorModelRevisionList | None: Requested simulator model revision
Examples:
Get simulator model revision by id:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.models.revisions.retrieve(id=123)
Get simulator model revision by external id:
>>> res = client.simulators.models.revisions.retrieve(external_id="abcdef")
"""
identifiers = IdentifierSequence.load(ids=id, external_ids=external_id).as_singleton()
return self._retrieve_multiple(
list_cls=SimulatorModelRevisionList,
resource_cls=SimulatorModelRevision,
identifiers=identifiers,
)


class SimulatorModelsAPI(APIClient):
_RESOURCE_PATH = "/simulators/models"

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

def list(
self,
limit: int = DEFAULT_LIMIT_READ,
filter: SimulatorModelsFilter | dict[str, Any] | None = None,
sort: CreatedTimeSort | None = None,
) -> SimulatorModelList:
"""`Filter simulator models <https://developer.cognite.com/api#tag/Simulator-Models/operation/filter_simulator_models_simulators_models_list_post>`_
Retrieves a list of simulator models that match the given criteria
Args:
limit (int): Maximum number of results to return. Defaults to 25. Set to -1, float(“inf”) or None to return all items.
filter (SimulatorModelsFilter | dict[str, Any] | None): Filter to apply.
sort (CreatedTimeSort | None): The criteria to sort by.
Returns:
SimulatorModelList: List of simulator models
Examples:
List simulator models:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.models.list()
Specify filter and sort order:
>>> res = client.simulators.models.list(
... filter={"name": "my_simulator_model"},
... sort=("created_time")
... )
"""
self._warning.warn()
return self._list(
method="POST",
limit=limit,
resource_cls=SimulatorModel,
list_cls=SimulatorModelList,
sort=[CreatedTimeSort.load(sort).dump()] if sort else None,
filter=filter.dump(camel_case=True) if isinstance(filter, CogniteFilter) else None,
)

def retrieve(self, id: int | None = None, external_id: str | None = None) -> SimulatorModel | None:
"""`Retrieve simulator model <https://developer.cognite.com/api#tag/Simulator-Models/operation/retrieve_simulator_model_simulators_models_byids_post>`_
Retrieve a simulator model by ID or external ID
Args:
id (int | None): The id of the simulator model.
external_id (str | None): The external id of the simulator model.
Returns:
SimulatorModel | None: Requested simulator model
Examples:
Retrieve simulator model by id:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.models.retrieve(id=1)
Retrieve simulator model by external id:
>>> res = client.simulators.models.retrieve(external_id="foo")
"""
identifiers = IdentifierSequence.load(ids=id, external_ids=external_id).as_singleton()
return self._retrieve_multiple(
list_cls=SimulatorModelList,
resource_cls=SimulatorModel,
identifiers=identifiers,
)
2 changes: 2 additions & 0 deletions cognite/client/_cognite_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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.simulators.simulator_models import SimulatorModelsAPI
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 @@ -85,6 +86,7 @@ def __init__(self, config: ClientConfig | None = None) -> None:
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)
self.simulators.models = SimulatorModelsAPI(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
14 changes: 14 additions & 0 deletions cognite/client/data_classes/simulators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from __future__ import annotations

from cognite.client.data_classes.simulators.models import (
CreatedTimeSort,
PropertySort,
SimulatorModel,
SimulatorModelList,
SimulatorModelRevision,
SimulatorModelRevisionList,
)
from cognite.client.data_classes.simulators.simulators import (
Simulator,
SimulatorList,
Expand All @@ -12,8 +20,14 @@
)

__all__ = [
"CreatedTimeSort",
"PropertySort",
"Simulator",
"SimulatorList",
"SimulatorModel",
"SimulatorModelList",
"SimulatorModelRevision",
"SimulatorModelRevisionList",
"SimulatorStep",
"SimulatorStepField",
"SimulatorStepOption",
Expand Down
23 changes: 23 additions & 0 deletions cognite/client/data_classes/simulators/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

from collections.abc import Sequence

from cognite.client.data_classes._base import CogniteFilter


class SimulatorModelsFilter(CogniteFilter):
def __init__(
self,
simulator_external_ids: Sequence[str] | None = None,
) -> None:
self.simulator_external_ids = simulator_external_ids


class SimulatorModelRevisionsFilter(CogniteFilter):
def __init__(
self,
model_external_ids: Sequence[str] | None = None,
all_versions: bool | None = None,
) -> None:
self.model_external_ids = model_external_ids
self.all_versions = all_versions
Loading

0 comments on commit f016a1c

Please sign in to comment.