Skip to content

Commit a297a30

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

14 files changed

Lines changed: 331 additions & 605 deletions

File tree

packages/python-sdk/e2b/connection_config.py

Lines changed: 68 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,28 @@
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+
"""Timeout for the request in **seconds**, defaults to 60 seconds."""
25+
request_timeout: Optional[float]
26+
"""Additional headers to send with the request."""
27+
headers: Dict[str, str]
28+
"""E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable"""
29+
api_key: Optional[str]
30+
"""E2B domain to use for authentication, defaults to `E2B_DOMAIN` environment variable"""
31+
domain: str
32+
"""Whether to use debug mode, defaults to `E2B_DEBUG` environment variable"""
33+
debug: bool
34+
"""Proxy to use for the request. In case of a sandbox it applies to all **requests made to the returned sandbox**"""
35+
proxy: Optional[ProxyTypes]
36+
37+
1438
class ConnectionConfig:
1539
"""
1640
Configuration for the connection to the API.
@@ -40,6 +64,7 @@ def __init__(
4064
access_token: Optional[str] = None,
4165
request_timeout: Optional[float] = None,
4266
headers: Optional[Dict[str, str]] = None,
67+
extra_sandbox_headers: Optional[Dict[str, str]] = None,
4368
proxy: Optional[ProxyTypes] = None,
4469
):
4570
self.domain = domain or ConnectionConfig._domain()
@@ -48,6 +73,7 @@ def __init__(
4873
self.access_token = access_token or ConnectionConfig._access_token()
4974
self.headers = headers or {}
5075
self.headers["User-Agent"] = f"e2b-python-sdk/{package_version}"
76+
self.__extra_sandbox_headers = extra_sandbox_headers or {}
5177

5278
self.proxy = proxy
5379

@@ -82,6 +108,48 @@ def _get_request_timeout(
82108
def get_request_timeout(self, request_timeout: Optional[float] = None):
83109
return self._get_request_timeout(self.request_timeout, request_timeout)
84110

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

86154
Username = Literal["root", "user"]
87155
"""

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)