-
Notifications
You must be signed in to change notification settings - Fork 860
Add getInfo/get_info methods to the SDKs #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
709f17e
3d439eb
27016be
1338e5c
b5d99e1
654ac8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@e2b/python-sdk': patch | ||
| 'e2b': patch | ||
| --- | ||
|
|
||
| added getInfo methods |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ export interface SandboxListOpts extends SandboxApiOpts { | |
| /** | ||
| * Filter the list of sandboxes, e.g. by metadata `metadata:{"key": "value"}`, if there are multiple filters they are combined with AND. | ||
| */ | ||
| query?: {metadata?: Record<string, string>} | ||
| query?: { metadata?: Record<string, string> } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -46,6 +46,11 @@ export interface SandboxInfo { | |
| * Sandbox start time. | ||
| */ | ||
| startedAt: Date | ||
|
|
||
| /** | ||
| * Sandbox expiration date. | ||
| */ | ||
| endAt: Date | ||
| } | ||
|
|
||
| export class SandboxApi { | ||
|
|
@@ -94,23 +99,27 @@ export class SandboxApi { | |
| * | ||
| * @returns list of running sandboxes. | ||
| */ | ||
| static async list( | ||
| opts?: SandboxListOpts): Promise<SandboxInfo[]> { | ||
| static async list(opts?: SandboxListOpts): Promise<SandboxInfo[]> { | ||
| const config = new ConnectionConfig(opts) | ||
| const client = new ApiClient(config) | ||
|
|
||
| let metadata = undefined | ||
| if (opts?.query) { | ||
| if (opts.query.metadata) { | ||
| const encodedPairs: Record<string, string> = Object.fromEntries(Object.entries(opts.query.metadata).map(([key, value]) => [encodeURIComponent(key), encodeURIComponent(value)])) | ||
| const encodedPairs: Record<string, string> = Object.fromEntries( | ||
| Object.entries(opts.query.metadata).map(([key, value]) => [ | ||
| encodeURIComponent(key), | ||
| encodeURIComponent(value), | ||
| ]) | ||
| ) | ||
| metadata = new URLSearchParams(encodedPairs).toString() | ||
| } | ||
| } | ||
|
|
||
| const res = await client.api.GET('/sandboxes', { | ||
| params: { | ||
| query: {metadata}, | ||
| }, | ||
| params: { | ||
| query: { metadata }, | ||
| }, | ||
| signal: config.getSignal(opts?.requestTimeoutMs), | ||
| }) | ||
|
|
||
|
|
@@ -129,10 +138,57 @@ export class SandboxApi { | |
| ...(sandbox.alias && { name: sandbox.alias }), | ||
| metadata: sandbox.metadata ?? {}, | ||
| startedAt: new Date(sandbox.startedAt), | ||
| endAt: new Date(sandbox.endAt), | ||
| })) ?? [] | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Get sandbox information like sandbox id, template, metadata, started at/end at date. | ||
| * | ||
| * @param sandboxId sandbox ID. | ||
| * @param opts connection options. | ||
| * | ||
| * @returns sandbox information. | ||
| */ | ||
| static async getInfo( | ||
| sandboxId: string, | ||
| opts?: SandboxApiOpts | ||
| ): Promise<SandboxInfo> { | ||
| const config = new ConnectionConfig(opts) | ||
| const client = new ApiClient(config) | ||
|
|
||
| const res = await client.api.GET('/sandboxes/{sandboxID}', { | ||
| params: { | ||
| path: { | ||
| sandboxID: sandboxId, | ||
| }, | ||
| }, | ||
| signal: config.getSignal(opts?.requestTimeoutMs), | ||
| }) | ||
|
|
||
| const err = handleApiError(res) | ||
| if (err) { | ||
| throw err | ||
| } | ||
|
|
||
| if (!res.data) { | ||
| throw new Error('Sandbox not found') | ||
| } | ||
|
|
||
| return { | ||
| sandboxId: this.getSandboxId({ | ||
| sandboxId: res.data.sandboxID, | ||
| clientId: res.data.clientID, | ||
| }), | ||
| templateId: res.data.templateID, | ||
| ...(res.data.alias && { name: res.data.alias }), | ||
| metadata: res.data.metadata ?? {}, | ||
| startedAt: new Date(res.data.startedAt), | ||
| endAt: new Date(res.data.endAt), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the sandbox is still running? It it the expected end time then? It might be good to explain in then in the documentation as it isn't clear imo
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's returned from the API already, should be the timestamp when the sandbox is expected to expire |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set the timeout of the specified sandbox. | ||
| * After the timeout expires the sandbox will be automatically killed. | ||
|
|
@@ -219,7 +275,7 @@ export class SandboxApi { | |
| sandboxId: res.data!.sandboxID, | ||
| clientId: res.data!.clientID, | ||
| }), | ||
| envdVersion: res.data!.envdVersion | ||
| envdVersion: res.data!.envdVersion, | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,5 +19,10 @@ sandboxTest.skipIf(isDebug)('shorten then lenghten timeout', async ({ sandbox }) | |
|
|
||
| await wait(6000) | ||
|
|
||
| await sandbox.isRunning() | ||
| expect(await sandbox.isRunning()).toBeTruthy() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before, I believe the test would pass even if the promise resolved with |
||
| }) | ||
|
|
||
| sandboxTest.skipIf(isDebug)('get sandbox timeout', async ({ sandbox }) => { | ||
| const { endAt } = await sandbox.getInfo() | ||
| expect(endAt).toBeInstanceOf(Date) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.