11import os
22
3- from typing import Literal , Optional , Dict
3+ from typing import Literal , Optional , Dict , TypedDict
44from httpx ._types import ProxyTypes
5+ from typing_extensions import Unpack
56
67from e2b .api .metadata import package_version
78
1112KEEPALIVE_PING_HEADER = "Keepalive-Ping-Interval"
1213
1314
15+ class ApiParams (TypedDict , total = False ):
16+ """
17+ Parameters for a request.
18+
19+ In the case of a sandbox, it applies to all **requests made to the returned sandbox**.
20+ """
21+
22+ request_timeout : Optional [float ]
23+ """Timeout for the request in **seconds**, defaults to 60 seconds."""
24+
25+ headers : Optional [Dict [str , str ]]
26+ """Additional headers to send with the request."""
27+
28+ api_key : Optional [str ]
29+ """E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable."""
30+
31+ domain : Optional [str ]
32+ """E2B domain to use for authentication, defaults to `E2B_DOMAIN` environment variable."""
33+
34+ debug : Optional [bool ]
35+ """Whether to use debug mode, defaults to `E2B_DEBUG` environment variable."""
36+
37+ proxy : Optional [ProxyTypes ]
38+ """Proxy to use for the request. In case of a sandbox it applies to all **requests made to the returned sandbox**."""
39+
40+
1441class ConnectionConfig :
1542 """
1643 Configuration for the connection to the API.
@@ -40,6 +67,7 @@ def __init__(
4067 access_token : Optional [str ] = None ,
4168 request_timeout : Optional [float ] = None ,
4269 headers : Optional [Dict [str , str ]] = None ,
70+ extra_sandbox_headers : Optional [Dict [str , str ]] = None ,
4371 proxy : Optional [ProxyTypes ] = None ,
4472 ):
4573 self .domain = domain or ConnectionConfig ._domain ()
@@ -48,6 +76,7 @@ def __init__(
4876 self .access_token = access_token or ConnectionConfig ._access_token ()
4977 self .headers = headers or {}
5078 self .headers ["User-Agent" ] = f"e2b-python-sdk/{ package_version } "
79+ self .__extra_sandbox_headers = extra_sandbox_headers or {}
5180
5281 self .proxy = proxy
5382
@@ -82,6 +111,53 @@ def _get_request_timeout(
82111 def get_request_timeout (self , request_timeout : Optional [float ] = None ):
83112 return self ._get_request_timeout (self .request_timeout , request_timeout )
84113
114+ def get_api_params (
115+ self ,
116+ ** opts : Unpack [ApiParams ],
117+ ) -> dict :
118+ """
119+ Get the parameters for the API call.
120+
121+ This is used to avoid passing the following attributes to the API call:
122+ - access_token
123+ - api_url
124+
125+ It also returns a copy, so the original object is not modified.
126+
127+ :return: Dictionary of parameters for the API call
128+ """
129+ headers = opts .get ("headers" )
130+ request_timeout = opts .get ("request_timeout" )
131+ api_key = opts .get ("api_key" )
132+ domain = opts .get ("domain" )
133+ debug = opts .get ("debug" )
134+ proxy = opts .get ("proxy" )
135+
136+ req_headers = self .headers .copy ()
137+ if headers is not None :
138+ req_headers .update (headers )
139+
140+ return dict (
141+ ApiParams (
142+ api_key = api_key if api_key is not None else self .api_key ,
143+ domain = domain if domain is not None else self .domain ,
144+ debug = debug if debug is not None else self .debug ,
145+ request_timeout = self .get_request_timeout (request_timeout ),
146+ headers = req_headers ,
147+ proxy = proxy if proxy is not None else self .proxy ,
148+ )
149+ )
150+
151+ @property
152+ def sandbox_headers (self ):
153+ """
154+ # We need this separate as we use the same header for E2B access token to API and envd access token to sandbox.
155+ """
156+ return {
157+ ** self .headers ,
158+ ** self .__extra_sandbox_headers ,
159+ }
160+
85161
86162Username = Literal ["root" , "user" ]
87163"""
0 commit comments