|
1 | 1 | import logging |
2 | 2 | import httpx |
3 | 3 |
|
4 | | -from typing import Optional, Dict, overload, Literal, Union |
| 4 | +from typing import Optional, Dict, overload, Literal, Union, List |
5 | 5 | from httpx import Client |
6 | 6 | from e2b import Sandbox as BaseSandbox, InvalidArgumentException |
7 | 7 |
|
@@ -270,3 +270,89 @@ def create_code_context( |
270 | 270 | return Context.from_json(data) |
271 | 271 | except httpx.TimeoutException: |
272 | 272 | raise format_request_timeout_error() |
| 273 | + |
| 274 | + def remove_code_context( |
| 275 | + self, |
| 276 | + context: Union[Context, str], |
| 277 | + ) -> None: |
| 278 | + """ |
| 279 | + Removes a context. |
| 280 | +
|
| 281 | + :param context: Context to remove. Can be a Context object or a context ID string. |
| 282 | +
|
| 283 | + :return: None |
| 284 | + """ |
| 285 | + context_id = context.id if isinstance(context, Context) else context |
| 286 | + |
| 287 | + headers: Dict[str, str] = {} |
| 288 | + if self._envd_access_token: |
| 289 | + headers = {"X-Access-Token": self._envd_access_token} |
| 290 | + |
| 291 | + try: |
| 292 | + response = self._client.delete( |
| 293 | + f"{self._jupyter_url}/contexts/{context_id}", |
| 294 | + headers=headers, |
| 295 | + timeout=self.connection_config.request_timeout, |
| 296 | + ) |
| 297 | + |
| 298 | + err = extract_exception(response) |
| 299 | + if err: |
| 300 | + raise err |
| 301 | + except httpx.TimeoutException: |
| 302 | + raise format_request_timeout_error() |
| 303 | + |
| 304 | + def list_code_contexts(self) -> List[Context]: |
| 305 | + """ |
| 306 | + List all contexts. |
| 307 | +
|
| 308 | + :return: List of contexts. |
| 309 | + """ |
| 310 | + headers: Dict[str, str] = {} |
| 311 | + if self._envd_access_token: |
| 312 | + headers = {"X-Access-Token": self._envd_access_token} |
| 313 | + |
| 314 | + try: |
| 315 | + response = self._client.get( |
| 316 | + f"{self._jupyter_url}/contexts", |
| 317 | + headers=headers, |
| 318 | + timeout=self.connection_config.request_timeout, |
| 319 | + ) |
| 320 | + |
| 321 | + err = extract_exception(response) |
| 322 | + if err: |
| 323 | + raise err |
| 324 | + |
| 325 | + data = response.json() |
| 326 | + return [Context.from_json(context_data) for context_data in data] |
| 327 | + except httpx.TimeoutException: |
| 328 | + raise format_request_timeout_error() |
| 329 | + |
| 330 | + def restart_code_context( |
| 331 | + self, |
| 332 | + context: Union[Context, str], |
| 333 | + ) -> None: |
| 334 | + """ |
| 335 | + Restart a context. |
| 336 | +
|
| 337 | + :param context: Context to restart. Can be a Context object or a context ID string. |
| 338 | +
|
| 339 | + :return: None |
| 340 | + """ |
| 341 | + context_id = context.id if isinstance(context, Context) else context |
| 342 | + |
| 343 | + headers: Dict[str, str] = {} |
| 344 | + if self._envd_access_token: |
| 345 | + headers = {"X-Access-Token": self._envd_access_token} |
| 346 | + |
| 347 | + try: |
| 348 | + response = self._client.post( |
| 349 | + f"{self._jupyter_url}/contexts/{context_id}/restart", |
| 350 | + headers=headers, |
| 351 | + timeout=self.connection_config.request_timeout, |
| 352 | + ) |
| 353 | + |
| 354 | + err = extract_exception(response) |
| 355 | + if err: |
| 356 | + raise err |
| 357 | + except httpx.TimeoutException: |
| 358 | + raise format_request_timeout_error() |
0 commit comments