cognite-neat 1.0.24__py3-none-any.whl → 1.0.26__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.
- cognite/neat/__init__.py +1 -1
- cognite/neat/_client/api.py +173 -3
- cognite/neat/_client/containers_api.py +29 -90
- cognite/neat/_client/data_model_api.py +46 -74
- cognite/neat/_client/filters.py +40 -0
- cognite/neat/_client/init/credentials.py +0 -10
- cognite/neat/_client/init/env_vars.py +34 -22
- cognite/neat/_client/init/interactive.py +133 -0
- cognite/neat/_client/init/main.py +25 -15
- cognite/neat/_client/spaces_api.py +32 -67
- cognite/neat/_client/statistics_api.py +9 -4
- cognite/neat/_client/views_api.py +39 -89
- cognite/neat/_data_model/models/dms/_base.py +3 -0
- cognite/neat/_session/_session.py +13 -1
- cognite/neat/_utils/http_client/_client.py +3 -1
- cognite/neat/_utils/http_client/_data_classes.py +3 -3
- cognite/neat/_utils/repo.py +6 -4
- cognite/neat/_version.py +1 -1
- {cognite_neat-1.0.24.dist-info → cognite_neat-1.0.26.dist-info}/METADATA +1 -1
- {cognite_neat-1.0.24.dist-info → cognite_neat-1.0.26.dist-info}/RECORD +21 -19
- {cognite_neat-1.0.24.dist-info → cognite_neat-1.0.26.dist-info}/WHEEL +0 -0
cognite/neat/__init__.py
CHANGED
cognite/neat/_client/api.py
CHANGED
|
@@ -1,8 +1,178 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from collections.abc import Iterable, Sequence
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Generic, Literal, TypeAlias, TypeVar
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, JsonValue, TypeAdapter
|
|
7
|
+
|
|
1
8
|
from cognite.neat._client.config import NeatClientConfig
|
|
2
|
-
from cognite.neat.
|
|
9
|
+
from cognite.neat._data_model.models.dms._base import T_Resource, T_Response
|
|
10
|
+
from cognite.neat._utils.collection import chunker_sequence
|
|
11
|
+
from cognite.neat._utils.http_client import HTTPClient, ParametersRequest, SimpleBodyRequest, SuccessResponse
|
|
12
|
+
from cognite.neat._utils.useful_types import T_Reference
|
|
13
|
+
|
|
14
|
+
from .data_classes import PagedResponse
|
|
15
|
+
|
|
16
|
+
_T_BaseModel = TypeVar("_T_BaseModel", bound=BaseModel)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class Endpoint:
|
|
21
|
+
method: Literal["GET", "POST"]
|
|
22
|
+
path: str
|
|
23
|
+
item_limit: int
|
|
3
24
|
|
|
4
25
|
|
|
5
|
-
|
|
6
|
-
|
|
26
|
+
APIMethod: TypeAlias = Literal["apply", "retrieve", "delete", "list"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class NeatAPI(Generic[T_Reference, T_Resource, T_Response], ABC):
|
|
30
|
+
def __init__(
|
|
31
|
+
self, neat_config: NeatClientConfig, http_client: HTTPClient, endpoint_map: dict[APIMethod, Endpoint]
|
|
32
|
+
) -> None:
|
|
7
33
|
self._config = neat_config
|
|
8
34
|
self._http_client = http_client
|
|
35
|
+
self._method_endpoint_map = endpoint_map
|
|
36
|
+
|
|
37
|
+
@abstractmethod
|
|
38
|
+
def _validate_page_response(self, response: SuccessResponse) -> PagedResponse[T_Response]:
|
|
39
|
+
"""Parse a single item response."""
|
|
40
|
+
raise NotImplementedError()
|
|
41
|
+
|
|
42
|
+
@abstractmethod
|
|
43
|
+
def _validate_id_response(self, response: SuccessResponse) -> list[T_Reference]:
|
|
44
|
+
"""Parse a single item response."""
|
|
45
|
+
raise NotImplementedError()
|
|
46
|
+
|
|
47
|
+
def _make_url(self, path: str = "") -> str:
|
|
48
|
+
"""Create the full URL for this resource endpoint."""
|
|
49
|
+
return self._config.create_api_url(path)
|
|
50
|
+
|
|
51
|
+
def _request_item_response(
|
|
52
|
+
self,
|
|
53
|
+
items: Sequence[BaseModel],
|
|
54
|
+
method: APIMethod,
|
|
55
|
+
extra_body: dict[str, Any] | None = None,
|
|
56
|
+
) -> list[T_Response]:
|
|
57
|
+
response_items: list[T_Response] = []
|
|
58
|
+
for response in self._chunk_requests(items, method, extra_body):
|
|
59
|
+
response_items.extend(self._validate_page_response(response).items)
|
|
60
|
+
return response_items
|
|
61
|
+
|
|
62
|
+
def _request_id_response(
|
|
63
|
+
self,
|
|
64
|
+
items: Sequence[BaseModel],
|
|
65
|
+
method: APIMethod,
|
|
66
|
+
extra_body: dict[str, Any] | None = None,
|
|
67
|
+
) -> list[T_Reference]:
|
|
68
|
+
response_items: list[T_Reference] = []
|
|
69
|
+
for response in self._chunk_requests(items, method, extra_body):
|
|
70
|
+
response_items.extend(self._validate_id_response(response))
|
|
71
|
+
return response_items
|
|
72
|
+
|
|
73
|
+
def _chunk_requests(
|
|
74
|
+
self,
|
|
75
|
+
items: Sequence[_T_BaseModel],
|
|
76
|
+
method: APIMethod,
|
|
77
|
+
extra_body: dict[str, Any] | None = None,
|
|
78
|
+
) -> Iterable[SuccessResponse]:
|
|
79
|
+
"""Send requests in chunks and yield responses.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
items: The items to process.
|
|
83
|
+
method: The API method to use. This is used ot look the up the endpoint.
|
|
84
|
+
extra_body: Optional extra body content to include in the request.
|
|
85
|
+
|
|
86
|
+
Yields:
|
|
87
|
+
The successful responses from the API.
|
|
88
|
+
"""
|
|
89
|
+
# Filter out None
|
|
90
|
+
endpoint = self._method_endpoint_map[method]
|
|
91
|
+
|
|
92
|
+
for chunk in chunker_sequence(items, endpoint.item_limit):
|
|
93
|
+
request = SimpleBodyRequest(
|
|
94
|
+
endpoint_url=self._make_url(endpoint.path),
|
|
95
|
+
method=endpoint.method,
|
|
96
|
+
body=TypeAdapter(dict[str, JsonValue]).dump_json(
|
|
97
|
+
{
|
|
98
|
+
"items": [item.model_dump(by_alias=True) for item in chunk],
|
|
99
|
+
**(extra_body or {}),
|
|
100
|
+
},
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
response = self._http_client.request_with_retries(request)
|
|
104
|
+
response.raise_for_status()
|
|
105
|
+
yield response.success_response
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def _filter_out_none_values(cls, params: dict[str, Any] | None) -> dict[str, Any] | None:
|
|
109
|
+
request_params: dict[str, Any] | None = None
|
|
110
|
+
if params:
|
|
111
|
+
request_params = {k: v for k, v in params.items() if v is not None}
|
|
112
|
+
return request_params
|
|
113
|
+
|
|
114
|
+
def _paginate(
|
|
115
|
+
self,
|
|
116
|
+
limit: int,
|
|
117
|
+
cursor: str | None = None,
|
|
118
|
+
params: dict[str, Any] | None = None,
|
|
119
|
+
) -> PagedResponse[T_Response]:
|
|
120
|
+
"""Fetch a single page of resources.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
params: Query parameters for the request. Supported parameters depend on
|
|
124
|
+
the resource type but typically include:
|
|
125
|
+
- cursor: Cursor for pagination
|
|
126
|
+
- limit: Maximum number of items (defaults to list limit)
|
|
127
|
+
- space: Filter by space
|
|
128
|
+
- includeGlobal: Whether to include global resources
|
|
129
|
+
limit: Maximum number of items to return in the page.
|
|
130
|
+
cursor: Cursor for pagination.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
A Page containing the items and the cursor for the next page.
|
|
134
|
+
"""
|
|
135
|
+
endpoint = self._method_endpoint_map["list"]
|
|
136
|
+
if not (0 < limit <= endpoint.item_limit):
|
|
137
|
+
raise ValueError(f"Limit must be between 1 and {endpoint.item_limit}, got {limit}.")
|
|
138
|
+
if endpoint.method != "GET":
|
|
139
|
+
raise NotImplementedError(f"Pagination not implemented for method {endpoint.method}.")
|
|
140
|
+
request_params = self._filter_out_none_values(params) or {}
|
|
141
|
+
request_params["limit"] = limit
|
|
142
|
+
if cursor is not None:
|
|
143
|
+
request_params["cursor"] = cursor
|
|
144
|
+
request = ParametersRequest(
|
|
145
|
+
endpoint_url=self._make_url(endpoint.path),
|
|
146
|
+
method=endpoint.method,
|
|
147
|
+
parameters=request_params,
|
|
148
|
+
)
|
|
149
|
+
result = self._http_client.request_with_retries(request)
|
|
150
|
+
result.raise_for_status()
|
|
151
|
+
return self._validate_page_response(result.success_response)
|
|
152
|
+
|
|
153
|
+
def _iterate(
|
|
154
|
+
self,
|
|
155
|
+
limit: int | None = None,
|
|
156
|
+
cursor: str | None = None,
|
|
157
|
+
params: dict[str, Any] | None = None,
|
|
158
|
+
) -> Iterable[list[T_Response]]:
|
|
159
|
+
"""Iterate over all resources, handling pagination automatically."""
|
|
160
|
+
next_cursor = cursor
|
|
161
|
+
total = 0
|
|
162
|
+
endpoint = self._method_endpoint_map["list"]
|
|
163
|
+
while True:
|
|
164
|
+
page_limit = endpoint.item_limit if limit is None else min(limit - total, endpoint.item_limit)
|
|
165
|
+
page = self._paginate(limit=page_limit, cursor=next_cursor, params=params)
|
|
166
|
+
yield page.items
|
|
167
|
+
total += len(page.items)
|
|
168
|
+
if page.next_cursor is None or (limit is not None and total >= limit):
|
|
169
|
+
break
|
|
170
|
+
next_cursor = page.next_cursor
|
|
171
|
+
|
|
172
|
+
def _list(
|
|
173
|
+
self,
|
|
174
|
+
limit: int | None = None,
|
|
175
|
+
params: dict[str, Any] | None = None,
|
|
176
|
+
) -> list[T_Response]:
|
|
177
|
+
"""List all resources, handling pagination automatically."""
|
|
178
|
+
return [item for batch in self._iterate(limit=limit, params=params) for item in batch]
|
|
@@ -1,18 +1,32 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
1
|
from collections.abc import Sequence
|
|
4
2
|
|
|
5
|
-
from cognite.neat._data_model.models.dms import ContainerReference, ContainerRequest, ContainerResponse
|
|
6
|
-
from cognite.neat._utils.http_client import
|
|
7
|
-
from cognite.neat._utils.useful_types import PrimitiveType
|
|
3
|
+
from cognite.neat._data_model.models.dms import ContainerReference, ContainerRequest, ContainerResponse
|
|
4
|
+
from cognite.neat._utils.http_client import HTTPClient, SuccessResponse
|
|
8
5
|
|
|
9
|
-
from .api import NeatAPI
|
|
6
|
+
from .api import Endpoint, NeatAPI
|
|
7
|
+
from .config import NeatClientConfig
|
|
10
8
|
from .data_classes import PagedResponse
|
|
9
|
+
from .filters import ContainerFilter
|
|
11
10
|
|
|
12
11
|
|
|
13
12
|
class ContainersAPI(NeatAPI):
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
def __init__(self, neat_config: NeatClientConfig, http_client: HTTPClient) -> None:
|
|
14
|
+
super().__init__(
|
|
15
|
+
neat_config,
|
|
16
|
+
http_client,
|
|
17
|
+
endpoint_map={
|
|
18
|
+
"apply": Endpoint("POST", "models/containers", item_limit=100),
|
|
19
|
+
"retrieve": Endpoint("POST", "models/containers/byids", item_limit=100),
|
|
20
|
+
"delete": Endpoint("POST", "models/containers/delete", item_limit=100),
|
|
21
|
+
"list": Endpoint("GET", "models/containers", item_limit=1000),
|
|
22
|
+
},
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
def _validate_page_response(self, response: SuccessResponse) -> PagedResponse[ContainerResponse]:
|
|
26
|
+
return PagedResponse[ContainerResponse].model_validate_json(response.body)
|
|
27
|
+
|
|
28
|
+
def _validate_id_response(self, response: SuccessResponse) -> list[ContainerReference]:
|
|
29
|
+
return PagedResponse[ContainerReference].model_validate_json(response.body).items
|
|
16
30
|
|
|
17
31
|
def apply(self, items: Sequence[ContainerRequest]) -> list[ContainerResponse]:
|
|
18
32
|
"""Apply (create or update) containers in CDF.
|
|
@@ -22,25 +36,9 @@ class ContainersAPI(NeatAPI):
|
|
|
22
36
|
Returns:
|
|
23
37
|
List of ContainerResponse objects.
|
|
24
38
|
"""
|
|
25
|
-
|
|
26
|
-
return []
|
|
27
|
-
if len(items) > 100:
|
|
28
|
-
raise ValueError("Cannot apply more than 100 containers at once.")
|
|
29
|
-
result = self._http_client.request_with_retries(
|
|
30
|
-
ItemsRequest(
|
|
31
|
-
endpoint_url=self._config.create_api_url(self.ENDPOINT),
|
|
32
|
-
method="POST",
|
|
33
|
-
body=DataModelBody(items=items),
|
|
34
|
-
)
|
|
35
|
-
)
|
|
36
|
-
result.raise_for_status()
|
|
37
|
-
result = PagedResponse[ContainerResponse].model_validate_json(result.success_response.body)
|
|
38
|
-
return result.items
|
|
39
|
+
return self._request_item_response(items, "apply")
|
|
39
40
|
|
|
40
|
-
def retrieve(
|
|
41
|
-
self,
|
|
42
|
-
items: list[ContainerReference],
|
|
43
|
-
) -> list[ContainerResponse]:
|
|
41
|
+
def retrieve(self, items: list[ContainerReference]) -> list[ContainerResponse]:
|
|
44
42
|
"""Retrieve containers by their identifiers.
|
|
45
43
|
|
|
46
44
|
Args:
|
|
@@ -49,21 +47,7 @@ class ContainersAPI(NeatAPI):
|
|
|
49
47
|
Returns:
|
|
50
48
|
List of ContainerResponse objects.
|
|
51
49
|
"""
|
|
52
|
-
|
|
53
|
-
return []
|
|
54
|
-
if len(items) > 1000:
|
|
55
|
-
raise ValueError("Cannot retrieve more than 1000 containers at once.")
|
|
56
|
-
|
|
57
|
-
result = self._http_client.request_with_retries(
|
|
58
|
-
ItemsRequest(
|
|
59
|
-
endpoint_url=self._config.create_api_url(f"{self.ENDPOINT}/byids"),
|
|
60
|
-
method="POST",
|
|
61
|
-
body=ItemIDBody(items=items),
|
|
62
|
-
)
|
|
63
|
-
)
|
|
64
|
-
result.raise_for_status()
|
|
65
|
-
result = PagedResponse[ContainerResponse].model_validate_json(result.success_response.body)
|
|
66
|
-
return result.items
|
|
50
|
+
return self._request_item_response(items, "retrieve")
|
|
67
51
|
|
|
68
52
|
def delete(self, items: list[ContainerReference]) -> list[ContainerReference]:
|
|
69
53
|
"""Delete containers by their identifiers.
|
|
@@ -74,27 +58,10 @@ class ContainersAPI(NeatAPI):
|
|
|
74
58
|
Returns:
|
|
75
59
|
List of ContainerReference objects representing the deleted containers.
|
|
76
60
|
"""
|
|
77
|
-
|
|
78
|
-
return []
|
|
79
|
-
if len(items) > 100:
|
|
80
|
-
raise ValueError("Cannot delete more than 100 containers at once.")
|
|
81
|
-
|
|
82
|
-
result = self._http_client.request_with_retries(
|
|
83
|
-
ItemsRequest(
|
|
84
|
-
endpoint_url=self._config.create_api_url(f"{self.ENDPOINT}/delete"),
|
|
85
|
-
method="POST",
|
|
86
|
-
body=ItemIDBody(items=items),
|
|
87
|
-
)
|
|
88
|
-
)
|
|
89
|
-
result.raise_for_status()
|
|
90
|
-
result = PagedResponse[ContainerReference].model_validate_json(result.success_response.body)
|
|
91
|
-
return result.items
|
|
61
|
+
return self._request_id_response(items, "delete")
|
|
92
62
|
|
|
93
63
|
def list(
|
|
94
|
-
self,
|
|
95
|
-
space: str | None = None,
|
|
96
|
-
include_global: bool = False,
|
|
97
|
-
limit: int | None = 10,
|
|
64
|
+
self, space: str | None = None, include_global: bool = False, limit: int | None = 10
|
|
98
65
|
) -> list[ContainerResponse]:
|
|
99
66
|
"""List containers in CDF Project.
|
|
100
67
|
|
|
@@ -106,33 +73,5 @@ class ContainersAPI(NeatAPI):
|
|
|
106
73
|
Returns:
|
|
107
74
|
List of ContainerResponse objects.
|
|
108
75
|
"""
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
elif limit is not None and limit == 0:
|
|
112
|
-
return []
|
|
113
|
-
parameters: dict[str, PrimitiveType] = {"includeGlobal": include_global}
|
|
114
|
-
if space is not None:
|
|
115
|
-
parameters["space"] = space
|
|
116
|
-
cursor: str | None = None
|
|
117
|
-
container_responses: list[ContainerResponse] = []
|
|
118
|
-
while True:
|
|
119
|
-
if cursor is not None:
|
|
120
|
-
parameters["cursor"] = cursor
|
|
121
|
-
if limit is None:
|
|
122
|
-
parameters["limit"] = self.LIST_REQUEST_LIMIT
|
|
123
|
-
else:
|
|
124
|
-
parameters["limit"] = min(self.LIST_REQUEST_LIMIT, limit - len(container_responses))
|
|
125
|
-
result = self._http_client.request_with_retries(
|
|
126
|
-
ParametersRequest(
|
|
127
|
-
endpoint_url=self._config.create_api_url(self.ENDPOINT),
|
|
128
|
-
method="GET",
|
|
129
|
-
parameters=parameters,
|
|
130
|
-
)
|
|
131
|
-
)
|
|
132
|
-
result.raise_for_status()
|
|
133
|
-
result = PagedResponse[ContainerResponse].model_validate_json(result.success_response.body)
|
|
134
|
-
container_responses.extend(result.items)
|
|
135
|
-
cursor = result.next_cursor
|
|
136
|
-
if cursor is None or (limit is not None and len(container_responses) >= limit):
|
|
137
|
-
break
|
|
138
|
-
return container_responses
|
|
76
|
+
filter = ContainerFilter(space=space, include_global=include_global)
|
|
77
|
+
return self._list(limit=limit, params=filter.dump())
|
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
from collections.abc import Sequence
|
|
2
2
|
|
|
3
|
-
from cognite.neat._data_model.models.dms import
|
|
4
|
-
from cognite.neat._utils.http_client import
|
|
5
|
-
from cognite.neat._utils.useful_types import PrimitiveType
|
|
3
|
+
from cognite.neat._data_model.models.dms import DataModelReference, DataModelRequest, DataModelResponse
|
|
4
|
+
from cognite.neat._utils.http_client import HTTPClient, SuccessResponse
|
|
6
5
|
|
|
7
|
-
from .api import NeatAPI
|
|
6
|
+
from .api import Endpoint, NeatAPI
|
|
7
|
+
from .config import NeatClientConfig
|
|
8
8
|
from .data_classes import PagedResponse
|
|
9
|
+
from .filters import DataModelFilter
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class DataModelsAPI(NeatAPI):
|
|
12
|
-
|
|
13
|
+
def __init__(self, neat_config: NeatClientConfig, http_client: HTTPClient) -> None:
|
|
14
|
+
super().__init__(
|
|
15
|
+
neat_config,
|
|
16
|
+
http_client,
|
|
17
|
+
endpoint_map={
|
|
18
|
+
"apply": Endpoint("POST", "/models/datamodels", item_limit=100),
|
|
19
|
+
"retrieve": Endpoint("POST", "/models/datamodels/byids", item_limit=100),
|
|
20
|
+
"delete": Endpoint("POST", "/models/datamodels/delete", item_limit=100),
|
|
21
|
+
"list": Endpoint("GET", "/models/datamodels", item_limit=1000),
|
|
22
|
+
},
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
def _validate_page_response(self, response: SuccessResponse) -> PagedResponse[DataModelResponse]:
|
|
26
|
+
return PagedResponse[DataModelResponse].model_validate_json(response.body)
|
|
27
|
+
|
|
28
|
+
def _validate_id_response(self, response: SuccessResponse) -> list[DataModelReference]:
|
|
29
|
+
return PagedResponse[DataModelReference].model_validate_json(response.body).items
|
|
13
30
|
|
|
14
31
|
def apply(self, data_models: Sequence[DataModelRequest]) -> list[DataModelResponse]:
|
|
15
32
|
"""Apply (create or update) data models in CDF.
|
|
@@ -19,26 +36,9 @@ class DataModelsAPI(NeatAPI):
|
|
|
19
36
|
Returns:
|
|
20
37
|
List of DataModelResponse objects.
|
|
21
38
|
"""
|
|
22
|
-
|
|
23
|
-
return []
|
|
24
|
-
if len(data_models) > 100:
|
|
25
|
-
raise ValueError("Cannot apply more than 100 data models at once.")
|
|
26
|
-
|
|
27
|
-
result = self._http_client.request_with_retries(
|
|
28
|
-
ItemsRequest(
|
|
29
|
-
endpoint_url=self._config.create_api_url(self.ENDPOINT),
|
|
30
|
-
method="POST",
|
|
31
|
-
body=DataModelBody(items=data_models),
|
|
32
|
-
)
|
|
33
|
-
)
|
|
34
|
-
result.raise_for_status()
|
|
35
|
-
result = PagedResponse[DataModelResponse].model_validate_json(result.success_response.body)
|
|
36
|
-
return result.items
|
|
39
|
+
return self._request_item_response(data_models, "apply")
|
|
37
40
|
|
|
38
|
-
def retrieve(
|
|
39
|
-
self,
|
|
40
|
-
items: list[DataModelReference],
|
|
41
|
-
) -> list[DataModelResponse]:
|
|
41
|
+
def retrieve(self, items: list[DataModelReference]) -> list[DataModelResponse]:
|
|
42
42
|
"""Retrieve data models by their identifiers.
|
|
43
43
|
|
|
44
44
|
Args:
|
|
@@ -46,21 +46,7 @@ class DataModelsAPI(NeatAPI):
|
|
|
46
46
|
Returns:
|
|
47
47
|
List of DataModelResponse objects.
|
|
48
48
|
"""
|
|
49
|
-
|
|
50
|
-
return []
|
|
51
|
-
if len(items) > 100:
|
|
52
|
-
raise ValueError("Cannot retrieve more than 1000 containers at once.")
|
|
53
|
-
|
|
54
|
-
result = self._http_client.request_with_retries(
|
|
55
|
-
ItemsRequest(
|
|
56
|
-
endpoint_url=self._config.create_api_url(f"{self.ENDPOINT}/byids"),
|
|
57
|
-
method="POST",
|
|
58
|
-
body=ItemIDBody(items=items),
|
|
59
|
-
)
|
|
60
|
-
)
|
|
61
|
-
result.raise_for_status()
|
|
62
|
-
result = PagedResponse[DataModelResponse].model_validate_json(result.success_response.body)
|
|
63
|
-
return result.items
|
|
49
|
+
return self._request_item_response(items, "retrieve")
|
|
64
50
|
|
|
65
51
|
def delete(self, items: list[DataModelReference]) -> list[DataModelReference]:
|
|
66
52
|
"""Delete data models by their identifiers.
|
|
@@ -70,46 +56,32 @@ class DataModelsAPI(NeatAPI):
|
|
|
70
56
|
Returns:
|
|
71
57
|
List of DataModelReference objects representing the deleted data models.
|
|
72
58
|
"""
|
|
73
|
-
|
|
74
|
-
return []
|
|
75
|
-
if len(items) > 100:
|
|
76
|
-
raise ValueError("Cannot delete more than 100 data models at once.")
|
|
77
|
-
|
|
78
|
-
result = self._http_client.request_with_retries(
|
|
79
|
-
ItemsRequest(
|
|
80
|
-
endpoint_url=self._config.create_api_url(f"{self.ENDPOINT}/delete"),
|
|
81
|
-
method="POST",
|
|
82
|
-
body=ItemIDBody(items=items),
|
|
83
|
-
)
|
|
84
|
-
)
|
|
85
|
-
result.raise_for_status()
|
|
86
|
-
result = PagedResponse[DataModelReference].model_validate_json(result.success_response.body)
|
|
87
|
-
return result.items
|
|
59
|
+
return self._request_id_response(items, "delete")
|
|
88
60
|
|
|
89
61
|
def list(
|
|
90
62
|
self,
|
|
91
63
|
space: str | None = None,
|
|
92
64
|
all_versions: bool = False,
|
|
65
|
+
inline_views: bool = False,
|
|
93
66
|
include_global: bool = False,
|
|
94
|
-
limit: int = 10,
|
|
67
|
+
limit: int | None = 10,
|
|
95
68
|
) -> list[DataModelResponse]:
|
|
96
|
-
"""List data models in CDF Project.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
69
|
+
"""List data models in CDF Project.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
space: If specified, only data models in this space are returned.
|
|
73
|
+
all_versions: If True, return all versions. If False, only return the latest version.
|
|
74
|
+
inline_views: If True, include views inline in the response.
|
|
75
|
+
include_global: If True, include global data models.
|
|
76
|
+
limit: Maximum number of data models to return. If None, return all data models.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
List of DataModelResponse objects.
|
|
80
|
+
"""
|
|
81
|
+
filter = DataModelFilter(
|
|
82
|
+
space=space,
|
|
83
|
+
all_versions=all_versions,
|
|
84
|
+
inline_views=inline_views,
|
|
85
|
+
include_global=include_global,
|
|
112
86
|
)
|
|
113
|
-
|
|
114
|
-
result = PagedResponse[DataModelResponse].model_validate_json(result.success_response.body)
|
|
115
|
-
return result.items
|
|
87
|
+
return self._list(limit=limit, params=filter.dump())
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from typing import Any, Literal
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, ConfigDict
|
|
4
|
+
from pydantic.alias_generators import to_camel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BaseModelRequest(BaseModel):
|
|
8
|
+
"""Base class for all object. This includes resources and nested objects."""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Filter(BaseModel):
|
|
12
|
+
model_config = ConfigDict(alias_generator=to_camel, extra="ignore", populate_by_name=True)
|
|
13
|
+
|
|
14
|
+
def dump(self, camel_case: bool = True) -> dict[str, Any]:
|
|
15
|
+
"""Dump the object to a dictionary.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
camel_case (bool): Whether to use camelCase for the keys. Default is True.
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
return self.model_dump(mode="json", by_alias=camel_case, exclude_unset=True)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class DataModelingFilter(Filter):
|
|
25
|
+
space: str | None = None
|
|
26
|
+
include_global: bool | None = None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ContainerFilter(DataModelingFilter):
|
|
30
|
+
used_for: Literal["node", "edge", "record", "all"] | None = None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ViewFilter(DataModelingFilter):
|
|
34
|
+
include_inherited_properties: bool | None = None
|
|
35
|
+
all_versions: bool | None = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class DataModelFilter(DataModelingFilter):
|
|
39
|
+
inline_views: bool | None = None
|
|
40
|
+
all_versions: bool | None = None
|
|
@@ -12,7 +12,6 @@ def get_credentials(env_vars: ClientEnvironmentVariables) -> CredentialProvider:
|
|
|
12
12
|
"client_credentials": create_client_credentials,
|
|
13
13
|
"interactive": create_interactive_credentials,
|
|
14
14
|
"token": create_token_credentials,
|
|
15
|
-
"infer": create_infer_credentials,
|
|
16
15
|
}
|
|
17
16
|
return options[env_vars.LOGIN_FLOW](env_vars)
|
|
18
17
|
|
|
@@ -59,12 +58,3 @@ def create_token_credentials(env_vars: ClientEnvironmentVariables) -> Credential
|
|
|
59
58
|
if not env_vars.CDF_TOKEN:
|
|
60
59
|
raise ValueError("CDF_TOKEN environment variable must be set for token authentication.")
|
|
61
60
|
return Token(env_vars.CDF_TOKEN)
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def create_infer_credentials(env_vars: ClientEnvironmentVariables) -> CredentialProvider:
|
|
65
|
-
if env_vars.IDP_CLIENT_SECRET:
|
|
66
|
-
return create_client_credentials(env_vars)
|
|
67
|
-
elif env_vars.CDF_TOKEN:
|
|
68
|
-
return create_token_credentials(env_vars)
|
|
69
|
-
else:
|
|
70
|
-
return create_interactive_credentials(env_vars)
|
|
@@ -4,7 +4,6 @@ from typing import Any, Literal, TypeAlias, get_args
|
|
|
4
4
|
|
|
5
5
|
from pydantic import BaseModel, ConfigDict, ValidationError
|
|
6
6
|
|
|
7
|
-
from cognite.neat._utils.repo import get_repo_root
|
|
8
7
|
from cognite.neat._utils.validation import humanize_validation_error
|
|
9
8
|
|
|
10
9
|
if sys.version_info >= (3, 11):
|
|
@@ -12,9 +11,10 @@ if sys.version_info >= (3, 11):
|
|
|
12
11
|
else:
|
|
13
12
|
from typing_extensions import Self
|
|
14
13
|
|
|
15
|
-
LoginFlow: TypeAlias = Literal["
|
|
16
|
-
|
|
14
|
+
LoginFlow: TypeAlias = Literal["client_credentials", "interactive", "token"]
|
|
15
|
+
AVAILABLE_LOGIN_FLOWS: tuple[LoginFlow, ...] = get_args(LoginFlow)
|
|
17
16
|
Provider: TypeAlias = Literal["entra_id", "auth0", "cdf", "other"]
|
|
17
|
+
AVAILABLE_PROVIDERS: tuple[Provider, ...] = get_args(Provider)
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
class ClientEnvironmentVariables(BaseModel):
|
|
@@ -24,7 +24,7 @@ class ClientEnvironmentVariables(BaseModel):
|
|
|
24
24
|
CDF_CLUSTER: str
|
|
25
25
|
CDF_PROJECT: str
|
|
26
26
|
PROVIDER: Provider = "entra_id"
|
|
27
|
-
LOGIN_FLOW: LoginFlow = "
|
|
27
|
+
LOGIN_FLOW: LoginFlow = "client_credentials"
|
|
28
28
|
|
|
29
29
|
IDP_CLIENT_ID: str | None = None
|
|
30
30
|
IDP_CLIENT_SECRET: str | None = None
|
|
@@ -103,24 +103,7 @@ class ClientEnvironmentVariables(BaseModel):
|
|
|
103
103
|
)
|
|
104
104
|
|
|
105
105
|
|
|
106
|
-
def
|
|
107
|
-
to_search: list[tuple[str, Path]] = []
|
|
108
|
-
try:
|
|
109
|
-
repo_root = get_repo_root()
|
|
110
|
-
except RuntimeError:
|
|
111
|
-
...
|
|
112
|
-
else:
|
|
113
|
-
to_search.append(("repository root", repo_root))
|
|
114
|
-
to_search.append(("current working directory", Path.cwd()))
|
|
115
|
-
for location_desc, path in to_search:
|
|
116
|
-
env_path = path / env_file_name
|
|
117
|
-
if env_path.is_file():
|
|
118
|
-
print(f"Found {env_file_name} in {location_desc}.")
|
|
119
|
-
return _parse_env_file(env_path)
|
|
120
|
-
raise FileNotFoundError(f"Could not find {env_file_name} in the repository root or current working directory.")
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def _parse_env_file(env_file_path: Path) -> ClientEnvironmentVariables:
|
|
106
|
+
def parse_env_file(env_file_path: Path) -> ClientEnvironmentVariables:
|
|
124
107
|
content = env_file_path.read_text()
|
|
125
108
|
variables: dict[str, Any] = {}
|
|
126
109
|
for line in content.splitlines():
|
|
@@ -129,3 +112,32 @@ def _parse_env_file(env_file_path: Path) -> ClientEnvironmentVariables:
|
|
|
129
112
|
key, value = line.strip().split("=", 1)
|
|
130
113
|
variables[key] = value
|
|
131
114
|
return ClientEnvironmentVariables.create_humanize(variables)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def create_env_file_content(provider: Provider, login_flow: LoginFlow) -> str:
|
|
118
|
+
lines = [
|
|
119
|
+
"# Cognite NEAT Client Environment Variables",
|
|
120
|
+
"CDF_CLUSTER=<your-cdf-cluster>",
|
|
121
|
+
"CDF_PROJECT=<your-cdf-project>",
|
|
122
|
+
"",
|
|
123
|
+
]
|
|
124
|
+
if login_flow != "token":
|
|
125
|
+
lines.append(f"PROVIDER={provider}")
|
|
126
|
+
lines.append(f"LOGIN_FLOW={login_flow}")
|
|
127
|
+
lines.append("")
|
|
128
|
+
if login_flow in ("client_credentials", "interactive"):
|
|
129
|
+
lines.append("IDP_CLIENT_ID=<your-idp-client-id>")
|
|
130
|
+
if login_flow == "client_credentials":
|
|
131
|
+
lines.append("IDP_CLIENT_SECRET=<your-idp-client-secret>")
|
|
132
|
+
if provider == "entra_id":
|
|
133
|
+
lines.append("IDP_TENANT_ID=<your-idp-tenant-id>")
|
|
134
|
+
if provider not in ("cdf", "entra_id"):
|
|
135
|
+
lines.append("IDP_TOKEN_URL=<your-idp-token-url>")
|
|
136
|
+
if provider == "other":
|
|
137
|
+
lines.append("IDP_AUDIENCE=<your-idp-audience>")
|
|
138
|
+
lines.append("IDP_SCOPES=<your-idp-scopes-comma-separated>")
|
|
139
|
+
lines.append("IDP_AUTHORITY_URL=<your-idp-authority-url>")
|
|
140
|
+
elif login_flow == "token":
|
|
141
|
+
lines.append("CDF_TOKEN=<your-cdf-token>")
|
|
142
|
+
|
|
143
|
+
return "\n".join(lines)
|