peak-sdk 1.0.0__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 (51) hide show
  1. peak/__init__.py +36 -0
  2. peak/_version.py +21 -0
  3. peak/auth.py +22 -0
  4. peak/base_client.py +52 -0
  5. peak/cli/__init_.py +20 -0
  6. peak/cli/args.py +84 -0
  7. peak/cli/cli.py +56 -0
  8. peak/cli/helpers.py +187 -0
  9. peak/cli/press/__init__.py +21 -0
  10. peak/cli/press/apps/__init__.py +40 -0
  11. peak/cli/press/apps/deployments.py +238 -0
  12. peak/cli/press/apps/specs.py +387 -0
  13. peak/cli/press/blocks/__init__.py +40 -0
  14. peak/cli/press/blocks/deployments.py +240 -0
  15. peak/cli/press/blocks/specs.py +492 -0
  16. peak/cli/press/deployments.py +78 -0
  17. peak/cli/press/specs.py +131 -0
  18. peak/cli/resources/__init__.py +21 -0
  19. peak/cli/resources/artifacts.py +310 -0
  20. peak/cli/resources/images.py +886 -0
  21. peak/cli/resources/webapps.py +356 -0
  22. peak/cli/resources/workflows.py +703 -0
  23. peak/cli/ruff.toml +11 -0
  24. peak/cli/version.py +49 -0
  25. peak/compression.py +162 -0
  26. peak/config.py +24 -0
  27. peak/constants.py +105 -0
  28. peak/exceptions.py +217 -0
  29. peak/handler.py +358 -0
  30. peak/helpers.py +184 -0
  31. peak/logger.py +48 -0
  32. peak/press/__init__.py +28 -0
  33. peak/press/apps.py +669 -0
  34. peak/press/blocks.py +707 -0
  35. peak/press/deployments.py +145 -0
  36. peak/press/specs.py +260 -0
  37. peak/py.typed +0 -0
  38. peak/resources/__init__.py +28 -0
  39. peak/resources/artifacts.py +343 -0
  40. peak/resources/images.py +675 -0
  41. peak/resources/webapps.py +278 -0
  42. peak/resources/workflows.py +625 -0
  43. peak/session.py +259 -0
  44. peak/telemetry.py +201 -0
  45. peak/template.py +231 -0
  46. peak/validators.py +48 -0
  47. peak_sdk-1.0.0.dist-info/LICENSE +201 -0
  48. peak_sdk-1.0.0.dist-info/METADATA +199 -0
  49. peak_sdk-1.0.0.dist-info/RECORD +51 -0
  50. peak_sdk-1.0.0.dist-info/WHEEL +4 -0
  51. peak_sdk-1.0.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,343 @@
1
+ #
2
+ # # Copyright © 2023 Peak AI Limited. or its affiliates. All Rights Reserved.
3
+ # #
4
+ # # Licensed under the Apache License, Version 2.0 (the "License"). You
5
+ # # may not use this file except in compliance with the License. A copy of
6
+ # # the License is located at:
7
+ # #
8
+ # # https://github.com/PeakBI/peak-sdk/blob/main/LICENSE
9
+ # #
10
+ # # or in the "license" file accompanying this file. This file is
11
+ # # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12
+ # # ANY KIND, either express or implied. See the License for the specific
13
+ # # language governing permissions and limitations under the License.
14
+ # #
15
+ # # This file is part of the peak-sdk.
16
+ # # see (https://github.com/PeakBI/peak-sdk)
17
+ # #
18
+ # # You should have received a copy of the APACHE LICENSE, VERSION 2.0
19
+ # # along with this program. If not, see <https://apache.org/licenses/LICENSE-2.0>
20
+ #
21
+ """Artifact client module."""
22
+ from __future__ import annotations
23
+
24
+ from typing import Any, Dict, Iterator, List, Literal, Optional, overload
25
+
26
+ from peak.base_client import BaseClient
27
+ from peak.constants import ArtifactInfo, ContentType, HttpMethods
28
+ from peak.session import Session
29
+
30
+
31
+ class Artifact(BaseClient):
32
+ """Artifact client class."""
33
+
34
+ BASE_ENDPOINT = "artifacts/api/v1"
35
+
36
+ @overload
37
+ def list_artifacts(
38
+ self,
39
+ page_size: Optional[int] = None,
40
+ page_number: Optional[int] = None,
41
+ *,
42
+ return_iterator: Literal[False],
43
+ ) -> Dict[str, Any]:
44
+ ...
45
+
46
+ @overload
47
+ def list_artifacts(
48
+ self,
49
+ page_size: Optional[int] = None,
50
+ page_number: Optional[int] = None,
51
+ *,
52
+ return_iterator: Literal[True] = True,
53
+ ) -> Iterator[Dict[str, Any]]:
54
+ ...
55
+
56
+ def list_artifacts(
57
+ self,
58
+ page_size: Optional[int] = None,
59
+ page_number: Optional[int] = None,
60
+ *,
61
+ return_iterator: bool = True,
62
+ ) -> Iterator[Dict[str, Any]] | Dict[str, Any]:
63
+ """Retrieve a list of artifacts.
64
+
65
+ REFERENCE:
66
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/get_api_v1_artifacts>`__
67
+
68
+ Args:
69
+ page_size (int | None): The number of artifacts per page.
70
+ page_number (int | None): The page number to retrieve. Only used when return_iterator is False.
71
+ return_iterator (bool): Whether to return an iterator object or a list of artifacts, defaults to True.
72
+
73
+ Returns:
74
+ Iterator[Dict[str, Any]] | Dict[str, Any]: an iterator object which returns an element per iteration, until there are no more elements to return.
75
+ If `return_iterator` is set to False, a dictionary containing the list and pagination details is returned instead.
76
+
77
+ Set `return_iterator` to True if you want automatic client-side pagination, or False if you want server-side pagination.
78
+
79
+ Raises:
80
+ BadRequestException: The given request parameters are invalid.
81
+ UnauthorizedException: The credentials are invalid.
82
+ ForbiddenException: The user does not have permission to perform the operation.
83
+ InternalServerErrorException: The server failed to process the request.
84
+ """
85
+ method, endpoint = HttpMethods.GET, f"{self.BASE_ENDPOINT}/artifacts/"
86
+ params = {"pageSize": page_size}
87
+
88
+ if return_iterator:
89
+ return self.session.create_generator_request(
90
+ endpoint,
91
+ method,
92
+ content_type=ContentType.APPLICATION_JSON,
93
+ params=params,
94
+ response_key="artifacts",
95
+ )
96
+
97
+ return self.session.create_request( # type: ignore[no-any-return]
98
+ endpoint,
99
+ method,
100
+ content_type=ContentType.APPLICATION_JSON,
101
+ params={**params, "pageNumber": page_number},
102
+ )
103
+
104
+ def create_artifact(
105
+ self,
106
+ name: str,
107
+ artifact: ArtifactInfo,
108
+ description: Optional[str] = None,
109
+ ) -> Dict[str, Any]:
110
+ """Create a new artifact.
111
+
112
+ REFERENCE:
113
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/post_api_v1_artifacts>`__
114
+
115
+ Args:
116
+ name (str): Name of the artifact.
117
+ artifact (ArtifactInfo): Mapping of artifact attributes that specifies how the artifact will be generated,
118
+ it accepts two keys `path`, which is required and `ignore_files` which is optional, and defaults to `.dockerignore`, it is strongly advised that users use `ignore_files` when generating artifacts to avoid copying any extra files in artifact.
119
+ description (str | None): A brief description of the artifact.
120
+
121
+ Returns:
122
+ Dict[str, Any]: `Id` and `Version` of the created artifact.
123
+
124
+ Raises:
125
+ BadRequestException: The given request parameters are invalid.
126
+ UnauthorizedException: The credentials are invalid.
127
+ ForbiddenException: The user does not have permission to perform the operation.
128
+ PayloadTooLargeException: The artifact exceeds maximum size.
129
+ InternalServerErrorException: The server failed to process the request.
130
+ """
131
+ method, endpoint = HttpMethods.POST, f"{self.BASE_ENDPOINT}/artifacts/"
132
+ body: Dict[str, Any] = {"name": name, "description": description}
133
+
134
+ return self.session.create_request( # type: ignore[no-any-return]
135
+ endpoint,
136
+ method,
137
+ content_type=ContentType.MULTIPART_FORM_DATA,
138
+ body=body,
139
+ path=artifact["path"],
140
+ ignore_files=artifact.get("ignore_files"),
141
+ )
142
+
143
+ def describe_artifact(
144
+ self,
145
+ artifact_id: str,
146
+ page_number: Optional[int] = None,
147
+ page_size: Optional[int] = None,
148
+ ) -> Dict[str, Any]:
149
+ """Retrieve details of a specific artifact with list of its versions.
150
+
151
+ REFERENCE:
152
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/get_api_v1_artifacts__artifactId_>`__
153
+
154
+ Args:
155
+ artifact_id (str): The ID of the artifact to retrieve.
156
+ page_number (int | None): The page number to retrieve.
157
+ page_size (int | None): The number of versions per page.
158
+
159
+ Returns:
160
+ Dict[str, Any]: a dictionary containing the details of the artifact.
161
+
162
+ Raises:
163
+ BadRequestException: The given request parameters are invalid.
164
+ UnauthorizedException: The credentials are invalid.
165
+ ForbiddenException: The user does not have permission to perform the operation.
166
+ NotFoundException: The given image does not exist.
167
+ InternalServerErrorException: The server failed to process the request.
168
+ """
169
+ method, endpoint = HttpMethods.GET, f"{self.BASE_ENDPOINT}/artifacts/{artifact_id}"
170
+ params = {"pageNumber": page_number, "pageSize": page_size}
171
+
172
+ return self.session.create_request( # type: ignore[no-any-return]
173
+ endpoint,
174
+ method,
175
+ content_type=ContentType.APPLICATION_JSON,
176
+ params=params,
177
+ )
178
+
179
+ def delete_artifact(self, artifact_id: str) -> Dict[None, None]:
180
+ """Delete an artifact with all its versions.
181
+
182
+ REFERENCE:
183
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/delete_api_v1_artifacts__artifactId_>`__
184
+
185
+ Args:
186
+ artifact_id (str): The ID of the artifact to delete.
187
+
188
+ Returns:
189
+ dict: Empty dictionary object.
190
+
191
+ Raises:
192
+ BadRequestException: The given request parameters are invalid.
193
+ UnauthorizedException: The credentials are invalid.
194
+ ForbiddenException: The user does not have permission to perform the operation.
195
+ NotFoundException: The given image does not exist.
196
+ InternalServerErrorException: The server failed to process the request.
197
+ """
198
+ method, endpoint = HttpMethods.DELETE, f"{self.BASE_ENDPOINT}/artifacts/{artifact_id}"
199
+
200
+ return self.session.create_request( # type: ignore[no-any-return]
201
+ endpoint,
202
+ method,
203
+ content_type=ContentType.APPLICATION_JSON,
204
+ )
205
+
206
+ def create_artifact_version(
207
+ self,
208
+ artifact_id: str,
209
+ artifact: ArtifactInfo,
210
+ ) -> Dict[str, int]:
211
+ """Create a new version of the artifact.
212
+
213
+ REFERENCE:
214
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/put_api_v1_artifacts__artifactId_>`__
215
+
216
+ Args:
217
+ artifact_id (str): ID of the artifact for which a new version is to be created.
218
+ artifact (ArtifactInfo): Mapping of artifact attributes that specifies how the artifact will be generated,
219
+ it accepts two keys `path`, which is required and `ignore_files` which is optional, and defaults to `.dockerignore`, it is strongly advised that users use `ignore_files` when generating artifacts to avoid copying any extra files in artifact.
220
+
221
+ Returns:
222
+ Dict[str, int]: version number.
223
+
224
+ Raises:
225
+ BadRequestException: The given request parameters are invalid.
226
+ UnauthorizedException: The credentials are invalid.
227
+ ForbiddenException: The user does not have permission to perform the operation.
228
+ NotFoundException: The given image does not exist.
229
+ PayloadTooLargeException: The artifact exceeds maximum size.
230
+ InternalServerErrorException: The server failed to process the request.
231
+ """
232
+ method, endpoint = HttpMethods.PUT, f"{self.BASE_ENDPOINT}/artifacts/{artifact_id}"
233
+
234
+ return self.session.create_request( # type: ignore[no-any-return]
235
+ endpoint,
236
+ method,
237
+ content_type=ContentType.MULTIPART_FORM_DATA,
238
+ body={},
239
+ path=artifact["path"],
240
+ ignore_files=artifact.get("ignore_files"),
241
+ )
242
+
243
+ def update_artifact(self, artifact_id: str, body: Dict[str, Any]) -> Dict[None, None]:
244
+ """Update an artifact's metadata.
245
+
246
+ REFERENCE:
247
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/patch_api_v1_artifacts__artifactId_>`__
248
+
249
+ Args:
250
+ artifact_id (str): ID of the artifact to be updated.
251
+ body (Dict[str, Any]): dictionary containing new metadata for artifact.
252
+
253
+ Returns:
254
+ dict: Empty dict object.
255
+
256
+ Raises:
257
+ BadRequestException: The given request parameters are invalid.
258
+ UnauthorizedException: The credentials are invalid.
259
+ ForbiddenException: The user does not have permission to perform the operation.
260
+ NotFoundException: The given image does not exist.
261
+ InternalServerErrorException: The server failed to process the request.
262
+ """
263
+ method, endpoint = HttpMethods.PATCH, f"{self.BASE_ENDPOINT}/artifacts/{artifact_id}"
264
+
265
+ return self.session.create_request( # type: ignore[no-any-return]
266
+ endpoint,
267
+ method,
268
+ content_type=ContentType.APPLICATION_JSON,
269
+ body=body,
270
+ )
271
+
272
+ def delete_artifact_version(self, artifact_id: str, version: int) -> Dict[None, None]:
273
+ """Delete a version of an artifact.
274
+
275
+ REFERENCE:
276
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/delete_api_v1_artifacts__artifactId___version_>`__
277
+
278
+ Args:
279
+ artifact_id (str): ID of the artifact.
280
+ version (int): Artifact version number to delete.
281
+
282
+ Returns:
283
+ dict: Empty dict object.
284
+
285
+ Raises:
286
+ BadRequestException: The given request parameters are invalid.
287
+ UnauthorizedException: The credentials are invalid.
288
+ ForbiddenException: The user does not have permission to perform the operation.
289
+ NotFoundException: The given image does not exist.
290
+ UnprocessableEntityException: The server was unable to process the request.
291
+ InternalServerErrorException: The server failed to process the request.
292
+ """
293
+ method, endpoint = HttpMethods.DELETE, f"{self.BASE_ENDPOINT}/artifacts/{artifact_id}/{version}"
294
+
295
+ return self.session.create_request( # type: ignore[no-any-return]
296
+ endpoint,
297
+ method,
298
+ content_type=ContentType.APPLICATION_JSON,
299
+ )
300
+
301
+ def download_artifact(self, artifact_id: str, download_path: str, version: Optional[int] = None) -> None:
302
+ """Download a version of the artifact.
303
+
304
+ REFERENCE:
305
+ 🔗 `API Documentation <https://service.peak.ai/artifacts/api-docs/index.htm#/artifacts/get_api_v1_artifacts__artifactId__download>`__
306
+
307
+ Args:
308
+ artifact_id (str): ID of the artifact to download.
309
+ download_path (str): Path (including filename) where the downloaded file will be stored.
310
+ version (int | None): Artifact version to download. If no version is given then latest version is downloaded.
311
+
312
+ Raises:
313
+ BadRequestException: The given request parameters are invalid.
314
+ UnauthorizedException: The credentials are invalid.
315
+ ForbiddenException: The user does not have permission to perform the operation.
316
+ NotFoundException: The given image does not exist.
317
+ InternalServerErrorException: The server failed to process the request.
318
+ """
319
+ method, endpoint = HttpMethods.GET, f"{self.BASE_ENDPOINT}/artifacts/{artifact_id}"
320
+
321
+ endpoint = f"{endpoint}/download" if version is None else f"{endpoint}/{version}/download"
322
+
323
+ self.session.create_download_request(
324
+ endpoint,
325
+ method,
326
+ content_type=ContentType.APPLICATION_JSON,
327
+ download_path=download_path,
328
+ )
329
+
330
+
331
+ def get_client(session: Optional[Session] = None) -> Artifact:
332
+ """Returns an Artifact client, If no session is provided, a default session is used.
333
+
334
+ Args:
335
+ session (Optional[Session]): A Session Object. Default is None.
336
+
337
+ Returns:
338
+ Artifact: the artifact client object
339
+ """
340
+ return Artifact(session)
341
+
342
+
343
+ __all__: List[str] = ["get_client"]