Skip to content

Commit ff1bee5

Browse files
committed
call it sandbox_url, implement async
1 parent 4ae206b commit ff1bee5

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

packages/python-sdk/e2b/connection_config.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ class ApiParams(TypedDict, total=False):
4040
proxy: Optional[ProxyTypes]
4141
"""Proxy to use for the request. In case of a sandbox it applies to all **requests made to the returned sandbox**."""
4242

43+
sandbox_url: Optional[str]
44+
"""URL to connect to sandbox, defaults to `E2B_SANDBOX_URL` environment variable."""
45+
4346

4447
class ConnectionConfig:
4548
"""
4649
Configuration for the connection to the API.
4750
"""
51+
envd_port = 49983
4852

4953
@staticmethod
5054
def _domain():
@@ -62,6 +66,10 @@ def _api_key():
6266
def _api_url():
6367
return os.getenv("E2B_API_URL")
6468

69+
@staticmethod
70+
def _sandbox_url():
71+
return os.getenv("E2B_SANDBOX_URL")
72+
6573
@staticmethod
6674
def _access_token():
6775
return os.getenv("E2B_ACCESS_TOKEN")
@@ -72,6 +80,7 @@ def __init__(
7280
debug: Optional[bool] = None,
7381
api_key: Optional[str] = None,
7482
api_url: Optional[str] = None,
83+
sandbox_url: Optional[str] = None,
7584
access_token: Optional[str] = None,
7685
request_timeout: Optional[float] = None,
7786
headers: Optional[Dict[str, str]] = None,
@@ -106,6 +115,8 @@ def __init__(
106115
or ("http://localhost:3000" if self.debug else f"https://api.{self.domain}")
107116
)
108117

118+
self._sandbox_url = sandbox_url or ConnectionConfig._sandbox_url()
119+
109120
@staticmethod
110121
def _get_request_timeout(
111122
default_timeout: Optional[float],
@@ -121,6 +132,28 @@ def _get_request_timeout(
121132
def get_request_timeout(self, request_timeout: Optional[float] = None):
122133
return self._get_request_timeout(self.request_timeout, request_timeout)
123134

135+
def get_sandbox_url(self, sandbox_id: str, sandbox_domain: str) -> str:
136+
if self._sandbox_url:
137+
return self._sandbox_url
138+
139+
return f"{'http' if self.debug else 'https'}://{self.get_host(sandbox_id, sandbox_domain, self.envd_port)}"
140+
141+
def get_host(self, sandbox_id: str, sandbox_domain: str, port: int) -> str:
142+
"""
143+
Get the host address to connect to the sandbox.
144+
You can then use this address to connect to the sandbox port from outside the sandbox via HTTP or WebSocket.
145+
146+
:param port: Port to connect to
147+
:param sandbox_domain: Domain to connect to
148+
:param sandbox_id: Sandbox to connect to
149+
150+
:return: Host address to connect to
151+
"""
152+
if self.debug:
153+
return f"localhost:{port}"
154+
155+
return f"{port}-{sandbox_id}.{sandbox_domain}"
156+
124157
def get_api_params(
125158
self,
126159
**opts: Unpack[ApiParams],

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SandboxOpts(TypedDict):
1616
sandbox_domain: Optional[str]
1717
envd_version: Version
1818
envd_access_token: Optional[str]
19-
envd_api_url: Optional[str]
19+
sandbox_url: Optional[str]
2020
connection_config: ConnectionConfig
2121

2222

@@ -27,7 +27,6 @@ class SandboxBase:
2727
keepalive_expiry=300,
2828
)
2929

30-
envd_port = 49983
3130
mcp_port = 50005
3231

3332
default_sandbox_timeout = 300
@@ -42,24 +41,15 @@ def __init__(
4241
envd_access_token: Optional[str],
4342
sandbox_domain: Optional[str],
4443
connection_config: ConnectionConfig,
45-
envd_api_url: Optional[str] = None,
4644
):
4745
self.__connection_config = connection_config
4846
self.__sandbox_id = sandbox_id
4947
self.__sandbox_domain = sandbox_domain or self.connection_config.domain
5048
self.__envd_version = envd_version
5149
self.__envd_access_token = envd_access_token
52-
self.__envd_api_url = (
53-
envd_api_url
54-
or SandboxBase._envd_api_url()
55-
or f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(self.envd_port)}"
56-
)
50+
self.__envd_api_url = self.connection_config.get_sandbox_url(self.sandbox_id, self.sandbox_domain)
5751
self.__mcp_token: Optional[str] = None
5852

59-
@staticmethod
60-
def _envd_api_url():
61-
return os.getenv("E2B_ENVD_API_URL")
62-
6353
@property
6454
def _envd_access_token(self) -> Optional[str]:
6555
"""Private property to access the envd token"""
@@ -206,10 +196,7 @@ def get_host(self, port: int) -> str:
206196
207197
:return: Host address to connect to
208198
"""
209-
if self.connection_config.debug:
210-
return f"localhost:{port}"
211-
212-
return f"{port}-{self.sandbox_id}.{self.sandbox_domain}"
199+
return self.connection_config.get_host(self.sandbox_id, self.sandbox_domain, port)
213200

214201
def get_mcp_url(self) -> str:
215202
"""

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(self, **opts: Unpack[SandboxOpts]):
9595
limits=self._limits, proxy=self.connection_config.proxy
9696
)
9797
self._envd_api = httpx.AsyncClient(
98-
base_url=self.envd_api_url,
98+
base_url=self.connection_config.get_sandbox_url(self.sandbox_id, self.sandbox_domain),
9999
transport=self._transport,
100100
headers=self.connection_config.sandbox_headers,
101101
)
@@ -165,7 +165,6 @@ async def create(
165165
secure: bool = True,
166166
allow_internet_access: bool = True,
167167
mcp: Optional[McpServer] = None,
168-
envd_api_url: Optional[str] = None,
169168
**opts: Unpack[ApiParams],
170169
) -> Self:
171170
"""
@@ -199,7 +198,6 @@ async def create(
199198
secure=secure,
200199
allow_internet_access=allow_internet_access,
201200
mcp=mcp,
202-
envd_api_url=envd_api_url,
203201
**opts,
204202
)
205203

@@ -689,7 +687,6 @@ async def _create(
689687
envs: Optional[Dict[str, str]],
690688
secure: bool,
691689
mcp: Optional[McpServer] = None,
692-
envd_api_url: Optional[str] = None,
693690
**opts: Unpack[ApiParams],
694691
) -> Self:
695692
extra_sandbox_headers = {}
@@ -723,8 +720,8 @@ async def _create(
723720
):
724721
extra_sandbox_headers["X-Access-Token"] = envd_access_token
725722

726-
extra_sandbox_headers["X-Sandbox-ID"] = sandbox_id
727-
extra_sandbox_headers["X-Sandbox-Port"] = str(cls.envd_port)
723+
extra_sandbox_headers["E2b-Sandbox-Id"] = sandbox_id
724+
extra_sandbox_headers["E2b-Sandbox-Port"] = str(ConnectionConfig.envd_port)
728725

729726
connection_config = ConnectionConfig(
730727
extra_sandbox_headers=extra_sandbox_headers,
@@ -737,5 +734,4 @@ async def _create(
737734
envd_version=envd_version,
738735
envd_access_token=envd_access_token,
739736
connection_config=connection_config,
740-
envd_api_url=envd_api_url,
741737
)

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def create(
168168
secure: bool = True,
169169
allow_internet_access: bool = True,
170170
mcp: Optional[McpServer] = None,
171-
envd_api_url: Optional[str] = None,
172171
**opts: Unpack[ApiParams],
173172
) -> Self:
174173
"""
@@ -202,7 +201,6 @@ def create(
202201
secure=secure,
203202
allow_internet_access=allow_internet_access,
204203
mcp=mcp,
205-
envd_api_url=envd_api_url,
206204
**opts,
207205
)
208206

@@ -686,7 +684,6 @@ def _create(
686684
secure: bool,
687685
allow_internet_access: bool,
688686
mcp: Optional[McpServer] = None,
689-
envd_api_url: Optional[str] = None,
690687
**opts: Unpack[ApiParams],
691688
) -> Self:
692689
extra_sandbox_headers = {}
@@ -720,8 +717,8 @@ def _create(
720717
):
721718
extra_sandbox_headers["X-Access-Token"] = envd_access_token
722719

723-
extra_sandbox_headers["X-Sandbox-ID"] = sandbox_id
724-
extra_sandbox_headers["X-Sandbox-Port"] = str(cls.envd_port)
720+
extra_sandbox_headers["E2b-Sandbox-Id"] = sandbox_id
721+
extra_sandbox_headers["E2b-Sandbox-Port"] = str(ConnectionConfig.envd_port)
725722

726723
connection_config = ConnectionConfig(
727724
extra_sandbox_headers=extra_sandbox_headers,
@@ -734,5 +731,4 @@ def _create(
734731
envd_version=envd_version,
735732
envd_access_token=envd_access_token,
736733
connection_config=connection_config,
737-
envd_api_url=envd_api_url,
738734
)

packages/python-sdk/e2b_connect/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,10 @@ def _prepare_server_stream_request(
287287
req,
288288
request_timeout=None,
289289
timeout=None,
290-
headers={},
290+
headers=None,
291291
**opts,
292292
):
293+
headers = headers or {}
293294
data = self._codec.encode(req)
294295
flags = EnvelopeFlags(0)
295296

0 commit comments

Comments
 (0)