Add unified e2b.Client and sandbox List API#3
Merged
Conversation
Introduce a unified Client that resolves Config once and shares a single
HTTP client and control-plane REST client across all lifecycle calls and
every Sandbox it creates, instead of rebuilding the REST client per call.
- client.go: Client{cfg, apiCli, httpCli}, NewClient, Config (deep-copies
Headers/ExtraSandboxHeaders to avoid aliasing the live request maps), and
Create/Connect/Kill methods carrying the lifecycle logic.
- sandbox.go: newSandbox becomes (*Client).newSandbox sharing the Client's
transports; package-level Create/Connect/Kill are kept as deprecated thin
wrappers that delegate to a one-shot Client (back-compat).
- list.go: SandboxListOptions, SandboxPaginator (HasNext/NextItems/NextToken)
and ListAll, following upstream cursor pagination via the x-next-token
response header; package-level List/ListAll convenience wrappers.
- types.go: sandboxInfoFromListed converter for ListedSandbox.
- client_test.go: client reuse, opts.Config ignored, Config deep-copy, List
pagination, ListAll, metadata/state params, error mapping, empty body.
- examples/*: migrate to NewClient(...).Create (clears SA1019, shows new API).
- README.md, doc.go: document the unified client and List.
Build, vet, race tests, and golangci-lint v2 all pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
当前 sandbox 生命周期入口
Create/Connect/Kill每次调用都会重新resolve()配置并重建控制面 REST 客户端*apiclient.Client(Config.HTTPClient为 nil 时还会新建*http.Client)。这不利于资源复用,也缺少一个"持有一次、统一管理所有功能"的客户端对象。此外 SDK 缺少列出 sandbox 的List接口。改动
统一客户端
e2b.Client(client.go)NewClient(cfg)一次性解析 Config,构造一份共享*http.Client与控制面 REST 客户端,供其创建的所有 sandbox 复用。Create/Connect/Kill/List/ListAll;Config()返回深拷贝(map 字段用maps.Clone,避免别名 Client 的活跃请求头、规避并发竞争)。重构
sandbox.gonewSandbox→(*Client).newSandbox,复用 Client 共享传输(envd 客户端仍按 sandbox 维度构造,但共享httpCli)。Create/Connect/Kill保留为弱化包装器(// Deprecated:),委托临时 Client,行为不变,向后兼容。List 分页接口(
list.go)SandboxPaginator(HasNext()/NextItems(ctx)/NextToken())+ 便捷ListAll,对齐上游 Python/JS 的游标分页(读响应头x-next-token)。SandboxListOptions(Metadata / State / Limit / NextToken),metadata 双层编码与上游一致。types.go新增sandboxInfoFromListed转换器。示例与文档
NewClient(...).Create(消除SA1019弃用 lint,示范推荐用法)。README.md/doc.go新增"统一客户端"段落和 scope 中的List。测试
新增
client_test.go(10 个用例):客户端复用同一apiCli/httpCli指针、opts.Config被忽略、Config()深拷贝、List 两页分页、ListAll、metadata/state 参数、错误映射(401/429/500)、空响应、转换器。go build✅ ·go vet✅ ·go test ./...✅ ·go test -race✅ ·golangci-lint run(v2.11.4)0 issues ✅经一轮 Codex review,已修复其指出的
Config()map 别名问题(P2)。🤖 Generated with Claude Code