Skip to content

Commit 876a7c1

Browse files
committed
Simplify connection config
1 parent 0bace98 commit 876a7c1

14 files changed

Lines changed: 416 additions & 321 deletions

File tree

packages/python-sdk/e2b/connection_config.py

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

33
from typing import Literal, Optional, Dict
4+
from dataclasses import dataclass
45
from httpx._types import ProxyTypes
56

67
from e2b.api.metadata import package_version
@@ -11,6 +12,20 @@
1112
KEEPALIVE_PING_HEADER = "Keepalive-Ping-Interval"
1213

1314

15+
@dataclass
16+
class ApiParams:
17+
"""
18+
Parameters for API calls that avoid passing access_token and api_url.
19+
"""
20+
21+
api_key: Optional[str]
22+
domain: str
23+
debug: bool
24+
request_timeout: Optional[float]
25+
headers: Dict[str, str]
26+
proxy: Optional[ProxyTypes]
27+
28+
1429
class ConnectionConfig:
1530
"""
1631
Configuration for the connection to the API.
@@ -40,6 +55,7 @@ def __init__(
4055
access_token: Optional[str] = None,
4156
request_timeout: Optional[float] = None,
4257
headers: Optional[Dict[str, str]] = None,
58+
extra_sandbox_headers: Optional[Dict[str, str]] = None,
4359
proxy: Optional[ProxyTypes] = None,
4460
):
4561
self.domain = domain or ConnectionConfig._domain()
@@ -48,6 +64,7 @@ def __init__(
4864
self.access_token = access_token or ConnectionConfig._access_token()
4965
self.headers = headers or {}
5066
self.headers["User-Agent"] = f"e2b-python-sdk/{package_version}"
67+
self.__extra_sandbox_headers = extra_sandbox_headers or {}
5168

5269
self.proxy = proxy
5370

@@ -82,6 +99,44 @@ def _get_request_timeout(
8299
def get_request_timeout(self, request_timeout: Optional[float] = None):
83100
return self._get_request_timeout(self.request_timeout, request_timeout)
84101

102+
def get_api_params(
103+
self,
104+
request_timeout: Optional[float],
105+
headers: Optional[Dict[str, str]],
106+
) -> dict:
107+
"""
108+
Get the parameters for the API call.
109+
110+
This is used to avoid passing following attributes to the API call:
111+
- access_token
112+
- api_url
113+
114+
It also returns a copy so the original object is not modified.
115+
116+
:param request_timeout: Request timeout in seconds
117+
:param headers: Additional headers to add to the request
118+
:return: Dictionary of parameters for the API call
119+
"""
120+
req_headers = self.headers.copy()
121+
if headers is not None:
122+
req_headers.update(headers)
123+
124+
return ApiParams(
125+
api_key=self.api_key,
126+
domain=self.domain,
127+
debug=self.debug,
128+
request_timeout=self.get_request_timeout(request_timeout),
129+
headers=req_headers,
130+
proxy=self.proxy,
131+
).__dict__
132+
133+
@property
134+
def sandbox_headers(self):
135+
return {
136+
**self.headers,
137+
**self.__extra_sandbox_headers,
138+
}
139+
85140

86141
Username = Literal["root", "user"]
87142
"""

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)