|
13 | 13 | NewSandbox, |
14 | 14 | PostSandboxesSandboxIDTimeoutBody, |
15 | 15 | Error, |
16 | | - ResumedSandbox, |
| 16 | + ConnectSandbox, |
| 17 | + Sandbox, |
17 | 18 | ) |
18 | 19 | from e2b.api.client.api.sandboxes import ( |
19 | 20 | get_sandboxes_sandbox_id, |
|
22 | 23 | post_sandboxes, |
23 | 24 | get_sandboxes_sandbox_id_metrics, |
24 | 25 | post_sandboxes_sandbox_id_pause, |
25 | | - post_sandboxes_sandbox_id_resume, |
| 26 | + post_sandboxes_sandbox_id_connect, |
26 | 27 | ) |
27 | 28 | from e2b.connection_config import ConnectionConfig, ApiParams |
28 | 29 | from e2b.api import handle_api_exception |
@@ -282,52 +283,42 @@ async def _cls_pause( |
282 | 283 | if res.status_code >= 300: |
283 | 284 | raise handle_api_exception(res) |
284 | 285 |
|
| 286 | + # Check if res.parse is Error |
| 287 | + if isinstance(res.parsed, Error): |
| 288 | + raise SandboxException(f"{res.parsed.message}: Request failed") |
| 289 | + |
285 | 290 | return sandbox_id |
286 | 291 |
|
287 | 292 | @classmethod |
288 | | - async def _cls_resume( |
| 293 | + async def _cls_connect( |
289 | 294 | cls, |
290 | 295 | sandbox_id: str, |
291 | 296 | timeout: Optional[int] = None, |
292 | 297 | **opts: Unpack[ApiParams], |
293 | | - ) -> bool: |
| 298 | + ) -> Sandbox: |
294 | 299 | timeout = timeout or SandboxBase.default_sandbox_timeout |
295 | 300 |
|
296 | | - # Temporary solution (02/12/2025), |
297 | | - # Options discussed: |
298 | | - # 1. No set - never sure how long the sandbox will be running |
299 | | - # 2. Always set the timeout in code - the user can't just connect to the sandbox |
300 | | - # without changing the timeout, round trip to the server time |
301 | | - # 3. Set the timeout in resume on backend - side effect on error |
302 | | - # 4. Create new endpoint for connect |
303 | | - try: |
304 | | - await SandboxApi._cls_set_timeout( |
305 | | - sandbox_id=sandbox_id, |
306 | | - timeout=timeout, |
307 | | - **opts, |
| 301 | + # Sandbox is not running, resume it |
| 302 | + config = ConnectionConfig(**opts) |
| 303 | + |
| 304 | + async with AsyncApiClient( |
| 305 | + config, |
| 306 | + limits=SandboxBase._limits, |
| 307 | + ) as api_client: |
| 308 | + res = await post_sandboxes_sandbox_id_connect.asyncio_detailed( |
| 309 | + sandbox_id, |
| 310 | + client=api_client, |
| 311 | + body=ConnectSandbox(timeout=timeout), |
308 | 312 | ) |
309 | | - return False |
310 | | - except SandboxException: |
311 | | - # Sandbox is not running, resume it |
312 | | - config = ConnectionConfig(**opts) |
313 | | - |
314 | | - async with AsyncApiClient( |
315 | | - config, |
316 | | - limits=SandboxBase._limits, |
317 | | - ) as api_client: |
318 | | - res = await post_sandboxes_sandbox_id_resume.asyncio_detailed( |
319 | | - sandbox_id, |
320 | | - client=api_client, |
321 | | - body=ResumedSandbox(timeout=timeout), |
322 | | - ) |
323 | 313 |
|
324 | | - if res.status_code == 404: |
325 | | - raise NotFoundException(f"Paused sandbox {sandbox_id} not found") |
| 314 | + if res.status_code == 404: |
| 315 | + raise NotFoundException(f"Paused sandbox {sandbox_id} not found") |
326 | 316 |
|
327 | | - if res.status_code == 409: |
328 | | - return False |
| 317 | + if res.status_code >= 300: |
| 318 | + raise handle_api_exception(res) |
329 | 319 |
|
330 | | - if res.status_code >= 300: |
331 | | - raise handle_api_exception(res) |
| 320 | + # Check if res.parse is Error |
| 321 | + if isinstance(res.parsed, Error): |
| 322 | + raise SandboxException(f"{res.parsed.message}: Request failed") |
332 | 323 |
|
333 | | - return True |
| 324 | + return res.parsed |
0 commit comments