cognite-toolkit 0.7.46__py3-none-any.whl → 0.7.48__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. cognite_toolkit/_cdf_tk/apps/_migrate_app.py +6 -6
  2. cognite_toolkit/_cdf_tk/client/_toolkit_client.py +6 -4
  3. cognite_toolkit/_cdf_tk/client/api/agents.py +107 -0
  4. cognite_toolkit/_cdf_tk/client/api/annotations.py +129 -0
  5. cognite_toolkit/_cdf_tk/client/api/containers.py +132 -0
  6. cognite_toolkit/_cdf_tk/client/api/data_models.py +137 -0
  7. cognite_toolkit/_cdf_tk/client/api/function_schedules.py +115 -0
  8. cognite_toolkit/_cdf_tk/client/api/functions.py +113 -0
  9. cognite_toolkit/_cdf_tk/client/api/graphql_data_models.py +167 -0
  10. cognite_toolkit/_cdf_tk/client/api/groups.py +121 -0
  11. cognite_toolkit/_cdf_tk/client/api/instances.py +139 -0
  12. cognite_toolkit/_cdf_tk/client/api/location_filters.py +177 -0
  13. cognite_toolkit/_cdf_tk/client/api/raw.py +2 -2
  14. cognite_toolkit/_cdf_tk/client/api/relationships.py +133 -0
  15. cognite_toolkit/_cdf_tk/client/api/robotics.py +19 -0
  16. cognite_toolkit/_cdf_tk/client/api/robotics_capabilities.py +127 -0
  17. cognite_toolkit/_cdf_tk/client/api/robotics_data_postprocessing.py +138 -0
  18. cognite_toolkit/_cdf_tk/client/api/robotics_frames.py +122 -0
  19. cognite_toolkit/_cdf_tk/client/api/robotics_locations.py +127 -0
  20. cognite_toolkit/_cdf_tk/client/api/robotics_maps.py +122 -0
  21. cognite_toolkit/_cdf_tk/client/api/robotics_robots.py +122 -0
  22. cognite_toolkit/_cdf_tk/client/api/search_config.py +101 -0
  23. cognite_toolkit/_cdf_tk/client/api/spaces.py +117 -0
  24. cognite_toolkit/_cdf_tk/client/api/streams.py +63 -55
  25. cognite_toolkit/_cdf_tk/client/api/three_d.py +293 -277
  26. cognite_toolkit/_cdf_tk/client/api/views.py +139 -0
  27. cognite_toolkit/_cdf_tk/client/cdf_client/api.py +42 -7
  28. cognite_toolkit/_cdf_tk/client/cdf_client/responses.py +11 -0
  29. cognite_toolkit/_cdf_tk/client/http_client/_client.py +5 -2
  30. cognite_toolkit/_cdf_tk/client/http_client/_data_classes.py +10 -0
  31. cognite_toolkit/_cdf_tk/client/http_client/_data_classes2.py +4 -3
  32. cognite_toolkit/_cdf_tk/client/request_classes/filters.py +75 -0
  33. cognite_toolkit/_cdf_tk/client/request_classes/graphql.py +28 -0
  34. cognite_toolkit/_cdf_tk/client/resource_classes/agent.py +8 -2
  35. cognite_toolkit/_cdf_tk/client/resource_classes/apm_config.py +128 -0
  36. cognite_toolkit/_cdf_tk/client/resource_classes/cognite_file.py +53 -0
  37. cognite_toolkit/_cdf_tk/client/resource_classes/data_modeling/__init__.py +4 -0
  38. cognite_toolkit/_cdf_tk/client/resource_classes/data_modeling/_instance.py +22 -11
  39. cognite_toolkit/_cdf_tk/client/resource_classes/function_schedule.py +8 -4
  40. cognite_toolkit/_cdf_tk/client/resource_classes/group/group.py +9 -5
  41. cognite_toolkit/_cdf_tk/client/resource_classes/identifiers.py +7 -0
  42. cognite_toolkit/_cdf_tk/client/resource_classes/location_filter.py +9 -2
  43. cognite_toolkit/_cdf_tk/client/resource_classes/relationship.py +9 -3
  44. cognite_toolkit/_cdf_tk/client/resource_classes/resource_view_mapping.py +38 -0
  45. cognite_toolkit/_cdf_tk/client/resource_classes/robotics/_map.py +6 -1
  46. cognite_toolkit/_cdf_tk/client/resource_classes/robotics/_robot.py +10 -5
  47. cognite_toolkit/_cdf_tk/client/resource_classes/streams.py +1 -20
  48. cognite_toolkit/_cdf_tk/client/resource_classes/three_d.py +30 -9
  49. cognite_toolkit/_cdf_tk/client/testing.py +2 -2
  50. cognite_toolkit/_cdf_tk/commands/_migrate/data_mapper.py +5 -5
  51. cognite_toolkit/_cdf_tk/commands/_migrate/migration_io.py +11 -7
  52. cognite_toolkit/_cdf_tk/commands/build_v2/_module_parser.py +138 -0
  53. cognite_toolkit/_cdf_tk/commands/build_v2/_modules_parser.py +163 -0
  54. cognite_toolkit/_cdf_tk/commands/build_v2/build_cmd.py +83 -96
  55. cognite_toolkit/_cdf_tk/commands/build_v2/{build_input.py → build_parameters.py} +8 -22
  56. cognite_toolkit/_cdf_tk/commands/build_v2/data_classes/_modules.py +27 -0
  57. cognite_toolkit/_cdf_tk/commands/build_v2/data_classes/_resource.py +22 -0
  58. cognite_toolkit/_cdf_tk/cruds/__init__.py +11 -5
  59. cognite_toolkit/_cdf_tk/cruds/_resource_cruds/streams.py +14 -30
  60. cognite_toolkit/_cdf_tk/data_classes/__init__.py +3 -0
  61. cognite_toolkit/_cdf_tk/data_classes/_issues.py +36 -0
  62. cognite_toolkit/_cdf_tk/data_classes/_module_directories.py +2 -1
  63. cognite_toolkit/_cdf_tk/storageio/_base.py +2 -0
  64. cognite_toolkit/_cdf_tk/storageio/logger.py +163 -0
  65. cognite_toolkit/_cdf_tk/utils/__init__.py +8 -1
  66. cognite_toolkit/_cdf_tk/utils/interactive_select.py +3 -1
  67. cognite_toolkit/_cdf_tk/utils/modules.py +7 -0
  68. cognite_toolkit/_repo_files/GitHub/.github/workflows/deploy.yaml +1 -1
  69. cognite_toolkit/_repo_files/GitHub/.github/workflows/dry-run.yaml +1 -1
  70. cognite_toolkit/_resources/cdf.toml +1 -1
  71. cognite_toolkit/_version.py +1 -1
  72. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.48.dist-info}/METADATA +1 -1
  73. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.48.dist-info}/RECORD +76 -46
  74. cognite_toolkit/_cdf_tk/commands/build_v2/build_issues.py +0 -27
  75. /cognite_toolkit/_cdf_tk/client/resource_classes/{search_config_resource.py → search_config.py} +0 -0
  76. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.48.dist-info}/WHEEL +0 -0
  77. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.48.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,115 @@
1
+ """Function Schedules API for managing CDF function schedules.
2
+
3
+ Based on the API specification at:
4
+ https://api-docs.cognite.com/20230101/tag/Function-schedules/operation/postFunctionSchedules
5
+ """
6
+
7
+ from collections.abc import Iterable, Sequence
8
+
9
+ from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, Endpoint, PagedResponse
10
+ from cognite_toolkit._cdf_tk.client.http_client import HTTPClient, ItemsSuccessResponse2, SuccessResponse2
11
+ from cognite_toolkit._cdf_tk.client.resource_classes.function_schedule import (
12
+ FunctionScheduleRequest,
13
+ FunctionScheduleResponse,
14
+ )
15
+ from cognite_toolkit._cdf_tk.client.resource_classes.identifiers import InternalId
16
+
17
+
18
+ class FunctionSchedulesAPI(CDFResourceAPI[InternalId, FunctionScheduleRequest, FunctionScheduleResponse]):
19
+ """API for managing CDF function schedules.
20
+
21
+ Note: Function schedules do not support update operations.
22
+ """
23
+
24
+ def __init__(self, http_client: HTTPClient) -> None:
25
+ super().__init__(
26
+ http_client=http_client,
27
+ method_endpoint_map={
28
+ "create": Endpoint(method="POST", path="/functions/schedules", item_limit=1),
29
+ "retrieve": Endpoint(method="POST", path="/functions/schedules/byids", item_limit=10_000),
30
+ "delete": Endpoint(method="POST", path="/functions/schedules/delete", item_limit=10_000),
31
+ "list": Endpoint(method="POST", path="/functions/schedules/list", item_limit=1000),
32
+ },
33
+ )
34
+
35
+ def _validate_page_response(
36
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
37
+ ) -> PagedResponse[FunctionScheduleResponse]:
38
+ return PagedResponse[FunctionScheduleResponse].model_validate_json(response.body)
39
+
40
+ def create(self, items: Sequence[FunctionScheduleRequest]) -> list[FunctionScheduleResponse]:
41
+ """Create function schedules in CDF.
42
+
43
+ Args:
44
+ items: List of FunctionScheduleRequest objects to create.
45
+
46
+ Returns:
47
+ List of created FunctionScheduleResponse objects.
48
+ """
49
+ return self._request_item_response(items, "create")
50
+
51
+ def retrieve(self, items: Sequence[InternalId], ignore_unknown_ids: bool = False) -> list[FunctionScheduleResponse]:
52
+ """Retrieve function schedules from CDF by ID.
53
+
54
+ Args:
55
+ items: List of InternalId objects to retrieve.
56
+ ignore_unknown_ids: Whether to ignore unknown IDs.
57
+
58
+ Returns:
59
+ List of retrieved FunctionScheduleResponse objects.
60
+ """
61
+ return self._request_item_response(
62
+ items, method="retrieve", extra_body={"ignoreUnknownIds": ignore_unknown_ids}
63
+ )
64
+
65
+ def delete(self, items: Sequence[InternalId]) -> None:
66
+ """Delete function schedules from CDF.
67
+
68
+ Args:
69
+ items: List of InternalId objects to delete.
70
+ """
71
+ self._request_no_response(items, "delete")
72
+
73
+ def paginate(
74
+ self,
75
+ limit: int = 100,
76
+ cursor: str | None = None,
77
+ ) -> PagedResponse[FunctionScheduleResponse]:
78
+ """Get a page of function schedules from CDF.
79
+
80
+ Args:
81
+ limit: Maximum number of function schedules to return.
82
+ cursor: Cursor for pagination.
83
+
84
+ Returns:
85
+ PagedResponse of FunctionScheduleResponse objects.
86
+ """
87
+ return self._paginate(
88
+ cursor=cursor,
89
+ limit=limit,
90
+ )
91
+
92
+ def iterate(
93
+ self,
94
+ limit: int | None = None,
95
+ ) -> Iterable[list[FunctionScheduleResponse]]:
96
+ """Iterate over all function schedules in CDF.
97
+
98
+ Args:
99
+ limit: Maximum total number of function schedules to return.
100
+
101
+ Returns:
102
+ Iterable of lists of FunctionScheduleResponse objects.
103
+ """
104
+ return self._iterate(limit=limit)
105
+
106
+ def list(self, limit: int | None = None) -> list[FunctionScheduleResponse]:
107
+ """List all function schedules in CDF.
108
+
109
+ Args:
110
+ limit: Maximum total number of function schedules to return.
111
+
112
+ Returns:
113
+ List of FunctionScheduleResponse objects.
114
+ """
115
+ return self._list(limit=limit)
@@ -0,0 +1,113 @@
1
+ """Functions API for managing CDF functions.
2
+
3
+ Based on the API specification at:
4
+ https://api-docs.cognite.com/20230101/tag/Functions/operation/postFunctions
5
+ """
6
+
7
+ from collections.abc import Iterable, Sequence
8
+
9
+ from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, Endpoint, PagedResponse
10
+ from cognite_toolkit._cdf_tk.client.http_client import HTTPClient, ItemsSuccessResponse2, SuccessResponse2
11
+ from cognite_toolkit._cdf_tk.client.resource_classes.function import FunctionRequest, FunctionResponse
12
+ from cognite_toolkit._cdf_tk.client.resource_classes.identifiers import InternalId
13
+
14
+
15
+ class FunctionsAPI(CDFResourceAPI[InternalId, FunctionRequest, FunctionResponse]):
16
+ """API for managing CDF functions.
17
+
18
+ Note: Functions do not support update operations.
19
+ """
20
+
21
+ def __init__(self, http_client: HTTPClient) -> None:
22
+ super().__init__(
23
+ http_client=http_client,
24
+ method_endpoint_map={
25
+ "create": Endpoint(method="POST", path="/functions", item_limit=1),
26
+ "retrieve": Endpoint(method="POST", path="/functions/byids", item_limit=10),
27
+ "delete": Endpoint(method="POST", path="/functions/delete", item_limit=10),
28
+ "list": Endpoint(method="POST", path="/functions/list", item_limit=1000),
29
+ },
30
+ )
31
+
32
+ def _validate_page_response(
33
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
34
+ ) -> PagedResponse[FunctionResponse]:
35
+ return PagedResponse[FunctionResponse].model_validate_json(response.body)
36
+
37
+ def create(self, items: Sequence[FunctionRequest]) -> list[FunctionResponse]:
38
+ """Create functions in CDF.
39
+
40
+ Args:
41
+ items: List of FunctionRequest objects to create.
42
+
43
+ Returns:
44
+ List of created FunctionResponse objects.
45
+ """
46
+ return self._request_item_response(items, "create")
47
+
48
+ def retrieve(self, items: Sequence[InternalId], ignore_unknown_ids: bool = False) -> list[FunctionResponse]:
49
+ """Retrieve functions from CDF by ID.
50
+
51
+ Args:
52
+ items: List of InternalId objects to retrieve.
53
+ ignore_unknown_ids: Whether to ignore unknown IDs.
54
+
55
+ Returns:
56
+ List of retrieved FunctionResponse objects.
57
+ """
58
+ return self._request_item_response(
59
+ items, method="retrieve", extra_body={"ignoreUnknownIds": ignore_unknown_ids}
60
+ )
61
+
62
+ def delete(self, items: Sequence[InternalId], ignore_unknown_ids: bool = False) -> None:
63
+ """Delete functions from CDF.
64
+
65
+ Args:
66
+ items: List of InternalId objects to delete.
67
+ ignore_unknown_ids: Whether to ignore unknown IDs.
68
+ """
69
+ self._request_no_response(items, "delete", extra_body={"ignoreUnknownIds": ignore_unknown_ids})
70
+
71
+ def paginate(
72
+ self,
73
+ limit: int = 100,
74
+ cursor: str | None = None,
75
+ ) -> PagedResponse[FunctionResponse]:
76
+ """Get a page of functions from CDF.
77
+
78
+ Args:
79
+ limit: Maximum number of functions to return.
80
+ cursor: Cursor for pagination.
81
+
82
+ Returns:
83
+ PagedResponse of FunctionResponse objects.
84
+ """
85
+ return self._paginate(
86
+ cursor=cursor,
87
+ limit=limit,
88
+ )
89
+
90
+ def iterate(
91
+ self,
92
+ limit: int | None = None,
93
+ ) -> Iterable[list[FunctionResponse]]:
94
+ """Iterate over all functions in CDF.
95
+
96
+ Args:
97
+ limit: Maximum total number of functions to return.
98
+
99
+ Returns:
100
+ Iterable of lists of FunctionResponse objects.
101
+ """
102
+ return self._iterate(limit=limit)
103
+
104
+ def list(self, limit: int | None = None) -> list[FunctionResponse]:
105
+ """List all functions in CDF.
106
+
107
+ Args:
108
+ limit: Maximum total number of functions to return.
109
+
110
+ Returns:
111
+ List of FunctionResponse objects.
112
+ """
113
+ return self._list(limit=limit)
@@ -0,0 +1,167 @@
1
+ """GraphQL Data Models API for managing CDF GraphQL/DML data models.
2
+
3
+ This API provides a wrapper around the legacy DML API for managing GraphQL data models.
4
+ """
5
+
6
+ from collections.abc import Iterable, Sequence
7
+ from typing import Any
8
+
9
+ from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, Endpoint, PagedResponse
10
+ from cognite_toolkit._cdf_tk.client.cdf_client.responses import GraphQLUpsertResponse
11
+ from cognite_toolkit._cdf_tk.client.http_client import (
12
+ HTTPClient,
13
+ ItemsSuccessResponse2,
14
+ RequestMessage2,
15
+ SuccessResponse2,
16
+ ToolkitAPIError,
17
+ )
18
+ from cognite_toolkit._cdf_tk.client.request_classes.filters import DataModelFilter
19
+ from cognite_toolkit._cdf_tk.client.request_classes.graphql import UPSERT_BODY
20
+ from cognite_toolkit._cdf_tk.client.resource_classes.data_modeling import DataModelReference
21
+ from cognite_toolkit._cdf_tk.client.resource_classes.graphql_data_model import (
22
+ GraphQLDataModelRequest,
23
+ GraphQLDataModelResponse,
24
+ )
25
+
26
+
27
+ class GraphQLDataModelsAPI(CDFResourceAPI[DataModelReference, GraphQLDataModelRequest, GraphQLDataModelResponse]):
28
+ """API for managing CDF GraphQL/DML data models.
29
+
30
+ This API uses GraphQL mutations to manage data models with DML (Data Modeling Language).
31
+ """
32
+
33
+ def __init__(self, http_client: HTTPClient) -> None:
34
+ super().__init__(
35
+ http_client=http_client,
36
+ method_endpoint_map={
37
+ "retrieve": Endpoint(method="POST", path="/models/datamodels/byids", item_limit=100),
38
+ "delete": Endpoint(method="POST", path="/models/datamodels/delete", item_limit=100),
39
+ "list": Endpoint(method="GET", path="/models/datamodels", item_limit=1000),
40
+ },
41
+ )
42
+
43
+ def _validate_page_response(
44
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
45
+ ) -> PagedResponse[GraphQLDataModelResponse]:
46
+ return PagedResponse[GraphQLDataModelResponse].model_validate_json(response.body)
47
+
48
+ def _post_graphql(self, payload: dict[str, Any]) -> GraphQLUpsertResponse:
49
+ """Execute a GraphQL query against the DML endpoint."""
50
+ request = RequestMessage2(
51
+ endpoint_url=self._make_url("/dml/graphql"),
52
+ method="POST",
53
+ body_content=payload,
54
+ )
55
+ result = self._http_client.request_single_retries(request)
56
+ response = result.get_success_or_raise()
57
+ parsed = GraphQLUpsertResponse.model_validate_json(response.body)
58
+ if errors := parsed.upsert_graph_ql_dml_version.errors:
59
+ raise ToolkitAPIError(f"Failed GraphQL errors: {errors}")
60
+ return parsed
61
+
62
+ def create(self, items: Sequence[GraphQLDataModelRequest]) -> list[GraphQLDataModelResponse]:
63
+ """Apply (create or update) GraphQL data models in CDF.
64
+
65
+ Args:
66
+ items: List of GraphQLDataModelRequest objects to apply.
67
+
68
+ Returns:
69
+ List of applied GraphQLDataModelResponse objects.
70
+ """
71
+ results: list[GraphQLDataModelResponse] = []
72
+ for item in items:
73
+ payload = {"query": UPSERT_BODY, "variables": {"dmCreate": item.dump()}}
74
+ response = self._post_graphql(payload)
75
+
76
+ results.append(response.upsert_graph_ql_dml_version.data)
77
+
78
+ return results
79
+
80
+ def update(self, items: Sequence[GraphQLDataModelRequest]) -> list[GraphQLDataModelResponse]:
81
+ """Update GraphQL data models in CDF.
82
+
83
+ Args:
84
+ items: List of GraphQLDataModelRequest objects to update.
85
+ Returns:
86
+ List of updated GraphQLDataModelResponse objects.
87
+ """
88
+ return self.create(items)
89
+
90
+ def retrieve(
91
+ self, items: Sequence[DataModelReference], inline_views: bool = False
92
+ ) -> list[GraphQLDataModelResponse]:
93
+ """Retrieve GraphQL data models from CDF.
94
+
95
+ Args:
96
+ items: List of DataModelReference objects to retrieve.
97
+ inline_views: Whether to include full view definitions in the response.
98
+
99
+ Returns:
100
+ List of retrieved GraphQLDataModelResponse objects.
101
+ """
102
+ return self._request_item_response(items, method="retrieve", extra_body={"inlineViews": inline_views})
103
+
104
+ def delete(self, items: Sequence[DataModelReference]) -> None:
105
+ """Delete GraphQL data models from CDF.
106
+
107
+ Args:
108
+ items: List of DataModelReference objects to delete.
109
+ """
110
+ self._request_no_response(items, "delete")
111
+
112
+ def paginate(
113
+ self,
114
+ filter: DataModelFilter | None = None,
115
+ limit: int = 100,
116
+ cursor: str | None = None,
117
+ ) -> PagedResponse[GraphQLDataModelResponse]:
118
+ """Get a page of GraphQL data models from CDF.
119
+
120
+ Args:
121
+ filter: DataModelFilter to filter data models.
122
+ limit: Maximum number of data models to return.
123
+ cursor: Cursor for pagination.
124
+
125
+ Returns:
126
+ PagedResponse of GraphQLDataModelResponse objects.
127
+ """
128
+ return self._paginate(
129
+ cursor=cursor,
130
+ limit=limit,
131
+ params=filter.dump() if filter else None,
132
+ )
133
+
134
+ def iterate(
135
+ self,
136
+ filter: DataModelFilter | None = None,
137
+ limit: int | None = None,
138
+ ) -> Iterable[list[GraphQLDataModelResponse]]:
139
+ """Iterate over all GraphQL data models in CDF.
140
+
141
+ Args:
142
+ filter: DataModelFilter to filter data models.
143
+ limit: Maximum total number of data models to return.
144
+
145
+ Returns:
146
+ Iterable of lists of GraphQLDataModelResponse objects.
147
+ """
148
+ return self._iterate(
149
+ limit=limit,
150
+ params=filter.dump() if filter else None,
151
+ )
152
+
153
+ def list(
154
+ self,
155
+ filter: DataModelFilter | None = None,
156
+ limit: int | None = None,
157
+ ) -> list[GraphQLDataModelResponse]:
158
+ """List all GraphQL data models in CDF.
159
+
160
+ Args:
161
+ filter: DataModelFilter to filter data models.
162
+ limit: Maximum total number of data models to return.
163
+
164
+ Returns:
165
+ List of GraphQLDataModelResponse objects.
166
+ """
167
+ return self._list(limit=limit, params=filter.dump() if filter else None)
@@ -0,0 +1,121 @@
1
+ """Groups API for managing CDF access groups.
2
+
3
+ Based on the API specification at:
4
+ https://api-docs.cognite.com/20230101/tag/Groups/operation/createGroups
5
+ """
6
+
7
+ from collections.abc import Iterable, Sequence
8
+
9
+ from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, Endpoint, PagedResponse
10
+ from cognite_toolkit._cdf_tk.client.http_client import (
11
+ HTTPClient,
12
+ ItemsSuccessResponse2,
13
+ RequestMessage2,
14
+ SuccessResponse2,
15
+ )
16
+ from cognite_toolkit._cdf_tk.client.resource_classes.group import GroupRequest, GroupResponse
17
+ from cognite_toolkit._cdf_tk.client.resource_classes.identifiers import InternalId
18
+ from cognite_toolkit._cdf_tk.utils.collection import chunker_sequence
19
+
20
+
21
+ class GroupsAPI(CDFResourceAPI[InternalId, GroupRequest, GroupResponse]):
22
+ """API for managing CDF access groups.
23
+
24
+ Note: Groups do not support update operations.
25
+ """
26
+
27
+ def __init__(self, http_client: HTTPClient) -> None:
28
+ super().__init__(
29
+ http_client=http_client,
30
+ method_endpoint_map={
31
+ "create": Endpoint(method="POST", path="/groups", item_limit=1000),
32
+ "delete": Endpoint(method="POST", path="/groups/delete", item_limit=1000),
33
+ "list": Endpoint(method="GET", path="/groups", item_limit=1000),
34
+ },
35
+ )
36
+
37
+ def _validate_page_response(
38
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
39
+ ) -> PagedResponse[GroupResponse]:
40
+ return PagedResponse[GroupResponse].model_validate_json(response.body)
41
+
42
+ def create(self, items: Sequence[GroupRequest]) -> list[GroupResponse]:
43
+ """Create groups in CDF.
44
+
45
+ Args:
46
+ items: List of GroupRequest objects to create.
47
+
48
+ Returns:
49
+ List of created GroupResponse objects.
50
+ """
51
+ return self._request_item_response(items, "create")
52
+
53
+ def delete(self, items: Sequence[InternalId]) -> None:
54
+ """Delete groups from CDF.
55
+
56
+ Args:
57
+ items: List of InternalId objects to delete.
58
+ """
59
+ # Custom implementation since delete does not wrap the items in a {"id": ...} structure
60
+ endpoint = self._method_endpoint_map["delete"]
61
+ for chunk in chunker_sequence(items, endpoint.item_limit):
62
+ request = RequestMessage2(
63
+ endpoint_url=self._make_url(endpoint.path),
64
+ method=endpoint.method,
65
+ body_content={"items": [item.id for item in chunk]},
66
+ )
67
+ response = self._http_client.request_single_retries(request)
68
+ response.get_success_or_raise()
69
+
70
+ def paginate(
71
+ self,
72
+ all_groups: bool = False,
73
+ limit: int = 100,
74
+ cursor: str | None = None,
75
+ ) -> PagedResponse[GroupResponse]:
76
+ """Get a page of groups from CDF.
77
+
78
+ Args:
79
+ all_groups: Whether to return all groups (requires admin permissions).
80
+ limit: Maximum number of groups to return.
81
+ cursor: Cursor for pagination.
82
+
83
+ Returns:
84
+ PagedResponse of GroupResponse objects.
85
+ """
86
+ return self._paginate(
87
+ cursor=cursor,
88
+ limit=limit,
89
+ params={"all": all_groups} if all_groups else None,
90
+ )
91
+
92
+ def iterate(
93
+ self,
94
+ all_groups: bool = False,
95
+ limit: int | None = None,
96
+ ) -> Iterable[list[GroupResponse]]:
97
+ """Iterate over all groups in CDF.
98
+
99
+ Args:
100
+ all_groups: Whether to return all groups (requires admin permissions).
101
+ limit: Maximum total number of groups to return.
102
+
103
+ Returns:
104
+ Iterable of lists of GroupResponse objects.
105
+ """
106
+ return self._iterate(
107
+ limit=limit,
108
+ params={"all": all_groups} if all_groups else None,
109
+ )
110
+
111
+ def list(self, all_groups: bool = False, limit: int | None = None) -> list[GroupResponse]:
112
+ """List all groups in CDF.
113
+
114
+ Args:
115
+ all_groups: Whether to return all groups (requires admin permissions).
116
+ limit: Maximum total number of groups to return.
117
+
118
+ Returns:
119
+ List of GroupResponse objects.
120
+ """
121
+ return self._list(limit=limit, params={"all": all_groups} if all_groups else None)
@@ -0,0 +1,139 @@
1
+ from collections.abc import Iterable, Sequence
2
+ from typing import Any, Literal
3
+
4
+ from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, PagedResponse, ResponseItems
5
+ from cognite_toolkit._cdf_tk.client.cdf_client.api import Endpoint
6
+ from cognite_toolkit._cdf_tk.client.http_client import HTTPClient, ItemsSuccessResponse2, SuccessResponse2
7
+ from cognite_toolkit._cdf_tk.client.request_classes.filters import InstanceFilter
8
+ from cognite_toolkit._cdf_tk.client.resource_classes.data_modeling import (
9
+ InstanceRequest,
10
+ InstanceResponse,
11
+ ViewReference,
12
+ )
13
+ from cognite_toolkit._cdf_tk.client.resource_classes.instance_api import TypedInstanceIdentifier
14
+
15
+
16
+ class InstancesAPI(CDFResourceAPI[TypedInstanceIdentifier, InstanceRequest, InstanceResponse]):
17
+ def __init__(self, http_client: HTTPClient) -> None:
18
+ super().__init__(
19
+ http_client=http_client,
20
+ method_endpoint_map={
21
+ "upsert": Endpoint(method="POST", path="/models/instances", item_limit=1000),
22
+ "retrieve": Endpoint(method="POST", path="/models/instances/byids", item_limit=1000),
23
+ "delete": Endpoint(method="POST", path="/models/instances/delete", item_limit=1000),
24
+ "list": Endpoint(method="POST", path="/models/instances/list", item_limit=1000),
25
+ },
26
+ )
27
+
28
+ def _validate_page_response(
29
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
30
+ ) -> PagedResponse[InstanceResponse]:
31
+ return PagedResponse[InstanceResponse].model_validate_json(response.body)
32
+
33
+ def _validate_response(self, response: SuccessResponse2) -> ResponseItems[TypedInstanceIdentifier]:
34
+ return ResponseItems[TypedInstanceIdentifier].model_validate_json(response.body)
35
+
36
+ def create(self, items: Sequence[InstanceRequest]) -> list[InstanceResponse]:
37
+ """Create instances in CDF.
38
+
39
+ Args:
40
+ items: List of InstanceRequest objects to create.
41
+ Returns:
42
+ List of created InstanceResponse objects.
43
+ """
44
+ return self._request_item_response(items, "upsert")
45
+
46
+ def retrieve(
47
+ self, items: Sequence[TypedInstanceIdentifier], source: ViewReference | None = None
48
+ ) -> list[InstanceResponse]:
49
+ """Retrieve instances from CDF.
50
+
51
+ Args:
52
+ items: List of ExternalId objects to retrieve.
53
+ source: Optional ViewReference to specify the source view for the instances.
54
+ Returns:
55
+ List of retrieved InstanceResponse objects.
56
+ """
57
+ return self._request_item_response(
58
+ items, method="retrieve", extra_body={"sources": [{"source": source.dump()}]} if source else None
59
+ )
60
+
61
+ def delete(self, items: Sequence[TypedInstanceIdentifier]) -> list[TypedInstanceIdentifier]:
62
+ """Delete instances from CDF.
63
+
64
+ Args:
65
+ items: List of TypedInstanceIdentifier objects to delete.
66
+ """
67
+ response_items: list[TypedInstanceIdentifier] = []
68
+ for response in self._chunk_requests(items, "delete", self._serialize_items):
69
+ response_items.extend(self._validate_response(response).items)
70
+ return response_items
71
+
72
+ @staticmethod
73
+ def _create_sort_body(instance_type: Literal["node", "edge"] | None) -> list[dict]:
74
+ """We sort by space and externalId to get a stable sort order.
75
+
76
+ This is also more performant than sorting by using the default sort, which will sort on
77
+ internal CDF IDs. This will be slow if you have deleted a lot of instances, as they will be counted.
78
+ By sorting on space and externalId, we avoid this issue.
79
+ """
80
+ instance_type = instance_type or "node"
81
+ return [
82
+ {
83
+ "property": [instance_type, "space"],
84
+ "direction": "ascending",
85
+ },
86
+ {
87
+ "property": [instance_type, "externalId"],
88
+ "direction": "ascending",
89
+ },
90
+ ]
91
+
92
+ @classmethod
93
+ def _create_body(cls, filter: InstanceFilter | None) -> dict[str, Any]:
94
+ return {
95
+ **(filter.model_dump(exclude_none=True) if filter else {}),
96
+ "sort": cls._create_sort_body(filter.instance_type if filter else "node"),
97
+ }
98
+
99
+ def paginate(
100
+ self,
101
+ filter: InstanceFilter | None = None,
102
+ limit: int = 100,
103
+ cursor: str | None = None,
104
+ ) -> PagedResponse[InstanceResponse]:
105
+ """Iterate over all instances in CDF.
106
+
107
+ Args:
108
+ filter: InstanceFilter to filter instances.
109
+ limit: Maximum number of items to return.
110
+ cursor: Cursor for pagination.
111
+
112
+ Returns:
113
+ PagedResponse of InstanceResponse objects.
114
+ """
115
+ return self._paginate(
116
+ cursor=cursor,
117
+ limit=limit,
118
+ body=self._create_body(filter),
119
+ )
120
+
121
+ def iterate(self, filter: InstanceFilter | None = None, limit: int = 100) -> Iterable[list[InstanceResponse]]:
122
+ """Iterate over all instances in CDF.
123
+
124
+ Args:
125
+ filter: InstanceFilter to filter instances.
126
+ limit: Maximum number of items to return per page.
127
+
128
+ Returns:
129
+ Iterable of lists of InstanceResponse objects.
130
+ """
131
+ return self._iterate(limit=limit, body=self._create_body(filter))
132
+
133
+ def list(self, filter: InstanceFilter | None = None, limit: int | None = 100) -> list[InstanceResponse]:
134
+ """List all instances in CDF.
135
+
136
+ Returns:
137
+ List of InstanceResponse objects.
138
+ """
139
+ return self._list(limit=limit, body=self._create_body(filter))