cognite-toolkit 0.7.46__py3-none-any.whl → 0.7.47__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 (28) hide show
  1. cognite_toolkit/_cdf_tk/client/api/agents.py +107 -0
  2. cognite_toolkit/_cdf_tk/client/api/annotations.py +129 -0
  3. cognite_toolkit/_cdf_tk/client/api/containers.py +132 -0
  4. cognite_toolkit/_cdf_tk/client/api/data_models.py +137 -0
  5. cognite_toolkit/_cdf_tk/client/api/function_schedules.py +115 -0
  6. cognite_toolkit/_cdf_tk/client/api/functions.py +113 -0
  7. cognite_toolkit/_cdf_tk/client/api/graphql_data_models.py +167 -0
  8. cognite_toolkit/_cdf_tk/client/api/groups.py +121 -0
  9. cognite_toolkit/_cdf_tk/client/api/relationships.py +133 -0
  10. cognite_toolkit/_cdf_tk/client/api/spaces.py +117 -0
  11. cognite_toolkit/_cdf_tk/client/api/views.py +139 -0
  12. cognite_toolkit/_cdf_tk/client/cdf_client/api.py +8 -2
  13. cognite_toolkit/_cdf_tk/client/cdf_client/responses.py +11 -0
  14. cognite_toolkit/_cdf_tk/client/http_client/_data_classes.py +10 -0
  15. cognite_toolkit/_cdf_tk/client/request_classes/filters.py +31 -0
  16. cognite_toolkit/_cdf_tk/client/request_classes/graphql.py +28 -0
  17. cognite_toolkit/_cdf_tk/client/resource_classes/agent.py +8 -2
  18. cognite_toolkit/_cdf_tk/client/resource_classes/function_schedule.py +8 -4
  19. cognite_toolkit/_cdf_tk/client/resource_classes/group/group.py +9 -5
  20. cognite_toolkit/_cdf_tk/client/resource_classes/relationship.py +9 -3
  21. cognite_toolkit/_repo_files/GitHub/.github/workflows/deploy.yaml +1 -1
  22. cognite_toolkit/_repo_files/GitHub/.github/workflows/dry-run.yaml +1 -1
  23. cognite_toolkit/_resources/cdf.toml +1 -1
  24. cognite_toolkit/_version.py +1 -1
  25. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.47.dist-info}/METADATA +1 -1
  26. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.47.dist-info}/RECORD +28 -16
  27. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.47.dist-info}/WHEEL +0 -0
  28. {cognite_toolkit-0.7.46.dist-info → cognite_toolkit-0.7.47.dist-info}/entry_points.txt +0 -0
@@ -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,133 @@
1
+ """Relationships API for managing CDF relationships.
2
+
3
+ Based on the API specification at:
4
+ https://api-docs.cognite.com/20230101/tag/Relationships/operation/createRelationships
5
+ """
6
+
7
+ from collections.abc import Iterable, Sequence
8
+ from typing import Literal
9
+
10
+ from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, Endpoint, PagedResponse
11
+ from cognite_toolkit._cdf_tk.client.http_client import HTTPClient, ItemsSuccessResponse2, SuccessResponse2
12
+ from cognite_toolkit._cdf_tk.client.resource_classes.identifiers import ExternalId
13
+ from cognite_toolkit._cdf_tk.client.resource_classes.relationship import RelationshipRequest, RelationshipResponse
14
+
15
+
16
+ class RelationshipsAPI(CDFResourceAPI[ExternalId, RelationshipRequest, RelationshipResponse]):
17
+ """API for managing CDF relationships."""
18
+
19
+ def __init__(self, http_client: HTTPClient) -> None:
20
+ super().__init__(
21
+ http_client=http_client,
22
+ method_endpoint_map={
23
+ "create": Endpoint(method="POST", path="/relationships", item_limit=1000),
24
+ "retrieve": Endpoint(method="POST", path="/relationships/byids", item_limit=1000),
25
+ "update": Endpoint(method="POST", path="/relationships/update", item_limit=1000),
26
+ "delete": Endpoint(method="POST", path="/relationships/delete", item_limit=1000),
27
+ "list": Endpoint(method="POST", path="/relationships/list", item_limit=1000),
28
+ },
29
+ )
30
+
31
+ def _validate_page_response(
32
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
33
+ ) -> PagedResponse[RelationshipResponse]:
34
+ return PagedResponse[RelationshipResponse].model_validate_json(response.body)
35
+
36
+ def create(self, items: Sequence[RelationshipRequest]) -> list[RelationshipResponse]:
37
+ """Create relationships in CDF.
38
+
39
+ Args:
40
+ items: List of RelationshipRequest objects to create.
41
+
42
+ Returns:
43
+ List of created RelationshipResponse objects.
44
+ """
45
+ return self._request_item_response(items, "create")
46
+
47
+ def retrieve(
48
+ self, items: Sequence[ExternalId], ignore_unknown_ids: bool = False, fetch_resources: bool = False
49
+ ) -> list[RelationshipResponse]:
50
+ """Retrieve relationships from CDF.
51
+
52
+ Args:
53
+ items: List of ExternalId objects to retrieve.
54
+ ignore_unknown_ids: Whether to ignore unknown IDs.
55
+ fetch_resources: If true, will try to fetch the resources referred to in the relationship,
56
+ based on the users access rights. Will silently fail to attach the resources
57
+ if the user lacks access to some of them.
58
+
59
+ Returns:
60
+ List of retrieved RelationshipResponse objects.
61
+ """
62
+ return self._request_item_response(
63
+ items,
64
+ method="retrieve",
65
+ extra_body={"ignoreUnknownIds": ignore_unknown_ids, "fetchResources": fetch_resources},
66
+ )
67
+
68
+ def update(
69
+ self, items: Sequence[RelationshipRequest], mode: Literal["patch", "replace"] = "replace"
70
+ ) -> list[RelationshipResponse]:
71
+ """Update relationships in CDF.
72
+
73
+ Args:
74
+ items: List of RelationshipRequest objects to update.
75
+ mode: Update mode, either "patch" or "replace".
76
+
77
+ Returns:
78
+ List of updated RelationshipResponse objects.
79
+ """
80
+ return self._update(items, mode=mode)
81
+
82
+ def delete(self, items: Sequence[ExternalId], ignore_unknown_ids: bool = False) -> None:
83
+ """Delete relationships from CDF.
84
+
85
+ Args:
86
+ items: List of ExternalId objects to delete.
87
+ ignore_unknown_ids: Whether to ignore unknown IDs.
88
+ """
89
+ self._request_no_response(items, "delete", extra_body={"ignoreUnknownIds": ignore_unknown_ids})
90
+
91
+ def paginate(
92
+ self,
93
+ limit: int = 100,
94
+ cursor: str | None = None,
95
+ ) -> PagedResponse[RelationshipResponse]:
96
+ """Get a page of relationships from CDF.
97
+
98
+ Args:
99
+ limit: Maximum number of relationships to return.
100
+ cursor: Cursor for pagination.
101
+
102
+ Returns:
103
+ PagedResponse of RelationshipResponse objects.
104
+ """
105
+ return self._paginate(
106
+ cursor=cursor,
107
+ limit=limit,
108
+ )
109
+
110
+ def iterate(
111
+ self,
112
+ limit: int | None = None,
113
+ ) -> Iterable[list[RelationshipResponse]]:
114
+ """Iterate over all relationships in CDF.
115
+
116
+ Args:
117
+ limit: Maximum total number of relationships to return.
118
+
119
+ Returns:
120
+ Iterable of lists of RelationshipResponse objects.
121
+ """
122
+ return self._iterate(limit=limit)
123
+
124
+ def list(self, limit: int | None = None) -> list[RelationshipResponse]:
125
+ """List all relationships in CDF.
126
+
127
+ Args:
128
+ limit: Maximum total number of relationships to return.
129
+
130
+ Returns:
131
+ List of RelationshipResponse objects.
132
+ """
133
+ return self._list(limit=limit)
@@ -0,0 +1,117 @@
1
+ """Spaces API for managing CDF data modeling spaces.
2
+
3
+ Based on the API specification at:
4
+ https://api-docs.cognite.com/20230101/tag/Spaces/operation/ApplySpaces
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.data_modeling import SpaceReference, SpaceRequest, SpaceResponse
12
+
13
+
14
+ class SpacesAPI(CDFResourceAPI[SpaceReference, SpaceRequest, SpaceResponse]):
15
+ """API for managing CDF data modeling spaces.
16
+
17
+ Spaces use an apply/upsert pattern for create and update operations.
18
+ """
19
+
20
+ def __init__(self, http_client: HTTPClient) -> None:
21
+ super().__init__(
22
+ http_client=http_client,
23
+ method_endpoint_map={
24
+ "upsert": Endpoint(method="POST", path="/models/spaces", item_limit=100),
25
+ "retrieve": Endpoint(method="POST", path="/models/spaces/byids", item_limit=100),
26
+ "delete": Endpoint(method="POST", path="/models/spaces/delete", item_limit=100),
27
+ "list": Endpoint(method="GET", path="/models/spaces", item_limit=1000),
28
+ },
29
+ )
30
+
31
+ def _validate_page_response(
32
+ self, response: SuccessResponse2 | ItemsSuccessResponse2
33
+ ) -> PagedResponse[SpaceResponse]:
34
+ return PagedResponse[SpaceResponse].model_validate_json(response.body)
35
+
36
+ def apply(self, items: Sequence[SpaceRequest]) -> list[SpaceResponse]:
37
+ """Apply (create or update) spaces in CDF.
38
+
39
+ Args:
40
+ items: List of SpaceRequest objects to apply.
41
+
42
+ Returns:
43
+ List of applied SpaceResponse objects.
44
+ """
45
+ return self._request_item_response(items, "upsert")
46
+
47
+ def retrieve(self, items: Sequence[SpaceReference]) -> list[SpaceResponse]:
48
+ """Retrieve spaces from CDF.
49
+
50
+ Args:
51
+ items: List of SpaceReference objects to retrieve.
52
+
53
+ Returns:
54
+ List of retrieved SpaceResponse objects.
55
+ """
56
+ return self._request_item_response(items, method="retrieve")
57
+
58
+ def delete(self, items: Sequence[SpaceReference]) -> None:
59
+ """Delete spaces from CDF.
60
+
61
+ Args:
62
+ items: List of SpaceReference objects to delete.
63
+ """
64
+ self._request_no_response(items, "delete")
65
+
66
+ def paginate(
67
+ self,
68
+ include_global: bool = False,
69
+ limit: int = 100,
70
+ cursor: str | None = None,
71
+ ) -> PagedResponse[SpaceResponse]:
72
+ """Get a page of spaces from CDF.
73
+
74
+ Args:
75
+ include_global: Whether to include global spaces.
76
+ limit: Maximum number of spaces to return.
77
+ cursor: Cursor for pagination.
78
+
79
+ Returns:
80
+ PagedResponse of SpaceResponse objects.
81
+ """
82
+ return self._paginate(
83
+ cursor=cursor,
84
+ limit=limit,
85
+ params={"includeGlobal": include_global},
86
+ )
87
+
88
+ def iterate(
89
+ self,
90
+ include_global: bool = False,
91
+ limit: int | None = None,
92
+ ) -> Iterable[list[SpaceResponse]]:
93
+ """Iterate over all spaces in CDF.
94
+
95
+ Args:
96
+ include_global: Whether to include global spaces.
97
+ limit: Maximum total number of spaces to return.
98
+
99
+ Returns:
100
+ Iterable of lists of SpaceResponse objects.
101
+ """
102
+ return self._iterate(
103
+ limit=limit,
104
+ params={"includeGlobal": include_global},
105
+ )
106
+
107
+ def list(self, include_global: bool = False, limit: int | None = None) -> list[SpaceResponse]:
108
+ """List all spaces in CDF.
109
+
110
+ Args:
111
+ include_global: Whether to include global spaces.
112
+ limit: Maximum total number of spaces to return.
113
+
114
+ Returns:
115
+ List of SpaceResponse objects.
116
+ """
117
+ return self._list(limit=limit, params={"includeGlobal": include_global})