diff --git a/.changeset/unlucky-papayas-design.md b/.changeset/unlucky-papayas-design.md new file mode 100644 index 0000000000..a1c1005bf5 --- /dev/null +++ b/.changeset/unlucky-papayas-design.md @@ -0,0 +1,6 @@ +--- +'@e2b/python-sdk': patch +'e2b': patch +--- + +make it possible to pass custom headers in the SDk diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.ts index 2d31fd1081..4d765919c5 100644 --- a/packages/js-sdk/src/api/index.ts +++ b/packages/js-sdk/src/api/index.ts @@ -58,6 +58,7 @@ class ApiClient { ...(config.accessToken && { Authorization: `Bearer ${config.accessToken}`, }), + ...config.headers, }, }) diff --git a/packages/js-sdk/src/connectionConfig.ts b/packages/js-sdk/src/connectionConfig.ts index ed9520399c..a02c27e4fb 100644 --- a/packages/js-sdk/src/connectionConfig.ts +++ b/packages/js-sdk/src/connectionConfig.ts @@ -12,19 +12,19 @@ export const KEEPALIVE_PING_HEADER = 'Keepalive-Ping-Interval' export interface ConnectionOpts { /** * E2B API key to use for authentication. - * + * * @default E2B_API_KEY // environment variable */ apiKey?: string /** * E2B access token to use for authentication. - * + * * @default E2B_ACCESS_TOKEN // environment variable */ accessToken?: string /** * Domain to use for the API. - * + * * @default E2B_DOMAIN // environment variable or `e2b.app` */ domain?: string @@ -36,7 +36,7 @@ export interface ConnectionOpts { debug?: boolean /** * Timeout for requests to the API in **milliseconds**. - * + * * @default 30_000 // 30 seconds */ requestTimeoutMs?: number @@ -44,6 +44,11 @@ export interface ConnectionOpts { * Logger to use for logging messages. It can accept any object that implements `Logger` interface—for example, {@link console}. */ logger?: Logger + + /** + * Additional headers to send with the request. + */ + headers?: Record } /** @@ -60,6 +65,8 @@ export class ConnectionConfig { readonly apiKey?: string readonly accessToken?: string + readonly headers?: Record + constructor(opts?: ConnectionOpts) { this.apiKey = opts?.apiKey || ConnectionConfig.apiKey this.debug = opts?.debug || ConnectionConfig.debug @@ -67,6 +74,7 @@ export class ConnectionConfig { this.accessToken = opts?.accessToken || ConnectionConfig.accessToken this.requestTimeoutMs = opts?.requestTimeoutMs ?? REQUEST_TIMEOUT_MS this.logger = opts?.logger + this.headers = opts?.headers this.apiUrl = this.debug ? 'http://localhost:3000' @@ -101,5 +109,4 @@ export class ConnectionConfig { */ export type Username = 'root' | 'user' - export const defaultUsername: Username = 'user' diff --git a/packages/python-sdk/e2b/api/__init__.py b/packages/python-sdk/e2b/api/__init__.py index 2d5aae547c..49530b1148 100644 --- a/packages/python-sdk/e2b/api/__init__.py +++ b/packages/python-sdk/e2b/api/__init__.py @@ -85,6 +85,11 @@ def __init__( auth_header_name = "X-API-KEY" if require_api_key else "Authorization" prefix = "" if require_api_key else "Bearer" + headers = { + **default_headers, + **(config.headers or {}), + } + super().__init__( base_url=config.api_url, httpx_args={ @@ -94,9 +99,7 @@ def __init__( }, "transport": transport, }, - headers={ - **default_headers, - }, + headers=headers, token=token, auth_header_name=auth_header_name, prefix=prefix, diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py index 6557d84f37..0a2f64816f 100644 --- a/packages/python-sdk/e2b/connection_config.py +++ b/packages/python-sdk/e2b/connection_config.py @@ -1,6 +1,6 @@ import os -from typing import Literal, Optional +from typing import Literal, Optional, Dict REQUEST_TIMEOUT: float = 30.0 # 30 seconds @@ -36,11 +36,13 @@ def __init__( api_key: Optional[str] = None, access_token: Optional[str] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ): self.domain = domain or ConnectionConfig._domain() self.debug = debug or ConnectionConfig._debug() self.api_key = api_key or ConnectionConfig._api_key() self.access_token = access_token or ConnectionConfig._access_token() + self.headers = headers self.request_timeout = ConnectionConfig._get_request_timeout( REQUEST_TIMEOUT, diff --git a/packages/python-sdk/e2b/sandbox_async/sandbox_api.py b/packages/python-sdk/e2b/sandbox_async/sandbox_api.py index 7e7bc29b5e..e3799b71f9 100644 --- a/packages/python-sdk/e2b/sandbox_async/sandbox_api.py +++ b/packages/python-sdk/e2b/sandbox_async/sandbox_api.py @@ -26,6 +26,7 @@ async def list( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> List[SandboxInfo]: """ List all running sandboxes. @@ -35,6 +36,7 @@ async def list( :param domain: Domain to use for the request, only relevant for self-hosted environments :param debug: Enable debug mode, all requested are then sent to localhost :param request_timeout: Timeout for the request in **seconds** + :param headers: Additional headers to send with the request :return: List of running sandboxes """ @@ -43,6 +45,7 @@ async def list( domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) # Convert filters to the format expected by the API @@ -92,6 +95,7 @@ async def get_info( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> SandboxInfo: """ Get the sandbox info. @@ -100,6 +104,8 @@ async def get_info( :param domain: Domain to use for the request, defaults to `E2B_DOMAIN` environment variable :param debug: Debug mode, defaults to `E2B_DEBUG` environment variable :param request_timeout: Timeout for the request in **seconds** + :param headers: Additional headers to send with the request + :return: Sandbox info """ config = ConnectionConfig( @@ -107,6 +113,7 @@ async def get_info( domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) async with AsyncApiClient(config) as api_client: @@ -143,12 +150,14 @@ async def _cls_kill( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> bool: config = ConnectionConfig( api_key=api_key, domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) if config.debug: @@ -178,12 +187,14 @@ async def _cls_set_timeout( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> None: config = ConnectionConfig( api_key=api_key, domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) if config.debug: @@ -211,12 +222,14 @@ async def _create_sandbox( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> SandboxCreateResponse: config = ConnectionConfig( api_key=api_key, domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) async with AsyncApiClient(config) as api_client: diff --git a/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py b/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py index da4c5920c7..484b7bdb9b 100644 --- a/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py +++ b/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py @@ -28,6 +28,7 @@ def list( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> List[SandboxInfo]: """ List all running sandboxes. @@ -37,6 +38,7 @@ def list( :param domain: Domain to use for the request, only relevant for self-hosted environments :param debug: Enable debug mode, all requested are then sent to localhost :param request_timeout: Timeout for the request in **seconds** + :param headers: Additional headers to send with the request :return: List of running sandboxes """ @@ -45,6 +47,7 @@ def list( domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) # Convert filters to the format expected by the API @@ -93,6 +96,7 @@ def get_info( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> SandboxInfo: """ Get the sandbox info. @@ -101,6 +105,8 @@ def get_info( :param domain: Domain to use for the request, defaults to `E2B_DOMAIN` environment variable :param debug: Debug mode, defaults to `E2B_DEBUG` environment variable :param request_timeout: Timeout for the request in **seconds** + :param headers: Additional headers to send with the request + :return: Sandbox info """ config = ConnectionConfig( @@ -108,6 +114,7 @@ def get_info( domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) with ApiClient( @@ -146,12 +153,14 @@ def _cls_kill( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> bool: config = ConnectionConfig( api_key=api_key, domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) if config.debug: @@ -183,12 +192,14 @@ def _cls_set_timeout( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> None: config = ConnectionConfig( api_key=api_key, domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) if config.debug: @@ -218,12 +229,14 @@ def _create_sandbox( domain: Optional[str] = None, debug: Optional[bool] = None, request_timeout: Optional[float] = None, + headers: Optional[Dict[str, str]] = None, ) -> SandboxCreateResponse: config = ConnectionConfig( api_key=api_key, domain=domain, debug=debug, request_timeout=request_timeout, + headers=headers, ) with ApiClient(