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+ """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+
1438class 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
86154Username = Literal ["root" , "user" ]
87155"""
0 commit comments