4a2a6c7051
### Merge Templates SDK into E2B SDK.
This PR merges code from Templates SDK to E2B SDK.
**Changelog**
- Added new error classes: BuildError, FileUploadError.
- Added template API endpoints to API Client generation (Python).
- Updated API Clients error handling to throw BuildError in addition to
SandboxError.
- Exported Template and TemplateAsync classes.
- Moved `getRuntime` utility method from api to utils.
- Browser is supported except file operations (COPY). Incompatible
modules are not bundled, using dynamic imports (tar, glob) in supported
environments (Node, Deno, Bun) instead.
- Basic tests were added, subject to expansion in a follow-up PR.
- Breaking: Before, 401 error was raising `SandboxError`, which was
incorrect, now it will raise `AuthenticationError`. I have also changed
the `AuthenticationError` to extend Error class, not `SandboxError`
anymore as it's now thrown by both Sandbox and Template.
**Usage**
JavaScript
```js
import { Template } from 'e2b';
const template = Template()
.fromImage('ubuntu:22.04')
.copy('folder/*.txt', 'folder', { forceUpload: true })
.setEnvs({
ENV_1: 'value1',
ENV_2: 'value2',
})
.runCmd('cat folder/test.txt')
.setWorkdir('/app')
.setStartCmd('echo "Hello, world!"', Template.waitForTimeout('10s'))
await Template.build(template, {
alias: 'test-template',
cpuCount: 1,
memoryMB: 1024,
onBuildLogs: (logEntry) => console.log(logEntry.toString()),
})
```
Python
```py
from e2b import Template
template = (
Template()
.from_image("ubuntu:22.04")
.copy("folder/*.txt", "folder", force_upload=True)
.set_envs(
{
"ENV_1": "value1",
"ENV_2": "value2",
}
)
.run_cmd("cat folder/test.txt")
.set_workdir("/app")
.set_start_cmd("echo 'Hello, world!'", Template.wait_for_timeout("10s"))
)
Template.build(
template,
alias="test-template",
cpu_count=1,
memory_mb=1024,
on_build_logs=lambda log_entry: print(log_entry),
)
```
Python Async
```py
from e2b import AsyncTemplate
template = (
AsyncTemplate()
.from_image("ubuntu:22.04")
.copy("folder/*.txt", "folder", force_upload=True)
.set_envs(
{
"ENV_1": "value1",
"ENV_2": "value2",
}
)
.run_cmd("cat folder/test.txt")
.set_workdir("/app")
.set_start_cmd("echo 'Hello, world!'", Template.wait_for_timeout("10s"))
)
await AsyncTemplate.build(
template,
alias="test-template",
cpu_count=1,
memory_mb=1024,
on_build_logs=lambda log_entry: print(log_entry),
)
```
---
Merge progress
- [x] JavaScript SDK
- [x] Python SDK
- [x] Tests
- [x] Split files for review
30 lines
782 B
Makefile
30 lines
782 B
Makefile
ROOT_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../..)
|
|
|
|
generate-api:
|
|
python $(ROOT_DIR)/spec/remove_extra_tags.py sandboxes templates
|
|
openapi-python-client generate --output-path $(ROOT_DIR)/packages/python-sdk/e2b/api/api --overwrite --path $(ROOT_DIR)/spec/openapi_generated.yml
|
|
rm -rf e2b/api/client
|
|
mv e2b/api/api/e2b_api_client e2b/api/client
|
|
rm -rf e2b/api/api
|
|
ruff format .
|
|
|
|
generate-envd:
|
|
if [ ! -f "/go/bin/protoc-gen-connect-python" ]; then \
|
|
$(MAKE) -C $(ROOT_DIR)/packages/connect-python build; \
|
|
fi
|
|
|
|
cd $(ROOT_DIR)/spec/envd && pwd && buf generate --template buf-python.gen.yaml
|
|
./scripts/fix-python-pb.sh
|
|
|
|
ruff format .
|
|
|
|
generate: generate-api generate-envd
|
|
|
|
init:
|
|
pip install openapi-python-client
|
|
|
|
lint:
|
|
ruff check .
|
|
|
|
format:
|
|
ruff format .
|