cognite-toolkit 0.7.54__py3-none-any.whl → 0.7.56__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 (39) hide show
  1. cognite_toolkit/_cdf_tk/apps/_download_app.py +19 -42
  2. cognite_toolkit/_cdf_tk/apps/_migrate_app.py +28 -36
  3. cognite_toolkit/_cdf_tk/apps/_purge.py +14 -15
  4. cognite_toolkit/_cdf_tk/apps/_upload_app.py +3 -9
  5. cognite_toolkit/_cdf_tk/client/http_client/__init__.py +0 -38
  6. cognite_toolkit/_cdf_tk/client/http_client/_client.py +4 -161
  7. cognite_toolkit/_cdf_tk/client/http_client/_data_classes2.py +18 -18
  8. cognite_toolkit/_cdf_tk/client/resource_classes/filemetadata.py +7 -1
  9. cognite_toolkit/_cdf_tk/commands/_migrate/command.py +8 -8
  10. cognite_toolkit/_cdf_tk/commands/_migrate/migration_io.py +26 -25
  11. cognite_toolkit/_cdf_tk/commands/_profile.py +1 -1
  12. cognite_toolkit/_cdf_tk/commands/_purge.py +20 -21
  13. cognite_toolkit/_cdf_tk/commands/_upload.py +4 -6
  14. cognite_toolkit/_cdf_tk/commands/auth.py +12 -15
  15. cognite_toolkit/_cdf_tk/commands/clean.py +2 -1
  16. cognite_toolkit/_cdf_tk/commands/dump_resource.py +30 -19
  17. cognite_toolkit/_cdf_tk/commands/init.py +3 -3
  18. cognite_toolkit/_cdf_tk/commands/modules.py +17 -10
  19. cognite_toolkit/_cdf_tk/commands/pull.py +2 -2
  20. cognite_toolkit/_cdf_tk/commands/repo.py +1 -1
  21. cognite_toolkit/_cdf_tk/commands/resources.py +8 -5
  22. cognite_toolkit/_cdf_tk/commands/run.py +8 -7
  23. cognite_toolkit/_cdf_tk/protocols.py +3 -1
  24. cognite_toolkit/_cdf_tk/storageio/_applications.py +3 -3
  25. cognite_toolkit/_cdf_tk/storageio/_base.py +16 -11
  26. cognite_toolkit/_cdf_tk/storageio/_datapoints.py +37 -25
  27. cognite_toolkit/_cdf_tk/storageio/_file_content.py +39 -35
  28. cognite_toolkit/_cdf_tk/storageio/_raw.py +6 -5
  29. cognite_toolkit/_cdf_tk/utils/auth.py +7 -7
  30. cognite_toolkit/_cdf_tk/utils/interactive_select.py +49 -49
  31. cognite_toolkit/_repo_files/GitHub/.github/workflows/deploy.yaml +1 -1
  32. cognite_toolkit/_repo_files/GitHub/.github/workflows/dry-run.yaml +1 -1
  33. cognite_toolkit/_resources/cdf.toml +1 -1
  34. cognite_toolkit/_version.py +1 -1
  35. {cognite_toolkit-0.7.54.dist-info → cognite_toolkit-0.7.56.dist-info}/METADATA +1 -1
  36. {cognite_toolkit-0.7.54.dist-info → cognite_toolkit-0.7.56.dist-info}/RECORD +38 -39
  37. cognite_toolkit/_cdf_tk/client/http_client/_data_classes.py +0 -428
  38. {cognite_toolkit-0.7.54.dist-info → cognite_toolkit-0.7.56.dist-info}/WHEEL +0 -0
  39. {cognite_toolkit-0.7.54.dist-info → cognite_toolkit-0.7.56.dist-info}/entry_points.txt +0 -0
@@ -1,428 +0,0 @@
1
- from abc import ABC, abstractmethod
2
- from collections import UserList
3
- from collections.abc import Hashable, Sequence
4
- from dataclasses import dataclass, field
5
- from typing import Generic, Literal, Protocol, TypeAlias, TypeVar
6
-
7
- import httpx
8
- from cognite.client.utils import _json
9
-
10
- from cognite_toolkit._cdf_tk.client.http_client._exception import ToolkitAPIError
11
- from cognite_toolkit._cdf_tk.client.http_client._tracker import ItemsRequestTracker
12
- from cognite_toolkit._cdf_tk.utils.useful_types import JsonVal, PrimitiveType
13
-
14
- StatusCode: TypeAlias = int
15
-
16
-
17
- @dataclass
18
- class HTTPMessage(ABC):
19
- """Base class for HTTP messages (requests and responses)"""
20
-
21
- def dump(self) -> dict[str, JsonVal]:
22
- """Dumps the message to a JSON serializable dictionary.
23
-
24
- Returns:
25
- dict[str, JsonVal]: The message as a dictionary.
26
- """
27
- # We avoid using the asdict function as we know we have a shallow structure,
28
- # and this roughly ~10x faster.
29
- output = self.__dict__.copy()
30
- output["type"] = type(self).__name__
31
- return output
32
-
33
-
34
- @dataclass
35
- class ErrorDetails:
36
- """This is the expected structure of error details in the CDF API"""
37
-
38
- code: int
39
- message: str
40
- missing: list[JsonVal] | None = None
41
- duplicated: list[JsonVal] | None = None
42
- is_auto_retryable: bool | None = None
43
-
44
- @classmethod
45
- def from_response(cls, response: httpx.Response) -> "ErrorDetails":
46
- try:
47
- error_data = response.json()["error"]
48
- if not isinstance(error_data, dict):
49
- return cls(code=response.status_code, message=str(error_data))
50
- return cls(
51
- code=error_data["code"],
52
- message=error_data["message"],
53
- missing=error_data.get("missing"),
54
- duplicated=error_data.get("duplicated"),
55
- is_auto_retryable=error_data.get("isAutoRetryable"),
56
- )
57
- except (ValueError, KeyError):
58
- # Fallback if response is not JSON or does not have expected structure
59
- return cls(code=response.status_code, message=response.text)
60
-
61
- def dump(self) -> dict[str, JsonVal]:
62
- output: dict[str, JsonVal] = {
63
- "code": self.code,
64
- "message": self.message,
65
- }
66
- if self.missing is not None:
67
- output["missing"] = self.missing
68
- if self.duplicated is not None:
69
- output["duplicated"] = self.duplicated
70
- if self.is_auto_retryable is not None:
71
- output["isAutoRetryable"] = self.is_auto_retryable
72
- return output
73
-
74
-
75
- @dataclass
76
- class FailedRequestMessage(HTTPMessage):
77
- error: str
78
-
79
- def __str__(self) -> str:
80
- return self.error
81
-
82
-
83
- @dataclass
84
- class ResponseMessage(HTTPMessage):
85
- status_code: StatusCode
86
-
87
-
88
- @dataclass
89
- class RequestMessage(HTTPMessage):
90
- """Base class for HTTP request messages"""
91
-
92
- endpoint_url: str
93
- method: Literal["GET", "POST", "PATCH", "DELETE", "PUT"]
94
- connect_attempt: int = 0
95
- read_attempt: int = 0
96
- status_attempt: int = 0
97
- api_version: str | None = None
98
- content_type: str = "application/json"
99
- accept: str = "application/json"
100
- content_length: int | None = None
101
-
102
- @property
103
- def total_attempts(self) -> int:
104
- return self.connect_attempt + self.read_attempt + self.status_attempt
105
-
106
- @abstractmethod
107
- def create_success_response(self, response: httpx.Response) -> Sequence[HTTPMessage]:
108
- raise NotImplementedError()
109
-
110
- @abstractmethod
111
- def create_failure_response(self, response: httpx.Response) -> Sequence[HTTPMessage]:
112
- raise NotImplementedError()
113
-
114
- @abstractmethod
115
- def create_failed_request(self, error_message: str) -> Sequence[HTTPMessage]:
116
- raise NotImplementedError()
117
-
118
-
119
- @dataclass
120
- class SuccessResponse(ResponseMessage):
121
- body: str
122
- content: bytes
123
-
124
- def dump(self) -> dict[str, JsonVal]:
125
- output = super().dump()
126
- # We cannot serialize bytes, so we indicate its presence instead
127
- output["content"] = "<bytes>" if self.content else None
128
- return output
129
-
130
-
131
- @dataclass
132
- class FailedResponse(ResponseMessage):
133
- body: str
134
- error: ErrorDetails
135
-
136
- def dump(self) -> dict[str, JsonVal]:
137
- output = super().dump()
138
- output["error"] = self.error.dump()
139
- return output
140
-
141
- def __str__(self) -> str:
142
- return f"{self.error.code} | {self.error.message}"
143
-
144
-
145
- @dataclass
146
- class SimpleRequest(RequestMessage):
147
- """Base class for requests with a simple success/fail response structure"""
148
-
149
- @classmethod
150
- def create_success_response(cls, response: httpx.Response) -> Sequence[ResponseMessage]:
151
- return [SuccessResponse(status_code=response.status_code, body=response.text, content=response.content)]
152
-
153
- @classmethod
154
- def create_failure_response(cls, response: httpx.Response) -> Sequence[HTTPMessage]:
155
- return [
156
- FailedResponse(
157
- status_code=response.status_code, error=ErrorDetails.from_response(response), body=response.text
158
- )
159
- ]
160
-
161
- @classmethod
162
- def create_failed_request(cls, error_message: str) -> Sequence[HTTPMessage]:
163
- return [FailedRequestMessage(error=error_message)]
164
-
165
-
166
- @dataclass
167
- class BodyRequest(RequestMessage, ABC):
168
- """Base class for HTTP request messages with a body"""
169
-
170
- @abstractmethod
171
- def data(self) -> str:
172
- raise NotImplementedError()
173
-
174
-
175
- @dataclass
176
- class ParamRequest(SimpleRequest):
177
- """Base class for HTTP request messages with query parameters"""
178
-
179
- parameters: dict[str, PrimitiveType] | None = None
180
-
181
-
182
- @dataclass
183
- class SimpleBodyRequest(SimpleRequest, BodyRequest):
184
- body_content: dict[str, JsonVal] = field(default_factory=dict)
185
-
186
- def data(self) -> str:
187
- return _dump_body(self.body_content)
188
-
189
-
190
- @dataclass
191
- class DataBodyRequest(SimpleRequest):
192
- data_content: bytes = b""
193
-
194
- def data(self) -> bytes:
195
- return self.data_content
196
-
197
- def dump(self) -> dict[str, JsonVal]:
198
- output = super().dump()
199
- # We cannot serialize bytes, so we indicate its presence instead
200
- output["data_content"] = "<bytes>"
201
- return output
202
-
203
-
204
- T_COVARIANT_ID = TypeVar("T_COVARIANT_ID", covariant=True)
205
-
206
-
207
- @dataclass
208
- class ItemMessage(Generic[T_COVARIANT_ID], ABC):
209
- """Base class for message related to a specific item identified by an ID"""
210
-
211
- ids: list[T_COVARIANT_ID] = field(default_factory=list)
212
-
213
-
214
- @dataclass
215
- class SuccessResponseItems(ItemMessage[T_COVARIANT_ID], SuccessResponse): ...
216
-
217
-
218
- @dataclass
219
- class FailedResponseItems(ItemMessage[T_COVARIANT_ID], FailedResponse): ...
220
-
221
-
222
- @dataclass
223
- class FailedRequestItems(ItemMessage[T_COVARIANT_ID], FailedRequestMessage): ...
224
-
225
-
226
- class RequestItem(Generic[T_COVARIANT_ID], Protocol):
227
- def dump(self) -> JsonVal: ...
228
-
229
- def as_id(self) -> T_COVARIANT_ID: ...
230
-
231
-
232
- @dataclass
233
- class ItemsRequest(Generic[T_COVARIANT_ID], BodyRequest):
234
- """Requests message for endpoints that accept multiple items in a single request.
235
-
236
- This class provides functionality to split large requests into smaller ones, handle responses for each item,
237
- and manage errors effectively.
238
-
239
- Attributes:
240
- items (list[T_RequestItem]): The list of items to be sent in the request body.
241
- extra_body_fields (dict[str, JsonVal]): Additional fields to include in the request body
242
- max_failures_before_abort (int): The maximum number of failed split requests before aborting further splits.
243
-
244
- """
245
-
246
- items: list[RequestItem[T_COVARIANT_ID]] = field(default_factory=list)
247
- extra_body_fields: dict[str, JsonVal] = field(default_factory=dict)
248
- max_failures_before_abort: int = 50
249
- tracker: ItemsRequestTracker | None = field(default=None, init=False)
250
-
251
- def dump(self) -> dict[str, JsonVal]:
252
- """Dumps the message to a JSON serializable dictionary.
253
-
254
- This override removes the 'as_id' attribute as it is not serializable.
255
-
256
- Returns:
257
- dict[str, JsonVal]: The message as a dictionary.
258
- """
259
- output = super().dump()
260
- output["items"] = self.dump_items()
261
- if self.tracker is not None:
262
- # We cannot serialize the tracker
263
- del output["tracker"]
264
- return output
265
-
266
- def dump_items(self) -> list[JsonVal]:
267
- """Dumps the items to a list of JSON serializable dictionaries.
268
-
269
- Returns:
270
- list[JsonVal]: The items as a list of dictionaries.
271
- """
272
- return [item.dump() for item in self.items]
273
-
274
- def body(self) -> dict[str, JsonVal]:
275
- if self.extra_body_fields:
276
- return {"items": self.dump_items(), **self.extra_body_fields}
277
- return {"items": self.dump_items()}
278
-
279
- def data(self) -> str:
280
- return _dump_body(self.body())
281
-
282
- def split(self, status_attempts: int) -> "list[ItemsRequest]":
283
- """Splits the request into two smaller requests.
284
-
285
- This is useful for retrying requests that fail due to size limits or timeouts.
286
-
287
- Args:
288
- status_attempts: The number of status attempts to set for the new requests. This is used when the
289
- request failed with a 5xx status code and we want to track the number of attempts. For 4xx errors,
290
- there is at least one item causing the error, so we do not increment the status attempts, but
291
- instead essentially do a binary search to find the problematic item(s).
292
-
293
- Returns:
294
- A list containing two new ItemsRequest instances, each with half of the original items.
295
-
296
- """
297
- mid = len(self.items) // 2
298
- if mid == 0:
299
- return [self]
300
- tracker = self.tracker or ItemsRequestTracker(self.max_failures_before_abort)
301
- tracker.register_failure()
302
- first_half = ItemsRequest[T_COVARIANT_ID](
303
- endpoint_url=self.endpoint_url,
304
- method=self.method,
305
- items=self.items[:mid],
306
- extra_body_fields=self.extra_body_fields,
307
- connect_attempt=self.connect_attempt,
308
- read_attempt=self.read_attempt,
309
- status_attempt=status_attempts,
310
- api_version=self.api_version,
311
- content_type=self.content_type,
312
- accept=self.accept,
313
- content_length=self.content_length,
314
- max_failures_before_abort=self.max_failures_before_abort,
315
- )
316
- first_half.tracker = tracker
317
- second_half = ItemsRequest[T_COVARIANT_ID](
318
- endpoint_url=self.endpoint_url,
319
- method=self.method,
320
- items=self.items[mid:],
321
- extra_body_fields=self.extra_body_fields,
322
- connect_attempt=self.connect_attempt,
323
- read_attempt=self.read_attempt,
324
- status_attempt=status_attempts,
325
- api_version=self.api_version,
326
- content_type=self.content_type,
327
- accept=self.accept,
328
- content_length=self.content_length,
329
- max_failures_before_abort=self.max_failures_before_abort,
330
- )
331
- second_half.tracker = tracker
332
- return [first_half, second_half]
333
-
334
- def create_success_response(self, response: httpx.Response) -> Sequence[HTTPMessage]:
335
- ids = [item.as_id() for item in self.items]
336
- return [
337
- SuccessResponseItems(
338
- status_code=response.status_code, ids=ids, body=response.text, content=response.content
339
- )
340
- ]
341
-
342
- def create_failure_response(self, response: httpx.Response) -> Sequence[HTTPMessage]:
343
- error = ErrorDetails.from_response(response)
344
- ids = [item.as_id() for item in self.items]
345
- return [FailedResponseItems(status_code=response.status_code, ids=ids, error=error, body=response.text)]
346
-
347
- def create_failed_request(self, error_message: str) -> Sequence[HTTPMessage]:
348
- ids = [item.as_id() for item in self.items]
349
- return [FailedRequestItems(ids=ids, error=error_message)]
350
-
351
-
352
- class ResponseList(UserList[ResponseMessage | FailedRequestMessage]):
353
- def __init__(self, collection: Sequence[ResponseMessage | FailedRequestMessage] | None = None) -> None:
354
- super().__init__(collection or [])
355
-
356
- def raise_for_status(self) -> None:
357
- """Raises an exception if any response in the list indicates a failure."""
358
- failed_responses = [resp for resp in self.data if isinstance(resp, FailedResponse)]
359
- failed_requests = [resp for resp in self.data if isinstance(resp, FailedRequestMessage)]
360
- if not failed_responses and not failed_requests:
361
- return
362
- error_messages = "; ".join(f"Status {err.status_code}: {err.error}" for err in failed_responses)
363
- if failed_requests:
364
- if error_messages:
365
- error_messages += "; "
366
- error_messages += "; ".join(f"Request error: {err.error}" for err in failed_requests)
367
- raise ToolkitAPIError(f"One or more requests failed: {error_messages}")
368
-
369
- @property
370
- def has_failed(self) -> bool:
371
- """Indicates whether any response in the list indicates a failure.
372
-
373
- Returns:
374
- bool: True if there are any failed responses or requests, False otherwise.
375
- """
376
- for resp in self.data:
377
- if isinstance(resp, FailedResponse | FailedRequestMessage):
378
- return True
379
- return False
380
-
381
- def get_first_body(self) -> dict[str, JsonVal]:
382
- """Returns the body of the first successful response in the list.
383
-
384
- Raises:
385
- ValueError: If there are no successful responses with a body.
386
-
387
- Returns:
388
- dict[str, JsonVal]: The body of the first successful response.
389
- """
390
- for resp in self.data:
391
- if isinstance(resp, SuccessResponse) and resp.body is not None:
392
- return _json.loads(resp.body)
393
- raise ValueError("No successful responses with a body found.")
394
-
395
- def as_item_responses(self, item_id: Hashable) -> list[ResponseMessage | FailedRequestMessage]:
396
- # Convert the responses to per-item responses
397
- results: list[ResponseMessage | FailedRequestMessage] = []
398
- for message in self.data:
399
- if isinstance(message, SuccessResponse):
400
- results.append(
401
- SuccessResponseItems(
402
- status_code=message.status_code, content=message.content, ids=[item_id], body=message.body
403
- )
404
- )
405
- elif isinstance(message, FailedResponse):
406
- results.append(
407
- FailedResponseItems(
408
- status_code=message.status_code, ids=[item_id], body=message.body, error=message.error
409
- )
410
- )
411
- elif isinstance(message, FailedRequestMessage):
412
- results.append(FailedRequestItems(ids=[item_id], error=message.error))
413
- else:
414
- results.append(message)
415
- return results
416
-
417
-
418
- def _dump_body(body: dict[str, JsonVal]) -> str:
419
- try:
420
- return _json.dumps(body, allow_nan=False)
421
- except ValueError as e:
422
- # A lot of work to give a more human friendly error message when nans and infs are present:
423
- msg = "Out of range float values are not JSON compliant"
424
- if msg in str(e): # exc. might e.g. contain an extra ": nan", depending on build (_json.make_encoder)
425
- raise ValueError(f"{msg}. Make sure your data does not contain NaN(s) or +/- Inf!").with_traceback(
426
- e.__traceback__
427
- ) from None
428
- raise