Skip to content

Commit c54528f

Browse files
authored
Update User-Agent header in SDKs based on the SDK version (#830)
# Description The bug where extra fields in envd response caused an exception was fixed in #738. As we're also ignoring new fields in envd (implemented in e2b-dev/runtime#847). We now have to update the `user-agent` header.
1 parent 706ebd9 commit c54528f

7 files changed

Lines changed: 23 additions & 4 deletions

File tree

.changeset/plenty-foxes-press.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'e2b': patch
3+
'@e2b/python-sdk': patch
4+
---
5+
6+
Update connect header

packages/js-sdk/src/api/metadata.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import platform from 'platform'
22

33
import { version } from '../../package.json'
44

5+
export { version }
6+
57
declare let window: any
68

79
type Runtime = 'node' | 'browser' | 'deno' | 'bun' | 'vercel-edge' | 'cloudflare-worker' | 'unknown'

packages/js-sdk/src/connectionConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Logger } from './logs'
2-
import { getEnvVar } from './api/metadata'
2+
import {getEnvVar, version} from './api/metadata'
33

44
const REQUEST_TIMEOUT_MS = 30_000 // 30 seconds
55
export const KEEPALIVE_PING_INTERVAL_SEC = 50 // 50 seconds
@@ -74,7 +74,8 @@ export class ConnectionConfig {
7474
this.accessToken = opts?.accessToken || ConnectionConfig.accessToken
7575
this.requestTimeoutMs = opts?.requestTimeoutMs ?? REQUEST_TIMEOUT_MS
7676
this.logger = opts?.logger
77-
this.headers = opts?.headers
77+
this.headers = opts?.headers || {}
78+
this.headers['User-Agent'] = `e2b-js-sdk/${version}`
7879

7980
this.apiUrl = this.debug
8081
? 'http://localhost:3000'

packages/js-sdk/src/sandbox/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ export class Sandbox extends SandboxApi {
167167
// connect-web package uses redirect: "error" which is not supported in edge runtimes
168168
// E2B endpoints should be safe to use with redirect: "follow" https://github.com/e2b-dev/E2B/issues/531#issuecomment-2779492867
169169

170-
const headers = new Headers(options?.headers)
170+
const headers = new Headers(this.connectionConfig.headers)
171+
new Headers(options?.headers).forEach(
172+
(value, key) => headers.append(key, value)
173+
)
174+
171175
if (this.envdAccessToken) {
172176
headers.append('X-Access-Token', this.envdAccessToken)
173177
}

packages/python-sdk/e2b/api/metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from importlib import metadata
44

5+
package_version = metadata.version("e2b")
6+
57
default_headers = {
68
"lang": "python",
79
"lang_version": platform.python_version(),

packages/python-sdk/e2b/connection_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Literal, Optional, Dict
44
from httpx._types import ProxyTypes
55

6+
from e2b.api.metadata import package_version
7+
68
REQUEST_TIMEOUT: float = 30.0 # 30 seconds
79

810
KEEPALIVE_PING_INTERVAL_SEC = 50 # 50 seconds
@@ -45,6 +47,8 @@ def __init__(
4547
self.api_key = api_key or ConnectionConfig._api_key()
4648
self.access_token = access_token or ConnectionConfig._access_token()
4749
self.headers = headers or {}
50+
self.headers["User-Agent"] = f"e2b-python-sdk/{package_version}"
51+
4852
self.proxy = proxy
4953

5054
self.request_timeout = ConnectionConfig._get_request_timeout(

packages/python-sdk/e2b_connect/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def __init__(
161161
self._codec = JSONCodec if json else ProtobufCodec
162162
self._response_type = response_type
163163
self._compressor = compressor
164-
self._headers = {**{"user-agent": "connect-python"}, **headers}
164+
self._headers = headers
165165
self._connection_retries = 3
166166

167167
def _prepare_unary_request(

0 commit comments

Comments
 (0)