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,278 @@
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
+
22
+ """Webapp client module."""
23
+ from __future__ import annotations
24
+
25
+ from typing import Any, Dict, Iterator, List, Literal, Optional, overload
26
+
27
+ from peak.base_client import BaseClient
28
+ from peak.constants import ContentType, HttpMethods
29
+ from peak.session import Session
30
+
31
+
32
+ class Webapp(BaseClient):
33
+ """Client class for interacting with webapps resource."""
34
+
35
+ BASE_ENDPOINT = "webapps/api/v1"
36
+
37
+ @overload
38
+ def list_webapps(
39
+ self,
40
+ page_size: Optional[int] = None,
41
+ page_number: Optional[int] = None,
42
+ status: Optional[List[str]] = None,
43
+ name: Optional[str] = None,
44
+ *,
45
+ return_iterator: Literal[False],
46
+ ) -> Dict[str, Any]:
47
+ ...
48
+
49
+ @overload
50
+ def list_webapps(
51
+ self,
52
+ page_size: Optional[int] = None,
53
+ page_number: Optional[int] = None,
54
+ status: Optional[List[str]] = None,
55
+ name: Optional[str] = None,
56
+ *,
57
+ return_iterator: Literal[True] = True,
58
+ ) -> Iterator[Dict[str, Any]]:
59
+ ...
60
+
61
+ def list_webapps(
62
+ self,
63
+ page_size: Optional[int] = None,
64
+ page_number: Optional[int] = None,
65
+ status: Optional[List[str]] = None,
66
+ name: Optional[str] = None,
67
+ *,
68
+ return_iterator: bool = True,
69
+ ) -> Iterator[Dict[str, Any]] | Dict[str, Any]:
70
+ """Retrieve a list of webapps.
71
+
72
+ REFERENCE:
73
+ 🔗 `API Documentation <https://service.peak.ai/webapps/api-docs/index.htm#/Webapps/list-webapp>`__
74
+
75
+ Args:
76
+ page_size (int | None): The number of webapps per page.
77
+ page_number (int | None): The page number to retrieve. Only used when `return_iterator` is False.
78
+ status (List[str] | None): A list of webapp status to filter the list by.
79
+ Allowed values are - CREATING, DEPLOYING, AVAILABLE, DELETING, CREATE_FAILED, DELETE_FAILED
80
+ name (str | None): Name of the webapp to search for.
81
+ return_iterator (bool): Whether to return an iterator object or list of webapps for a specified page number, defaults to True.
82
+
83
+ Returns:
84
+ Iterator[Dict[str, Any] | Dict[str, Any]: an iterator object which returns an element per iteration, until there are no more elements to return.
85
+ If `return_iterator` is set to False, a dictionary containing the list and pagination details is returned instead.
86
+ Set `return_iterator` to True if you want automatic client-side pagination, or False if you want server-side pagination.
87
+
88
+ Raises:
89
+ BadRequestException: The given request parameters are invalid.
90
+ UnauthorizedException: The credentials are invalid.
91
+ ForbiddenException: The user does not have permission to perform the operation.
92
+ InternalServerErrorException: The server failed to process the request.
93
+ """
94
+ method, endpoint = HttpMethods.GET, f"{self.BASE_ENDPOINT}/webapps/"
95
+ params = {"pageSize": page_size, "status": status, "searchTerm": name}
96
+
97
+ if return_iterator:
98
+ return self.session.create_generator_request(
99
+ endpoint,
100
+ method,
101
+ content_type=ContentType.APPLICATION_JSON,
102
+ response_key="webapps",
103
+ params=params,
104
+ )
105
+
106
+ return self.session.create_request( # type: ignore[no-any-return]
107
+ endpoint,
108
+ method,
109
+ content_type=ContentType.APPLICATION_JSON,
110
+ params={**params, "pageNumber": page_number},
111
+ )
112
+
113
+ def create_webapp(self, body: Dict[str, Any]) -> Dict[str, str]:
114
+ """Create a new webapp. Only geneic (EKS-based) webapps are supported at the moment.
115
+
116
+ REFERENCE:
117
+ 🔗 `API Documentation <https://service.peak.ai/webapps/api-docs/index.htm#/Webapps/create-webapp>`__
118
+
119
+ Args:
120
+ body (Dict[str, Any]): A dictionary containing the webapp config.
121
+
122
+ Returns:
123
+ Dict[str, str]: Id of the new webapp
124
+
125
+ Raises:
126
+ BadRequestException: The given request parameters are invalid.
127
+ UnauthorizedException: The credentials are invalid.
128
+ ForbiddenException: The user does not have permission to perform the operation.
129
+ InternalServerErrorException: The server failed to process the request.
130
+ """
131
+ method, endpoint = HttpMethods.POST, f"{self.BASE_ENDPOINT}/webapps/"
132
+
133
+ return self.session.create_request( # type: ignore[no-any-return]
134
+ endpoint,
135
+ method,
136
+ content_type=ContentType.APPLICATION_JSON,
137
+ body=body,
138
+ )
139
+
140
+ def update_webapp(
141
+ self,
142
+ webapp_id: str,
143
+ body: Dict[str, Any],
144
+ ) -> Dict[str, str]:
145
+ """Updates the existing webapp. Only geneic (EKS-based) webapps are supported at the moment.
146
+
147
+ REFERENCE:
148
+ 🔗 `API Documentation <https://service.peak.ai/webapps/api-docs/index.htm#/Webapps/update-webapp>`__
149
+
150
+ Args:
151
+ webapp_id (str): The ID of the webapp to update.
152
+ body (Dict[str, Any]): A dictionary containing the webapp config.
153
+
154
+ Returns:
155
+ Dict[str, str]: Id of the webapp.
156
+
157
+ Raises:
158
+ BadRequestException: The given request parameters are invalid.
159
+ UnauthorizedException: The credentials are invalid.
160
+ ForbiddenException: The user does not have permission to perform the operation.
161
+ NotFoundException: The given webapp does not exist.
162
+ UnprocessableEntityException: The server was unable to process the request.
163
+ InternalServerErrorException: The server failed to process the request.
164
+ """
165
+ method, endpoint = HttpMethods.PATCH, f"{self.BASE_ENDPOINT}/webapps/{webapp_id}"
166
+ return self.session.create_request( # type: ignore[no-any-return]
167
+ endpoint,
168
+ method,
169
+ content_type=ContentType.APPLICATION_JSON,
170
+ body=body,
171
+ )
172
+
173
+ def create_or_update_webapp(self, body: Dict[str, Any]) -> Dict[str, str]:
174
+ """Create a new webapp or updates an existing webapp based on webapp name.
175
+
176
+ REFERENCE:
177
+ 🔗 `API Documentation <https://service.peak.ai/webapps/api-docs/index.htm#/Webapps/create-webapp>`__
178
+
179
+ Args:
180
+ body (Dict[str, Any]): A dictionary containing the webapp config.
181
+
182
+ Returns:
183
+ Dict[str, str]: Id of the new or updated webapp.
184
+
185
+ Raises:
186
+ BadRequestException: The given request parameters are invalid.
187
+ UnauthorizedException: The credentials are invalid.
188
+ ForbiddenException: The user does not have permission to perform the operation.
189
+ InternalServerErrorException: The server failed to process the request.
190
+ """
191
+ webapp_name = body["name"] if "name" in body else ""
192
+ response = (
193
+ {} if not len(webapp_name) else self.list_webapps(page_size=100, return_iterator=False, name=webapp_name)
194
+ )
195
+ filtered_webapps = list(
196
+ filter(lambda webapp: webapp.get("name", "") == webapp_name, response.get("webapps", [])),
197
+ )
198
+
199
+ if len(filtered_webapps) > 0:
200
+ webapp_id = filtered_webapps[0]["id"]
201
+ return self.update_webapp(webapp_id=webapp_id, body=body)
202
+
203
+ return self.create_webapp(body=body)
204
+
205
+ def delete_webapp(
206
+ self,
207
+ webapp_id: str,
208
+ ) -> Dict[str, str]:
209
+ """Delete a webapp.
210
+
211
+ REFERENCE:
212
+ 🔗 `API Documentation <https://service.peak.ai/webapps/api-docs/index.htm#/Webapps/delete-webapp>`__
213
+
214
+ Args:
215
+ webapp_id (str): The ID of the webapp to delete.
216
+
217
+ Returns:
218
+ Dict[str, str]: Dictonary containing Id of the deleted webapp
219
+
220
+ Raises:
221
+ UnauthorizedException: The credentials are invalid.
222
+ ForbiddenException: The user does not have permission to perform the operation.
223
+ NotFoundException: The given webapp does not exist.
224
+ ConflictException: If the webapp is in a conflicting state while deleting.
225
+ InternalServerErrorException: The server failed to process the request.
226
+ """
227
+ method, endpoint = HttpMethods.DELETE, f"{self.BASE_ENDPOINT}/webapps/{webapp_id}"
228
+
229
+ return self.session.create_request( # type: ignore[no-any-return]
230
+ endpoint,
231
+ method,
232
+ content_type=ContentType.APPLICATION_JSON,
233
+ )
234
+
235
+ def describe_webapp(
236
+ self,
237
+ webapp_id: str,
238
+ ) -> Dict[str, Any]:
239
+ """Retrieve details of a specific webapp.
240
+
241
+ REFERENCE:
242
+ 🔗 `API Documentation <https://service.peak.ai/webapps/api-docs/index.htm#/Webapps/get-webapp>`__
243
+
244
+ Args:
245
+ webapp_id (str): The ID of the webapp to fetch.
246
+
247
+ Returns:
248
+ Dict[str, Any]: Dictonary containing details of the webapp
249
+
250
+ Raises:
251
+ UnauthorizedException: The credentials are invalid.
252
+ ForbiddenException: The user does not have permission to perform the operation.
253
+ NotFoundException: The given webapp does not exist.
254
+ UnprocessableEntityException: The server was unable to process the request.
255
+ InternalServerErrorException: The server failed to process the request.
256
+ """
257
+ method, endpoint = HttpMethods.GET, f"{self.BASE_ENDPOINT}/webapps/{webapp_id}"
258
+
259
+ return self.session.create_request( # type: ignore[no-any-return]
260
+ endpoint,
261
+ method,
262
+ content_type=ContentType.APPLICATION_JSON,
263
+ )
264
+
265
+
266
+ def get_client(session: Optional[Session] = None) -> Webapp:
267
+ """Returns a Webapp client, If no session is provided, a default session is used.
268
+
269
+ Args:
270
+ session (Optional[Session]): A Session Object. Default is None.
271
+
272
+ Returns:
273
+ Webapp: the webapp client object
274
+ """
275
+ return Webapp(session)
276
+
277
+
278
+ __all__ = ["get_client"]