Repository navigation
feat(sdk): idempotencyKey option for sandbox create #1978
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
Open
devin-ai-integration
wants to merge
6
commits into
main
from
devin/1791891129-sandbox-create-idempotency
+509
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20cfdbf
feat(sdk): add idempotency key connection option for sandbox create
devin-ai-integration[bot] 6a819e4
test(python): cast invalid idempotency key test value
devin-ai-integration[bot] 95602ac
feat(sdk): validate idempotency key against the API pattern
devin-ai-integration[bot] a86bf49
fix(sdk): scope idempotency keys to sandbox creation
devin-ai-integration[bot] e3373a6
fix(sdk): scope idempotency keys to sandbox creation
devin-ai-integration[bot] 2bd4e77
fix(sdk): remove client-bound idempotency guards
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| 'e2b': minor | ||
| '@e2b/python-sdk': minor | ||
| --- | ||
|
|
||
| - Added an `idempotencyKey` (JavaScript) and `idempotency_key` (Python) option | ||
| to `Sandbox.create` that sends `Idempotency-Key` on sandbox creation. Keys | ||
| must contain 1–255 lowercase ASCII letters, digits or hyphens. |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { afterAll, afterEach, beforeAll, expect, test } from 'vitest' | ||
| import { http, HttpResponse } from 'msw' | ||
|
|
||
| import { E2B, Sandbox } from '../../src' | ||
| import { ConnectionConfig } from '../../src/connectionConfig' | ||
| import { InvalidArgumentError } from '../../src/errors' | ||
| import { TEST_API_KEY, apiUrl } from '../setup' | ||
| import { setupMockApi } from '../mockApi' | ||
|
|
||
| const idempotencyKeys: (string | null)[] = [] | ||
| const listIdempotencyKeys: (string | null)[] = [] | ||
|
|
||
| const server = setupMockApi( | ||
| http.post(apiUrl('/v2/sandboxes'), ({ request }) => { | ||
| idempotencyKeys.push(request.headers.get('Idempotency-Key')) | ||
| return HttpResponse.json({ | ||
| sandboxID: 'test-sandbox-id', | ||
| templateID: 'base', | ||
| envdVersion: '0.2.4', | ||
| }) | ||
| }), | ||
| http.get(apiUrl('/v2/sandboxes'), ({ request }) => { | ||
| listIdempotencyKeys.push(request.headers.get('Idempotency-Key')) | ||
| return HttpResponse.json([]) | ||
| }) | ||
| ) | ||
|
|
||
| beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) | ||
|
|
||
| afterAll(() => server.close()) | ||
|
|
||
| afterEach(() => { | ||
| idempotencyKeys.length = 0 | ||
| listIdempotencyKeys.length = 0 | ||
| server.resetHandlers() | ||
| }) | ||
|
|
||
| test('Sandbox.create sends the idempotency key', async () => { | ||
| await Sandbox.create('base', { | ||
| apiKey: TEST_API_KEY, | ||
| idempotencyKey: 'abc123', | ||
| }) | ||
|
|
||
| expect(idempotencyKeys).toEqual(['abc123']) | ||
| }) | ||
|
|
||
| test('Sandbox.create does not send an idempotency key when unset', async () => { | ||
| await Sandbox.create('base', { apiKey: TEST_API_KEY }) | ||
|
|
||
| expect(idempotencyKeys).toEqual([null]) | ||
| }) | ||
|
|
||
| test('Sandbox.create sends a 255-character idempotency key', async () => { | ||
| const key = 'a'.repeat(255) | ||
|
|
||
| await Sandbox.create('base', { apiKey: TEST_API_KEY, idempotencyKey: key }) | ||
|
|
||
| expect(idempotencyKeys).toEqual([key]) | ||
| }) | ||
|
|
||
| test('Sandbox.create sends a lowercase UUID idempotency key', async () => { | ||
| const key = '123e4567-e89b-12d3-a456-426614174000' | ||
|
|
||
| await Sandbox.create('base', { apiKey: TEST_API_KEY, idempotencyKey: key }) | ||
|
|
||
| expect(idempotencyKeys).toEqual([key]) | ||
| }) | ||
|
|
||
| test('Sandbox.create rejects idempotency keys outside 1-255 characters before a request', async () => { | ||
| const emptyKey = Sandbox.create('base', { | ||
| apiKey: TEST_API_KEY, | ||
| idempotencyKey: '', | ||
| }) | ||
| await expect(emptyKey).rejects.toThrow(InvalidArgumentError) | ||
| await expect(emptyKey).rejects.toThrow( | ||
| 'Invalid idempotencyKey: expected 1-255 characters, got 0.' | ||
| ) | ||
|
|
||
| const longKey = Sandbox.create('base', { | ||
| apiKey: TEST_API_KEY, | ||
| idempotencyKey: 'a'.repeat(256), | ||
| }) | ||
| await expect(longKey).rejects.toThrow(InvalidArgumentError) | ||
| await expect(longKey).rejects.toThrow( | ||
| 'Invalid idempotencyKey: expected 1-255 characters, got 256.' | ||
| ) | ||
|
|
||
| expect(idempotencyKeys).toEqual([]) | ||
| }) | ||
|
|
||
| test.each(['ABC', 'abc_1', ' abc ', '界'])( | ||
| 'Sandbox.create rejects invalid idempotency key characters before a request: %s', | ||
| async (key) => { | ||
| await expect( | ||
| Sandbox.create('base', { apiKey: TEST_API_KEY, idempotencyKey: key }) | ||
| ).rejects.toThrow( | ||
| 'Invalid idempotencyKey: only lowercase letters, digits and hyphens are allowed.' | ||
| ) | ||
|
|
||
| expect(idempotencyKeys).toEqual([]) | ||
| } | ||
| ) | ||
|
|
||
| test('ConnectionConfig ignores an untyped idempotency key', () => { | ||
| const config = new ConnectionConfig({ idempotencyKey: 'x' } as any) | ||
|
|
||
| expect(config.headers).not.toHaveProperty('Idempotency-Key') | ||
| }) | ||
|
|
||
| test('Sandbox.list ignores an untyped idempotency key', async () => { | ||
| await Sandbox.list({ | ||
| apiKey: TEST_API_KEY, | ||
| idempotencyKey: 'x', | ||
| } as any).nextItems() | ||
|
|
||
| expect(listIdempotencyKeys).toEqual([null]) | ||
| }) | ||
|
|
||
| test('client.Sandbox.create accepts a per-call idempotency key', async () => { | ||
| const client = new E2B({ apiKey: TEST_API_KEY }) | ||
|
|
||
| await client.Sandbox.create('base', { idempotencyKey: 'abc123' }) | ||
| await client.Sandbox.create('base') | ||
|
|
||
| expect(idempotencyKeys).toEqual(['abc123', null]) | ||
| }) |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.