11import os
22
33from typing import Literal , Optional , Dict
4+ from dataclasses import dataclass
45from httpx ._types import ProxyTypes
6+ from typing_extensions import Unpack
57
68from e2b .api .metadata import package_version
79
1113KEEPALIVE_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+
1443class 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
86159Username = Literal ["root" , "user" ]
87160"""
0 commit comments