Skip to content

Commit

Permalink
refactor: better solution to Update object
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Sep 3, 2024
1 parent b65d68f commit f11d1b7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
1 change: 0 additions & 1 deletion cognite/client/_api/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def _convert_resource_to_patch_object(
resource: CogniteResource,
update_attributes: list[PropertySpec],
mode: Literal["replace_ignore_null", "patch", "replace"] = "replace_ignore_null",
identifying_properties: dict[str, Any] | None = None,
) -> dict[str, dict[str, dict]]:
if not isinstance(resource, Annotation):
return APIClient._convert_resource_to_patch_object(resource, update_attributes)
Expand Down
15 changes: 14 additions & 1 deletion cognite/client/_api/hosted_extractors/sources.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from collections.abc import Iterator
from typing import TYPE_CHECKING, Any, Sequence, overload
from typing import TYPE_CHECKING, Any, Literal, Sequence, overload

from cognite.client._api_client import APIClient
from cognite.client._constants import DEFAULT_LIMIT_READ
from cognite.client.data_classes._base import CogniteResource, PropertySpec
from cognite.client.data_classes.hosted_extractors.sources import Source, SourceList, SourceUpdate, SourceWrite
from cognite.client.utils._experimental import FeaturePreviewWarning
from cognite.client.utils._identifier import IdentifierSequence
Expand Down Expand Up @@ -260,3 +261,15 @@ def list(
limit=limit,
headers={"cdf-version": "beta"},
)

@classmethod
def _convert_resource_to_patch_object(
cls,
resource: CogniteResource,
update_attributes: list[PropertySpec],
mode: Literal["replace_ignore_null", "patch", "replace"] = "replace_ignore_null",
) -> dict[str, dict[str, dict]]:
output = super()._convert_resource_to_patch_object(resource, update_attributes, mode)
if hasattr(resource, "type"):
output["type"] = resource.type
return output
5 changes: 0 additions & 5 deletions cognite/client/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,6 @@ def _update_multiple(
item,
update_cls._get_update_properties(item),
mode,
update_cls._get_extra_identifying_properties(item),
)
)
elif isinstance(item, CogniteUpdate):
Expand Down Expand Up @@ -1224,7 +1223,6 @@ def _convert_resource_to_patch_object(
resource: CogniteResource,
update_attributes: list[PropertySpec],
mode: Literal["replace_ignore_null", "patch", "replace"] = "replace_ignore_null",
identifying_properties: dict[str, Any] | None = None,
) -> dict[str, dict[str, dict]]:
dumped_resource = resource.dump(camel_case=True)
has_id = "id" in dumped_resource
Expand All @@ -1240,9 +1238,6 @@ def _convert_resource_to_patch_object(
elif has_external_id:
patch_object["externalId"] = dumped_resource.pop("externalId")

if identifying_properties:
patch_object.update(identifying_properties)

update: dict[str, dict] = cls._clear_all_attributes(update_attributes) if mode == "replace" else {}

update_attribute_by_name = {prop.name: prop for prop in update_attributes}
Expand Down
6 changes: 0 additions & 6 deletions cognite/client/data_classes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,6 @@ def dump(self, camel_case: Literal[True] = True) -> dict[str, Any]:
def _get_update_properties(cls, item: CogniteResource | None = None) -> list[PropertySpec]:
raise NotImplementedError

@classmethod
def _get_extra_identifying_properties(cls, item: CogniteResource | None = None) -> dict[str, Any]:
# This method is used to provide additional identifying properties for the update object.
# It is intended to be overridden by subclasses that need to provide additional identifying properties.
return {}


T_CogniteUpdate = TypeVar("T_CogniteUpdate", bound=CogniteUpdate)

Expand Down

0 comments on commit f11d1b7

Please sign in to comment.