scalebox-sdk 0.1.4__py3-none-any.whl → 0.1.25__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 (68) hide show
  1. scalebox/__init__.py +1 -1
  2. scalebox/api/__init__.py +128 -128
  3. scalebox/api/client/__init__.py +8 -8
  4. scalebox/api/client/api/sandboxes/get_sandboxes.py +5 -3
  5. scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_metrics.py +2 -2
  6. scalebox/api/client/api/sandboxes/post_sandboxes.py +2 -2
  7. scalebox/api/client/client.py +288 -288
  8. scalebox/api/client/models/listed_sandbox.py +11 -9
  9. scalebox/api/client/models/new_sandbox.py +1 -1
  10. scalebox/api/client/models/sandbox.py +125 -125
  11. scalebox/api/client/models/sandbox_state.py +1 -0
  12. scalebox/api/client/types.py +46 -46
  13. scalebox/code_interpreter/code_interpreter_async.py +370 -369
  14. scalebox/code_interpreter/code_interpreter_sync.py +318 -317
  15. scalebox/connection_config.py +92 -92
  16. scalebox/csx_desktop/main.py +12 -12
  17. scalebox/generated/api_pb2_connect.py +17 -66
  18. scalebox/sandbox_async/commands/command.py +307 -307
  19. scalebox/sandbox_async/commands/command_handle.py +187 -187
  20. scalebox/sandbox_async/commands/pty.py +187 -187
  21. scalebox/sandbox_async/filesystem/filesystem.py +557 -557
  22. scalebox/sandbox_async/filesystem/watch_handle.py +61 -61
  23. scalebox/sandbox_async/main.py +647 -646
  24. scalebox/sandbox_async/sandbox_api.py +365 -365
  25. scalebox/sandbox_async/utils.py +7 -7
  26. scalebox/sandbox_sync/__init__.py +2 -2
  27. scalebox/sandbox_sync/commands/command.py +300 -300
  28. scalebox/sandbox_sync/commands/command_handle.py +150 -150
  29. scalebox/sandbox_sync/commands/pty.py +181 -181
  30. scalebox/sandbox_sync/filesystem/filesystem.py +543 -543
  31. scalebox/sandbox_sync/filesystem/watch_handle.py +66 -66
  32. scalebox/sandbox_sync/main.py +789 -790
  33. scalebox/sandbox_sync/sandbox_api.py +356 -356
  34. scalebox/test/CODE_INTERPRETER_TESTS_READY.md +256 -256
  35. scalebox/test/README.md +164 -164
  36. scalebox/test/aclient.py +72 -72
  37. scalebox/test/code_interpreter_centext.py +21 -21
  38. scalebox/test/code_interpreter_centext_sync.py +21 -21
  39. scalebox/test/code_interpreter_test.py +1 -1
  40. scalebox/test/code_interpreter_test_sync.py +1 -1
  41. scalebox/test/run_all_validation_tests.py +334 -334
  42. scalebox/test/test_basic.py +78 -78
  43. scalebox/test/test_code_interpreter_async_comprehensive.py +2653 -2653
  44. scalebox/test/{test_code_interpreter_e2bsync_comprehensive.py → test_code_interpreter_execcode.py} +328 -392
  45. scalebox/test/test_code_interpreter_sync_comprehensive.py +3416 -3412
  46. scalebox/test/test_csx_desktop_examples.py +130 -0
  47. scalebox/test/test_sandbox_async_comprehensive.py +736 -738
  48. scalebox/test/test_sandbox_stress_and_edge_cases.py +778 -778
  49. scalebox/test/test_sandbox_sync_comprehensive.py +779 -770
  50. scalebox/test/test_sandbox_usage_examples.py +987 -987
  51. scalebox/test/testacreate.py +24 -24
  52. scalebox/test/testagetinfo.py +18 -18
  53. scalebox/test/testcodeinterpreter_async.py +508 -508
  54. scalebox/test/testcodeinterpreter_sync.py +239 -239
  55. scalebox/test/testcomputeuse.py +2 -2
  56. scalebox/test/testnovnc.py +12 -12
  57. scalebox/test/testsandbox_api.py +15 -0
  58. scalebox/test/testsandbox_async.py +202 -118
  59. scalebox/test/testsandbox_sync.py +71 -38
  60. scalebox/version.py +2 -2
  61. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/METADATA +104 -103
  62. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/RECORD +66 -66
  63. scalebox/test/test_code_interpreter_e2basync_comprehensive.py +0 -2655
  64. scalebox/test/test_e2b_first.py +0 -11
  65. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/WHEEL +0 -0
  66. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/entry_points.txt +0 -0
  67. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/licenses/LICENSE +0 -0
  68. {scalebox_sdk-0.1.4.dist-info → scalebox_sdk-0.1.25.dist-info}/top_level.txt +0 -0
@@ -1,365 +1,365 @@
1
- import datetime
2
- import urllib.parse
3
- from typing import Dict, List, Optional
4
-
5
- from packaging.version import Version
6
-
7
- from ..api import AsyncApiClient, SandboxCreateResponse, handle_api_exception
8
- from ..api.client.api.sandboxes import (
9
- delete_sandboxes_sandbox_id,
10
- get_sandboxes,
11
- get_sandboxes_sandbox_id,
12
- get_sandboxes_sandbox_id_metrics,
13
- post_sandboxes,
14
- post_sandboxes_sandbox_id_timeout,
15
- )
16
- from ..api.client.models import Error, NewSandbox, PostSandboxesSandboxIDTimeoutBody
17
- from ..connection_config import ConnectionConfig, ProxyTypes
18
- from ..exceptions import SandboxException, TemplateException
19
- from ..sandbox.sandbox_api import (
20
- ListedSandbox,
21
- SandboxApiBase,
22
- SandboxInfo,
23
- SandboxMetrics,
24
- SandboxQuery,
25
- )
26
-
27
-
28
- class SandboxApi(SandboxApiBase):
29
- @classmethod
30
- async def list(
31
- cls,
32
- api_key: Optional[str] = None,
33
- query: Optional[SandboxQuery] = None,
34
- domain: Optional[str] = None,
35
- debug: Optional[bool] = None,
36
- request_timeout: Optional[float] = None,
37
- headers: Optional[Dict[str, str]] = None,
38
- proxy: Optional[ProxyTypes] = None,
39
- ) -> List[ListedSandbox]:
40
- """
41
- List all running sandboxes.
42
-
43
- :param api_key: API key to use for authentication, defaults to `E2B_API_KEY` environment variable
44
- :param query: Filter the list of sandboxes, e.g. by metadata `SandboxQuery(metadata={"key": "value"})`, if there are multiple filters they are combined with AND.
45
- :param domain: Domain to use for the request, only relevant for self-hosted environments
46
- :param debug: Enable debug mode, all requested are then sent to localhost
47
- :param request_timeout: Timeout for the request in **seconds**
48
- :param headers: Additional headers to send with the request
49
- :param proxy: Proxy to use for the request
50
-
51
- :return: List of running sandboxes
52
- """
53
- config = ConnectionConfig(
54
- api_key=api_key,
55
- domain=domain,
56
- debug=debug,
57
- request_timeout=request_timeout,
58
- headers=headers,
59
- proxy=proxy,
60
- )
61
-
62
- # Convert filters to the format expected by the API
63
- metadata = None
64
- if query:
65
- if query.metadata:
66
- quoted_metadata = {
67
- urllib.parse.quote(k): urllib.parse.quote(v)
68
- for k, v in query.metadata.items()
69
- }
70
- metadata = urllib.parse.urlencode(quoted_metadata)
71
-
72
- async with AsyncApiClient(
73
- config,
74
- limits=SandboxApiBase._limits,
75
- ) as api_client:
76
- res = await get_sandboxes.asyncio_detailed(
77
- client=api_client,
78
- metadata=metadata,
79
- )
80
-
81
- if res.status_code >= 300:
82
- raise handle_api_exception(res)
83
-
84
- if res.parsed is None:
85
- return []
86
-
87
- return [
88
- ListedSandbox(
89
- sandbox_id=sandbox.sandbox_id,
90
- template_id=sandbox.template_id,
91
- name=sandbox.alias if isinstance(sandbox.alias, str) else None,
92
- metadata=(
93
- sandbox.metadata if isinstance(sandbox.metadata, dict) else {}
94
- ),
95
- state=sandbox.state,
96
- cpu_count=sandbox.cpu_count,
97
- memory_mb=sandbox.memory_mb,
98
- started_at=sandbox.started_at,
99
- end_at=sandbox.end_at,
100
- )
101
- for sandbox in res.parsed
102
- ]
103
-
104
- @classmethod
105
- async def _cls_get_info(
106
- cls,
107
- sandbox_id: str,
108
- api_key: Optional[str] = None,
109
- domain: Optional[str] = None,
110
- debug: Optional[bool] = None,
111
- request_timeout: Optional[float] = None,
112
- headers: Optional[Dict[str, str]] = None,
113
- proxy: Optional[ProxyTypes] = None,
114
- ) -> SandboxInfo:
115
- """
116
- Get the sandbox info.
117
- :param sandbox_id: Sandbox ID
118
- :param api_key: API key to use for authentication, defaults to `E2B_API_KEY` environment variable
119
- :param domain: Domain to use for the request, defaults to `E2B_DOMAIN` environment variable
120
- :param debug: Debug mode, defaults to `E2B_DEBUG` environment variable
121
- :param request_timeout: Timeout for the request in **seconds**
122
- :param headers: Additional headers to send with the request
123
- :param proxy: Proxy to use for the request
124
-
125
- :return: Sandbox info
126
- """
127
- config = ConnectionConfig(
128
- api_key=api_key,
129
- domain=domain,
130
- debug=debug,
131
- request_timeout=request_timeout,
132
- headers=headers,
133
- proxy=proxy,
134
- )
135
-
136
- async with AsyncApiClient(
137
- config,
138
- limits=SandboxApiBase._limits,
139
- ) as api_client:
140
- res = await get_sandboxes_sandbox_id.asyncio_detailed(
141
- sandbox_id,
142
- client=api_client,
143
- )
144
-
145
- if res.status_code >= 300:
146
- raise handle_api_exception(res)
147
-
148
- if res.parsed is None:
149
- raise Exception("Body of the request is None")
150
-
151
- return SandboxInfo(
152
- sandbox_id=res.parsed.sandbox_id,
153
- sandbox_domain=res.parsed.domain,
154
- template_id=res.parsed.template_id,
155
- name=res.parsed.alias if isinstance(res.parsed.alias, str) else None,
156
- metadata=(
157
- res.parsed.metadata if isinstance(res.parsed.metadata, dict) else {}
158
- ),
159
- started_at=res.parsed.started_at,
160
- end_at=res.parsed.end_at,
161
- envd_version=res.parsed.envd_version,
162
- _envd_access_token=res.parsed.envd_access_token,
163
- )
164
-
165
- @classmethod
166
- async def _cls_kill(
167
- cls,
168
- sandbox_id: str,
169
- api_key: Optional[str] = None,
170
- domain: Optional[str] = None,
171
- debug: Optional[bool] = None,
172
- request_timeout: Optional[float] = None,
173
- headers: Optional[Dict[str, str]] = None,
174
- proxy: Optional[ProxyTypes] = None,
175
- ) -> bool:
176
- config = ConnectionConfig(
177
- api_key=api_key,
178
- domain=domain,
179
- debug=debug,
180
- request_timeout=request_timeout,
181
- headers=headers,
182
- proxy=proxy,
183
- )
184
-
185
- if config.debug:
186
- # Skip killing the sandbox in debug mode
187
- return True
188
-
189
- async with AsyncApiClient(
190
- config,
191
- limits=SandboxApiBase._limits,
192
- ) as api_client:
193
- res = await delete_sandboxes_sandbox_id.asyncio_detailed(
194
- sandbox_id,
195
- client=api_client,
196
- )
197
-
198
- if res.status_code == 404:
199
- return False
200
-
201
- if res.status_code >= 300:
202
- raise handle_api_exception(res)
203
-
204
- return True
205
-
206
- @classmethod
207
- async def _cls_set_timeout(
208
- cls,
209
- sandbox_id: str,
210
- timeout: int,
211
- api_key: Optional[str] = None,
212
- domain: Optional[str] = None,
213
- debug: Optional[bool] = None,
214
- request_timeout: Optional[float] = None,
215
- headers: Optional[Dict[str, str]] = None,
216
- proxy: Optional[ProxyTypes] = None,
217
- ) -> None:
218
- config = ConnectionConfig(
219
- api_key=api_key,
220
- domain=domain,
221
- debug=debug,
222
- request_timeout=request_timeout,
223
- headers=headers,
224
- proxy=proxy,
225
- )
226
-
227
- if config.debug:
228
- # Skip setting the timeout in debug mode
229
- return
230
-
231
- async with AsyncApiClient(
232
- config,
233
- limits=SandboxApiBase._limits,
234
- ) as api_client:
235
- res = await post_sandboxes_sandbox_id_timeout.asyncio_detailed(
236
- sandbox_id,
237
- client=api_client,
238
- body=PostSandboxesSandboxIDTimeoutBody(timeout=timeout),
239
- )
240
-
241
- if res.status_code >= 300:
242
- raise handle_api_exception(res)
243
-
244
- @classmethod
245
- async def _create_sandbox(
246
- cls,
247
- template: str,
248
- timeout: int,
249
- metadata: Optional[Dict[str, str]] = None,
250
- env_vars: Optional[Dict[str, str]] = None,
251
- secure: Optional[bool] = None,
252
- api_key: Optional[str] = None,
253
- domain: Optional[str] = None,
254
- debug: Optional[bool] = None,
255
- request_timeout: Optional[float] = None,
256
- headers: Optional[Dict[str, str]] = None,
257
- proxy: Optional[ProxyTypes] = None,
258
- allow_internet_access: Optional[bool] = True,
259
- ) -> SandboxCreateResponse:
260
- config = ConnectionConfig(
261
- api_key=api_key,
262
- domain=domain,
263
- debug=debug,
264
- request_timeout=request_timeout,
265
- headers=headers,
266
- proxy=proxy,
267
- )
268
-
269
- async with AsyncApiClient(
270
- config,
271
- limits=SandboxApiBase._limits,
272
- ) as api_client:
273
- res = await post_sandboxes.asyncio_detailed(
274
- body=NewSandbox(
275
- template_id=template,
276
- metadata=metadata or {},
277
- timeout=timeout,
278
- env_vars=env_vars or {},
279
- secure=secure or False,
280
- allow_internet_access=allow_internet_access,
281
- is_async=False,
282
- ),
283
- client=api_client,
284
- )
285
-
286
- if res.status_code >= 300:
287
- raise handle_api_exception(res)
288
-
289
- if res.parsed is None:
290
- raise Exception("Body of the request is None")
291
-
292
- # if Version(res.parsed.envd_version) < Version("0.1.0"):
293
- # await SandboxApi._cls_kill(res.parsed.sandbox_id)
294
- # raise TemplateException(
295
- # "You need to update the template to use the new SDK. "
296
- # "You can do this by running `e2b template build` in the directory with the template."
297
- # )
298
-
299
- return SandboxCreateResponse(
300
- sandbox_id=res.parsed.sandbox_id,
301
- sandbox_domain=res.parsed.domain,
302
- envd_version=res.parsed.envd_version,
303
- envd_access_token=res.parsed.envd_access_token,
304
- )
305
-
306
- @classmethod
307
- async def _cls_get_metrics(
308
- cls,
309
- sandbox_id: str,
310
- start: Optional[datetime.datetime] = None,
311
- end: Optional[datetime.datetime] = None,
312
- api_key: Optional[str] = None,
313
- domain: Optional[str] = None,
314
- debug: Optional[bool] = None,
315
- request_timeout: Optional[float] = None,
316
- headers: Optional[Dict[str, str]] = None,
317
- proxy: Optional[ProxyTypes] = None,
318
- ) -> List[SandboxMetrics]:
319
- config = ConnectionConfig(
320
- api_key=api_key,
321
- domain=domain,
322
- debug=debug,
323
- headers=headers,
324
- request_timeout=request_timeout,
325
- proxy=proxy,
326
- )
327
-
328
- if config.debug:
329
- # Skip getting the metrics in debug mode
330
- return []
331
-
332
- async with AsyncApiClient(
333
- config,
334
- limits=cls._limits,
335
- ) as api_client:
336
- res = await get_sandboxes_sandbox_id_metrics.asyncio_detailed(
337
- sandbox_id,
338
- start=int(start.timestamp() * 1000) if start else None,
339
- end=int(end.timestamp() * 1000) if end else None,
340
- client=api_client,
341
- )
342
-
343
- if res.status_code >= 300:
344
- raise handle_api_exception(res)
345
-
346
- if res.parsed is None:
347
- return []
348
-
349
- # Check if res.parse is Error
350
- if isinstance(res.parsed, Error):
351
- raise SandboxException(f"{res.parsed.message}: Request failed")
352
-
353
- # Convert to typed SandboxMetrics objects
354
- return [
355
- SandboxMetrics(
356
- cpu_count=metric.cpu_count,
357
- cpu_used_pct=metric.cpu_used_pct,
358
- disk_total=metric.disk_total,
359
- disk_used=metric.disk_used,
360
- mem_total=metric.mem_total,
361
- mem_used=metric.mem_used,
362
- timestamp=metric.timestamp,
363
- )
364
- for metric in res.parsed
365
- ]
1
+ import datetime
2
+ import urllib.parse
3
+ from typing import Dict, List, Optional
4
+
5
+ from packaging.version import Version
6
+
7
+ from ..api import AsyncApiClient, SandboxCreateResponse, handle_api_exception
8
+ from ..api.client.api.sandboxes import (
9
+ delete_sandboxes_sandbox_id,
10
+ get_sandboxes,
11
+ get_sandboxes_sandbox_id,
12
+ get_sandboxes_sandbox_id_metrics,
13
+ post_sandboxes,
14
+ post_sandboxes_sandbox_id_timeout,
15
+ )
16
+ from ..api.client.models import Error, NewSandbox, PostSandboxesSandboxIDTimeoutBody
17
+ from ..connection_config import ConnectionConfig, ProxyTypes
18
+ from ..exceptions import SandboxException, TemplateException
19
+ from ..sandbox.sandbox_api import (
20
+ ListedSandbox,
21
+ SandboxApiBase,
22
+ SandboxInfo,
23
+ SandboxMetrics,
24
+ SandboxQuery,
25
+ )
26
+
27
+
28
+ class SandboxApi(SandboxApiBase):
29
+ @classmethod
30
+ async def list(
31
+ cls,
32
+ api_key: Optional[str] = None,
33
+ query: Optional[SandboxQuery] = None,
34
+ domain: Optional[str] = None,
35
+ debug: Optional[bool] = None,
36
+ request_timeout: Optional[float] = None,
37
+ headers: Optional[Dict[str, str]] = None,
38
+ proxy: Optional[ProxyTypes] = None,
39
+ ) -> List[ListedSandbox]:
40
+ """
41
+ List all running sandboxes.
42
+
43
+ :param api_key: API key to use for authentication, defaults to `SBX_API_KEY` environment variable
44
+ :param query: Filter the list of sandboxes, e.g. by metadata `SandboxQuery(metadata={"key": "value"})`, if there are multiple filters they are combined with AND.
45
+ :param domain: Domain to use for the request, only relevant for self-hosted environments
46
+ :param debug: Enable debug mode, all requested are then sent to localhost
47
+ :param request_timeout: Timeout for the request in **seconds**
48
+ :param headers: Additional headers to send with the request
49
+ :param proxy: Proxy to use for the request
50
+
51
+ :return: List of running sandboxes
52
+ """
53
+ config = ConnectionConfig(
54
+ api_key=api_key,
55
+ domain=domain,
56
+ debug=debug,
57
+ request_timeout=request_timeout,
58
+ headers=headers,
59
+ proxy=proxy,
60
+ )
61
+
62
+ # Convert filters to the format expected by the API
63
+ metadata = None
64
+ if query:
65
+ if query.metadata:
66
+ quoted_metadata = {
67
+ urllib.parse.quote(k): urllib.parse.quote(v)
68
+ for k, v in query.metadata.items()
69
+ }
70
+ metadata = urllib.parse.urlencode(quoted_metadata)
71
+
72
+ async with AsyncApiClient(
73
+ config,
74
+ limits=SandboxApiBase._limits,
75
+ ) as api_client:
76
+ res = await get_sandboxes.asyncio_detailed(
77
+ client=api_client,
78
+ metadata=metadata,
79
+ )
80
+
81
+ if res.status_code >= 300:
82
+ raise handle_api_exception(res)
83
+
84
+ if res.parsed is None:
85
+ return []
86
+
87
+ return [
88
+ ListedSandbox(
89
+ sandbox_id=sandbox.sandbox_id,
90
+ template_id=sandbox.template_id,
91
+ name=sandbox.alias if isinstance(sandbox.alias, str) else None,
92
+ metadata=(
93
+ sandbox.metadata if isinstance(sandbox.metadata, dict) else {}
94
+ ),
95
+ state=sandbox.state,
96
+ cpu_count=sandbox.cpu_count,
97
+ memory_mb=sandbox.memory_mb,
98
+ started_at=sandbox.started_at,
99
+ end_at=sandbox.end_at,
100
+ )
101
+ for sandbox in res.parsed
102
+ ]
103
+
104
+ @classmethod
105
+ async def _cls_get_info(
106
+ cls,
107
+ sandbox_id: str,
108
+ api_key: Optional[str] = None,
109
+ domain: Optional[str] = None,
110
+ debug: Optional[bool] = None,
111
+ request_timeout: Optional[float] = None,
112
+ headers: Optional[Dict[str, str]] = None,
113
+ proxy: Optional[ProxyTypes] = None,
114
+ ) -> SandboxInfo:
115
+ """
116
+ Get the sandbox info.
117
+ :param sandbox_id: Sandbox ID
118
+ :param api_key: API key to use for authentication, defaults to `SBX_API_KEY` environment variable
119
+ :param domain: Domain to use for the request, defaults to `SBX_DOMAIN` environment variable
120
+ :param debug: Debug mode, defaults to `SBX_DEBUG` environment variable
121
+ :param request_timeout: Timeout for the request in **seconds**
122
+ :param headers: Additional headers to send with the request
123
+ :param proxy: Proxy to use for the request
124
+
125
+ :return: Sandbox info
126
+ """
127
+ config = ConnectionConfig(
128
+ api_key=api_key,
129
+ domain=domain,
130
+ debug=debug,
131
+ request_timeout=request_timeout,
132
+ headers=headers,
133
+ proxy=proxy,
134
+ )
135
+
136
+ async with AsyncApiClient(
137
+ config,
138
+ limits=SandboxApiBase._limits,
139
+ ) as api_client:
140
+ res = await get_sandboxes_sandbox_id.asyncio_detailed(
141
+ sandbox_id,
142
+ client=api_client,
143
+ )
144
+
145
+ if res.status_code >= 300:
146
+ raise handle_api_exception(res)
147
+
148
+ if res.parsed is None:
149
+ raise Exception("Body of the request is None")
150
+
151
+ return SandboxInfo(
152
+ sandbox_id=res.parsed.sandbox_id,
153
+ sandbox_domain=res.parsed.domain,
154
+ template_id=res.parsed.template_id,
155
+ name=res.parsed.alias if isinstance(res.parsed.alias, str) else None,
156
+ metadata=(
157
+ res.parsed.metadata if isinstance(res.parsed.metadata, dict) else {}
158
+ ),
159
+ started_at=res.parsed.started_at,
160
+ end_at=res.parsed.end_at,
161
+ envd_version=res.parsed.envd_version,
162
+ _envd_access_token=res.parsed.envd_access_token,
163
+ )
164
+
165
+ @classmethod
166
+ async def _cls_kill(
167
+ cls,
168
+ sandbox_id: str,
169
+ api_key: Optional[str] = None,
170
+ domain: Optional[str] = None,
171
+ debug: Optional[bool] = None,
172
+ request_timeout: Optional[float] = None,
173
+ headers: Optional[Dict[str, str]] = None,
174
+ proxy: Optional[ProxyTypes] = None,
175
+ ) -> bool:
176
+ config = ConnectionConfig(
177
+ api_key=api_key,
178
+ domain=domain,
179
+ debug=debug,
180
+ request_timeout=request_timeout,
181
+ headers=headers,
182
+ proxy=proxy,
183
+ )
184
+
185
+ if config.debug:
186
+ # Skip killing the sandbox in debug mode
187
+ return True
188
+
189
+ async with AsyncApiClient(
190
+ config,
191
+ limits=SandboxApiBase._limits,
192
+ ) as api_client:
193
+ res = await delete_sandboxes_sandbox_id.asyncio_detailed(
194
+ sandbox_id,
195
+ client=api_client,
196
+ )
197
+
198
+ if res.status_code == 404:
199
+ return False
200
+
201
+ if res.status_code >= 300:
202
+ raise handle_api_exception(res)
203
+
204
+ return True
205
+
206
+ @classmethod
207
+ async def _cls_set_timeout(
208
+ cls,
209
+ sandbox_id: str,
210
+ timeout: int,
211
+ api_key: Optional[str] = None,
212
+ domain: Optional[str] = None,
213
+ debug: Optional[bool] = None,
214
+ request_timeout: Optional[float] = None,
215
+ headers: Optional[Dict[str, str]] = None,
216
+ proxy: Optional[ProxyTypes] = None,
217
+ ) -> None:
218
+ config = ConnectionConfig(
219
+ api_key=api_key,
220
+ domain=domain,
221
+ debug=debug,
222
+ request_timeout=request_timeout,
223
+ headers=headers,
224
+ proxy=proxy,
225
+ )
226
+
227
+ if config.debug:
228
+ # Skip setting the timeout in debug mode
229
+ return
230
+
231
+ async with AsyncApiClient(
232
+ config,
233
+ limits=SandboxApiBase._limits,
234
+ ) as api_client:
235
+ res = await post_sandboxes_sandbox_id_timeout.asyncio_detailed(
236
+ sandbox_id,
237
+ client=api_client,
238
+ body=PostSandboxesSandboxIDTimeoutBody(timeout=timeout),
239
+ )
240
+
241
+ if res.status_code >= 300:
242
+ raise handle_api_exception(res)
243
+
244
+ @classmethod
245
+ async def _create_sandbox(
246
+ cls,
247
+ template: str,
248
+ timeout: int,
249
+ metadata: Optional[Dict[str, str]] = None,
250
+ env_vars: Optional[Dict[str, str]] = None,
251
+ secure: Optional[bool] = None,
252
+ api_key: Optional[str] = None,
253
+ domain: Optional[str] = None,
254
+ debug: Optional[bool] = None,
255
+ request_timeout: Optional[float] = None,
256
+ headers: Optional[Dict[str, str]] = None,
257
+ proxy: Optional[ProxyTypes] = None,
258
+ allow_internet_access: Optional[bool] = True,
259
+ ) -> SandboxCreateResponse:
260
+ config = ConnectionConfig(
261
+ api_key=api_key,
262
+ domain=domain,
263
+ debug=debug,
264
+ request_timeout=request_timeout,
265
+ headers=headers,
266
+ proxy=proxy,
267
+ )
268
+
269
+ async with AsyncApiClient(
270
+ config,
271
+ limits=SandboxApiBase._limits,
272
+ ) as api_client:
273
+ res = await post_sandboxes.asyncio_detailed(
274
+ body=NewSandbox(
275
+ template_id=template,
276
+ metadata=metadata or {},
277
+ timeout=timeout,
278
+ env_vars=env_vars or {},
279
+ secure=secure or False,
280
+ allow_internet_access=allow_internet_access,
281
+ is_async=False,
282
+ ),
283
+ client=api_client,
284
+ )
285
+
286
+ if res.status_code >= 300:
287
+ raise handle_api_exception(res)
288
+
289
+ if res.parsed is None:
290
+ raise Exception("Body of the request is None")
291
+
292
+ # if Version(res.parsed.envd_version) < Version("0.1.0"):
293
+ # await SandboxApi._cls_kill(res.parsed.sandbox_id)
294
+ # raise TemplateException(
295
+ # "You need to update the template to use the new SDK. "
296
+ # "You can do this by running `scalebox template build` in the directory with the template."
297
+ # )
298
+
299
+ return SandboxCreateResponse(
300
+ sandbox_id=res.parsed.sandbox_id,
301
+ sandbox_domain=res.parsed.domain,
302
+ envd_version=res.parsed.envd_version,
303
+ envd_access_token=res.parsed.envd_access_token,
304
+ )
305
+
306
+ @classmethod
307
+ async def _cls_get_metrics(
308
+ cls,
309
+ sandbox_id: str,
310
+ start: Optional[datetime.datetime] = None,
311
+ end: Optional[datetime.datetime] = None,
312
+ api_key: Optional[str] = None,
313
+ domain: Optional[str] = None,
314
+ debug: Optional[bool] = None,
315
+ request_timeout: Optional[float] = None,
316
+ headers: Optional[Dict[str, str]] = None,
317
+ proxy: Optional[ProxyTypes] = None,
318
+ ) -> List[SandboxMetrics]:
319
+ config = ConnectionConfig(
320
+ api_key=api_key,
321
+ domain=domain,
322
+ debug=debug,
323
+ headers=headers,
324
+ request_timeout=request_timeout,
325
+ proxy=proxy,
326
+ )
327
+
328
+ if config.debug:
329
+ # Skip getting the metrics in debug mode
330
+ return []
331
+
332
+ async with AsyncApiClient(
333
+ config,
334
+ limits=cls._limits,
335
+ ) as api_client:
336
+ res = await get_sandboxes_sandbox_id_metrics.asyncio_detailed(
337
+ sandbox_id,
338
+ start=int(start.timestamp() * 1000) if start else None,
339
+ end=int(end.timestamp() * 1000) if end else None,
340
+ client=api_client,
341
+ )
342
+
343
+ if res.status_code >= 300:
344
+ raise handle_api_exception(res)
345
+
346
+ if res.parsed is None:
347
+ return []
348
+
349
+ # Check if res.parse is Error
350
+ if isinstance(res.parsed, Error):
351
+ raise SandboxException(f"{res.parsed.message}: Request failed")
352
+
353
+ # Convert to typed SandboxMetrics objects
354
+ return [
355
+ SandboxMetrics(
356
+ cpu_count=metric.cpu_count,
357
+ cpu_used_pct=metric.cpu_used_pct,
358
+ disk_total=metric.disk_total,
359
+ disk_used=metric.disk_used,
360
+ mem_total=metric.mem_total,
361
+ mem_used=metric.mem_used,
362
+ timestamp=metric.timestamp,
363
+ )
364
+ for metric in res.parsed
365
+ ]