11import os
22
33from typing import Literal , Optional , Dict
4+ from dataclasses import dataclass
45from httpx ._types import ProxyTypes
56
67from e2b .api .metadata import package_version
1112KEEPALIVE_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+
1429class 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
86141Username = Literal ["root" , "user" ]
87142"""
0 commit comments