Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions python/e2b_code_interpreter/code_interpreter_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,23 @@ class AsyncSandbox(BaseAsyncSandbox):

@property
def _jupyter_url(self) -> str:
# When E2B_SANDBOX_URL is set (local/self-hosted environment), route through the client-proxy
sandbox_url = self.connection_config._sandbox_url
if sandbox_url:
return sandbox_url
return f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(JUPYTER_PORT)}"

@property
def _jupyter_headers(self) -> Dict[str, str]:
"""Extra headers for local client-proxy routing (E2b-Sandbox-Id / E2b-Sandbox-Port)."""
sandbox_url = self.connection_config._sandbox_url
if sandbox_url:
return {
"E2b-Sandbox-Id": self.sandbox_id,
"E2b-Sandbox-Port": str(JUPYTER_PORT),
}
return {}

@property
def _client(self) -> AsyncClient:
return AsyncClient(transport=self._transport)
Expand Down Expand Up @@ -194,6 +209,7 @@ async def run_code(
headers = {
"Content-Type": "application/json",
}
headers.update(self._jupyter_headers)
if self._envd_access_token:
headers["X-Access-Token"] = self._envd_access_token
if self.traffic_access_token:
Expand Down Expand Up @@ -260,6 +276,7 @@ async def create_code_context(
headers = {
"Content-Type": "application/json",
}
headers.update(self._jupyter_headers)
if self.traffic_access_token:
headers["E2B-Traffic-Access-Token"] = self.traffic_access_token

Expand Down Expand Up @@ -296,6 +313,7 @@ async def remove_code_context(
headers = {
"Content-Type": "application/json",
}
headers.update(self._jupyter_headers)
if self.traffic_access_token:
headers["E2B-Traffic-Access-Token"] = self.traffic_access_token

Expand All @@ -321,6 +339,7 @@ async def list_code_contexts(self) -> List[Context]:
headers = {
"Content-Type": "application/json",
}
headers.update(self._jupyter_headers)
if self.traffic_access_token:
headers["E2B-Traffic-Access-Token"] = self.traffic_access_token

Expand Down Expand Up @@ -355,6 +374,7 @@ async def restart_code_context(
headers = {
"Content-Type": "application/json",
}
headers.update(self._jupyter_headers)
if self.traffic_access_token:
headers["E2B-Traffic-Access-Token"] = self.traffic_access_token

Expand Down
20 changes: 20 additions & 0 deletions python/e2b_code_interpreter/code_interpreter_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@ class Sandbox(BaseSandbox):

@property
def _jupyter_url(self) -> str:
# When E2B_SANDBOX_URL is set (local environment), route through the client-proxy
sandbox_url = self.connection_config._sandbox_url
if sandbox_url:
return sandbox_url
return f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(JUPYTER_PORT)}"

@property
def _jupyter_headers(self) -> Dict[str, str]:
"""Extra headers for local client-proxy routing (E2b-Sandbox-Id / E2b-Sandbox-Port)."""
sandbox_url = self.connection_config._sandbox_url
if sandbox_url:
return {
"E2b-Sandbox-Id": self.sandbox_id,
"E2b-Sandbox-Port": str(JUPYTER_PORT),
}
return {}

@property
def _client(self) -> Client:
return Client(transport=self._transport)
Expand Down Expand Up @@ -190,6 +205,7 @@ def run_code(

try:
headers: Dict[str, str] = {"Content-Type": "application/json"}
headers.update(self._jupyter_headers)
if self._envd_access_token:
headers["X-Access-Token"] = self._envd_access_token
if self.traffic_access_token:
Expand Down Expand Up @@ -254,6 +270,7 @@ def create_code_context(

try:
headers: Dict[str, str] = {"Content-Type": "application/json"}
headers.update(self._jupyter_headers)
if self._envd_access_token:
headers["X-Access-Token"] = self._envd_access_token
if self.traffic_access_token:
Expand Down Expand Up @@ -290,6 +307,7 @@ def remove_code_context(

try:
headers: Dict[str, str] = {"Content-Type": "application/json"}
headers.update(self._jupyter_headers)
if self._envd_access_token:
headers["X-Access-Token"] = self._envd_access_token
if self.traffic_access_token:
Expand All @@ -315,6 +333,7 @@ def list_code_contexts(self) -> List[Context]:
"""
try:
headers: Dict[str, str] = {"Content-Type": "application/json"}
headers.update(self._jupyter_headers)
if self._envd_access_token:
headers["X-Access-Token"] = self._envd_access_token
if self.traffic_access_token:
Expand Down Expand Up @@ -350,6 +369,7 @@ def restart_code_context(

try:
headers: Dict[str, str] = {"Content-Type": "application/json"}
headers.update(self._jupyter_headers)
if self._envd_access_token:
headers["X-Access-Token"] = self._envd_access_token
if self.traffic_access_token:
Expand Down