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 example on how to update the nonce for transformations #1517

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 18 additions & 5 deletions cognite/client/_api/transformations/__init__.py
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this should accept a created session as well, would make the example much easier and more intuitive. Wdyt?

Also, please add some indentation to make it easier to read 🙏

Copy link
Contributor

@haakonvt haakonvt Nov 23, 2023

Choose a reason for hiding this comment

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

Can't be done because the CreatedSession does not store a reference to the CogniteClient, so we don't know what project to pass.

Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,28 @@ def update(

Perform a partial update on a transformation, updating the query and making it private::

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import TransformationUpdate
>>> c = CogniteClient()
>>> my_update = TransformationUpdate(id=1).query.set("SELECT * FROM _cdf.assets").is_public.set(False)
>>> res = c.transformations.update(my_update)
"""

Update the session used for reading (source) and writing (destination) when authenticating for all
transformations in a given data set:

>>> from cognite.client.data_classes import NonceCredentials
>>> to_update = c.transformations.list(data_set_external_ids=["foo"])
>>> new_session = c.iam.sessions.create()
>>> new_nonce = NonceCredentials(
... session_id=new_session.id,
... nonce=new_session.nonce,
... cdf_project_name=c.config.project
... )
>>> for tr in to_update:
... tr.source_nonce = new_nonce
... tr.destination_nonce = new_nonce
>>> res = c.transformations.update(to_update)
"""
if isinstance(item, Sequence):
item = list(item).copy()
item = list(item)
sessions: dict[str, NonceCredentials] = {}
for i, t in enumerate(item):
if isinstance(t, Transformation):
Expand All @@ -321,7 +334,7 @@ def update(
item._process_credentials(keep_none=True)
elif not isinstance(item, TransformationUpdate):
raise TypeError(
"item must be Sequence[Transformation], Transformation, Sequence[TransformationUpdate] or TransformationUpdate"
"item must be one of: TransformationUpdate, Transformation, Sequence[TransformationUpdate | Transformation]."
)

return self._update_multiple(
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/data_classes/transformations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ class TransformationFilter(CogniteFilter):
has_blocked_error (bool | None): Whether only the blocked transformations should be included in the results.
created_time (dict[str, Any] | TimestampRange | None): Range between two timestamps
last_updated_time (dict[str, Any] | TimestampRange | None): Range between two timestamps
data_set_ids (list[dict[str, Any]] | None): Return only transformations in the specified data sets with these ids.
data_set_ids (list[dict[str, Any]] | None): Return only transformations in the specified data sets with these ids, e.g. [{"id": 1}, {"externalId": "foo"}].
tags (TagsFilter | None): Return only the resource matching the specified tags constraints. It only supports ContainsAny as of now.
"""

Expand Down
Loading