Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/unlucky-papayas-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@e2b/python-sdk': patch
'e2b': patch
---

make it possible to pass custom headers in the SDk
1 change: 1 addition & 0 deletions packages/js-sdk/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ApiClient {
...(config.accessToken && {
Authorization: `Bearer ${config.accessToken}`,
}),
...config.headers,
},
})

Expand Down
17 changes: 12 additions & 5 deletions packages/js-sdk/src/connectionConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,14 +36,19 @@ export interface ConnectionOpts {
debug?: boolean
/**
* Timeout for requests to the API in **milliseconds**.
*
*
* @default 30_000 // 30 seconds
*/
requestTimeoutMs?: number
/**
* 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<string, string>
}

/**
Expand All @@ -60,13 +65,16 @@ export class ConnectionConfig {
readonly apiKey?: string
readonly accessToken?: string

readonly headers?: Record<string, string>

constructor(opts?: ConnectionOpts) {
this.apiKey = opts?.apiKey || ConnectionConfig.apiKey
this.debug = opts?.debug || ConnectionConfig.debug
this.domain = opts?.domain || ConnectionConfig.domain
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'
Expand Down Expand Up @@ -101,5 +109,4 @@ export class ConnectionConfig {
*/
export type Username = 'root' | 'user'


export const defaultUsername: Username = 'user'
9 changes: 6 additions & 3 deletions packages/python-sdk/e2b/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -94,9 +99,7 @@ def __init__(
},
"transport": transport,
},
headers={
**default_headers,
},
headers=headers,
token=token,
auth_header_name=auth_header_name,
prefix=prefix,
Expand Down
4 changes: 3 additions & 1 deletion packages/python-sdk/e2b/connection_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from typing import Literal, Optional
from typing import Literal, Optional, Dict

REQUEST_TIMEOUT: float = 30.0 # 30 seconds

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions packages/python-sdk/e2b/sandbox_async/sandbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
"""
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -100,13 +104,16 @@ 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(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
headers=headers,
)

async with AsyncApiClient(config) as api_client:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
"""
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -101,13 +105,16 @@ 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(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
headers=headers,
)

with ApiClient(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down