Skip to content

Commit 7570417

Browse files
committed
Simplify connection config
1 parent 0bace98 commit 7570417

15 files changed

Lines changed: 344 additions & 607 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/python-sdk': minor
3+
---
4+
5+
Refactor connection config and parameters for all calls to API

packages/python-sdk/e2b/connection_config.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22

33
from typing import Literal, Optional, Dict
4+
from dataclasses import dataclass
45
from httpx._types import ProxyTypes
6+
from typing_extensions import Unpack
57

68
from e2b.api.metadata import package_version
79

@@ -11,6 +13,33 @@
1113
KEEPALIVE_PING_HEADER = "Keepalive-Ping-Interval"
1214

1315

16+
@dataclass
17+
class ApiParams:
18+
"""
19+
Parameters for a request.
20+
21+
In the case of a sandbox, it applies to all **requests made to the returned sandbox**.
22+
"""
23+
24+
request_timeout: Optional[float]
25+
"""Timeout for the request in **seconds**, defaults to 60 seconds."""
26+
27+
headers: Dict[str, str]
28+
"""Additional headers to send with the request."""
29+
30+
api_key: Optional[str]
31+
"""E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable."""
32+
33+
domain: str
34+
"""E2B domain to use for authentication, defaults to `E2B_DOMAIN` environment variable."""
35+
36+
debug: bool
37+
"""Whether to use debug mode, defaults to `E2B_DEBUG` environment variable."""
38+
39+
proxy: Optional[ProxyTypes]
40+
"""Proxy to use for the request. In case of a sandbox it applies to all **requests made to the returned sandbox**."""
41+
42+
1443
class ConnectionConfig:
1544
"""
1645
Configuration for the connection to the API.
@@ -40,6 +69,7 @@ def __init__(
4069
access_token: Optional[str] = None,
4170
request_timeout: Optional[float] = None,
4271
headers: Optional[Dict[str, str]] = None,
72+
extra_sandbox_headers: Optional[Dict[str, str]] = None,
4373
proxy: Optional[ProxyTypes] = None,
4474
):
4575
self.domain = domain or ConnectionConfig._domain()
@@ -48,6 +78,7 @@ def __init__(
4878
self.access_token = access_token or ConnectionConfig._access_token()
4979
self.headers = headers or {}
5080
self.headers["User-Agent"] = f"e2b-python-sdk/{package_version}"
81+
self.__extra_sandbox_headers = extra_sandbox_headers or {}
5182

5283
self.proxy = proxy
5384

@@ -82,6 +113,48 @@ def _get_request_timeout(
82113
def get_request_timeout(self, request_timeout: Optional[float] = None):
83114
return self._get_request_timeout(self.request_timeout, request_timeout)
84115

116+
def get_api_params(
117+
self,
118+
**opts: Unpack[ApiParams],
119+
) -> dict:
120+
"""
121+
Get the parameters for the API call.
122+
123+
This is used to avoid passing the following attributes to the API call:
124+
- access_token
125+
- api_url
126+
127+
It also returns a copy, so the original object is not modified.
128+
129+
:return: Dictionary of parameters for the API call
130+
"""
131+
headers = opts.get("headers")
132+
request_timeout = opts.get("request_timeout")
133+
api_key = opts.get("api_key")
134+
domain = opts.get("domain")
135+
debug = opts.get("debug")
136+
proxy = opts.get("proxy")
137+
138+
req_headers = self.headers.copy()
139+
if headers is not None:
140+
req_headers.update(headers)
141+
142+
return ApiParams(
143+
api_key=api_key if api_key is not None else self.api_key,
144+
domain=domain if domain is not None else self.domain,
145+
debug=debug if debug is not None else self.debug,
146+
request_timeout=self.get_request_timeout(request_timeout),
147+
headers=req_headers,
148+
proxy=proxy if proxy is not None else self.proxy,
149+
).__dict__
150+
151+
@property
152+
def sandbox_headers(self):
153+
return {
154+
**self.headers,
155+
**self.__extra_sandbox_headers,
156+
}
157+
85158

86159
Username = Literal["root", "user"]
87160
"""

packages/python-sdk/e2b/sandbox/main.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import urllib.parse
22

3-
from abc import ABC, abstractmethod
43
from typing import Optional
54

65
from e2b.sandbox.signature import get_signature
@@ -9,7 +8,7 @@
98
from httpx import Limits
109

1110

12-
class SandboxSetup(ABC):
11+
class SandboxSetup:
1312
_limits = Limits(
1413
max_keepalive_connections=40,
1514
max_connections=40,
@@ -21,30 +20,48 @@ class SandboxSetup(ABC):
2120
default_sandbox_timeout = 300
2221
default_template = "base"
2322

23+
def __init__(
24+
self,
25+
sandbox_id: str,
26+
envd_version: Optional[str],
27+
envd_access_token: Optional[str],
28+
sandbox_domain: Optional[str],
29+
connection_config: ConnectionConfig,
30+
):
31+
self.__connection_config = connection_config
32+
self.__sandbox_id = sandbox_id
33+
self.__sandbox_domain = sandbox_domain or self.connection_config.domain
34+
self.__envd_version = envd_version
35+
self.__envd_access_token = envd_access_token
36+
self.__envd_api_url = f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(self.envd_port)}"
37+
38+
@property
39+
def _envd_access_token(self) -> Optional[str]:
40+
"""Private property to access the envd token"""
41+
return self.__envd_access_token
42+
2443
@property
25-
@abstractmethod
2644
def connection_config(self) -> ConnectionConfig:
27-
...
45+
return self.__connection_config
2846

2947
@property
30-
@abstractmethod
31-
def _envd_access_token(self) -> Optional[str]:
32-
...
48+
def _envd_version(self) -> Optional[str]:
49+
return self.__envd_version
3350

3451
@property
35-
@abstractmethod
36-
def envd_api_url(self) -> str:
37-
...
52+
def sandbox_domain(self) -> Optional[str]:
53+
return self.__sandbox_domain
3854

3955
@property
40-
@abstractmethod
41-
def sandbox_id(self) -> str:
42-
...
56+
def envd_api_url(self) -> str:
57+
return self.__envd_api_url
4358

4459
@property
45-
@abstractmethod
46-
def sandbox_domain(self) -> str:
47-
...
60+
def sandbox_id(self) -> str:
61+
"""
62+
Unique identifier of the sandbox.
63+
"""
64+
return self.__sandbox_id
4865

4966
def _file_url(
5067
self,

packages/python-sdk/e2b/sandbox/sandbox_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SandboxInfo:
2828
"""Sandbox expiration date."""
2929
envd_version: Optional[str]
3030
"""Envd version."""
31-
_envd_access_token: Optional[str]
31+
envd_access_token: Optional[str]
3232
"""Envd access token."""
3333

3434

packages/python-sdk/e2b/sandbox_async/commands/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(
3535
# compressor=e2b_connect.GzipCompressor,
3636
async_pool=pool,
3737
json=True,
38-
headers=connection_config.headers,
38+
headers=connection_config.sandbox_headers,
3939
)
4040

4141
async def list(

packages/python-sdk/e2b/sandbox_async/commands/pty.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(
3939
# compressor=e2b_connect.GzipCompressor,
4040
async_pool=pool,
4141
json=True,
42+
headers=connection_config.sandbox_headers,
4243
)
4344

4445
async def kill(

packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(
5151
# compressor=e2b_connect.GzipCompressor,
5252
async_pool=pool,
5353
json=True,
54-
headers=connection_config.headers,
54+
headers=connection_config.sandbox_headers,
5555
)
5656

5757
@overload

0 commit comments

Comments
 (0)