Skip to content

Commit

Permalink
refactor: generate client
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Aug 18, 2024
1 parent 6fad744 commit fd26464
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
17 changes: 14 additions & 3 deletions cognite/pygen/_core/templates/_api_client_multi_model.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from pathlib import Path
from typing import Any, Sequence

from cognite.client import ClientConfig, CogniteClient, data_modeling as dm
from cognite.client.data_classes import TimeSeriesList
from cognite.client.data_classes import TimeSeriesList, FileMetadataList, SequenceList
from cognite.client.credentials import OAuthClientCredentials

from ._api import ({% for api in api_classes %}{% if not api.data_class.is_edge_class %}
Expand Down Expand Up @@ -79,6 +79,8 @@ class {{ client_name }}:
) -> data_classes.ResourcesWriteResult:
"""Add or update (upsert) items.

This method will create the nodes, edges, timeseries, files and sequences of the supplied items.

Args:
items: One or more instances of the pygen generated data classes.
replace (bool): How do we behave when a property value exists? Do we replace all matching and existing values with the supplied values (true)?
Expand All @@ -103,8 +105,17 @@ class {{ client_name }}:
time_series = TimeSeriesList([])
if instances.time_series:
time_series = self._client.time_series.upsert(instances.time_series, mode="patch")
files = FileMetadataList([])
if instances.files:
for file in instances.files:
created, _ = self._client.files.create(file, overwrite=True)
files.append(created)

sequences = SequenceList([])
if instances.sequences:
sequences = self._client.sequences.upsert(instances.sequences, mode="patch")

return data_classes.ResourcesWriteResult(result.nodes, result.edges, TimeSeriesList(time_series))
return data_classes.ResourcesWriteResult(result.nodes, result.edges, time_series, files, sequences)

def _create_instances(
self,
Expand Down Expand Up @@ -133,7 +144,7 @@ class {{ client_name }}:
replace: bool = False,
write_none: bool = False,
) -> data_classes.ResourcesWriteResult:
"""Add or update (upsert) items.
"""[DEPRECATED] Add or update (upsert) items.

Args:
items: One or more instances of the pygen generated data classes.
Expand Down
17 changes: 14 additions & 3 deletions cognite/pygen/_core/templates/_api_client_single_model.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from pathlib import Path
from typing import Any, Sequence

from cognite.client import ClientConfig, CogniteClient, data_modeling as dm
from cognite.client.data_classes import TimeSeriesList
from cognite.client.data_classes import TimeSeriesList, FileMetadataList, SequenceList
from cognite.client.credentials import OAuthClientCredentials

from ._api import ({% for api in api_classes %}{% if not api.data_class.is_edge_class %}
Expand Down Expand Up @@ -54,6 +54,8 @@ class {{ client_name }}:
) -> data_classes.ResourcesWriteResult:
"""Add or update (upsert) items.

This method will create the nodes, edges, timeseries, files and sequences of the supplied items.

Args:
items: One or more instances of the pygen generated data classes.
replace (bool): How do we behave when a property value exists? Do we replace all matching and existing values with the supplied values (true)?
Expand All @@ -78,8 +80,17 @@ class {{ client_name }}:
time_series = TimeSeriesList([])
if instances.time_series:
time_series = self._client.time_series.upsert(instances.time_series, mode="patch")
files = FileMetadataList([])
if instances.files:
for file in instances.files:
created, _ = self._client.files.create(file, overwrite=True)
files.append(created)

sequences = SequenceList([])
if instances.sequences:
sequences = self._client.sequences.upsert(instances.sequences, mode="patch")

return data_classes.ResourcesWriteResult(result.nodes, result.edges, TimeSeriesList(time_series))
return data_classes.ResourcesWriteResult(result.nodes, result.edges, time_series, files, sequences)

def _create_instances(
self,
Expand Down Expand Up @@ -108,7 +119,7 @@ class {{ client_name }}:
replace: bool = False,
write_none: bool = False,
) -> data_classes.ResourcesWriteResult:
"""Add or update (upsert) items.
"""[DEPRECATED] Add or update (upsert) items.

Args:
items: One or more instances of the pygen generated data classes.
Expand Down
1 change: 0 additions & 1 deletion examples/omni/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from cognite.client import ClientConfig, CogniteClient, data_modeling as dm
from cognite.client.data_classes import TimeSeriesList, FileMetadataList, SequenceList
from cognite.client.credentials import OAuthClientCredentials
from cognite.client.exceptions import CogniteAPIError

from ._api import (
CDFExternalReferencesAPI,
Expand Down

0 comments on commit fd26464

Please sign in to comment.