Skip to content

Commit 442138d

Browse files
committed
feat(api): batch list endpoint (#781)
1 parent 9c31244 commit 442138d

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

Diff for: .stats.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 62
1+
configured_endpoints: 63

Diff for: api.md

+1
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,5 @@ Methods:
378378

379379
- <code title="post /batches">client.batches.<a href="./src/resources/batches.ts">create</a>({ ...params }) -> Batch</code>
380380
- <code title="get /batches/{batch_id}">client.batches.<a href="./src/resources/batches.ts">retrieve</a>(batchId) -> Batch</code>
381+
- <code title="get /batches">client.batches.<a href="./src/resources/batches.ts">list</a>({ ...params }) -> BatchesPage</code>
381382
- <code title="post /batches/{batch_id}/cancel">client.batches.<a href="./src/resources/batches.ts">cancel</a>(batchId) -> Batch</code>

Diff for: src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ export namespace OpenAI {
302302
export import Batch = API.Batch;
303303
export import BatchError = API.BatchError;
304304
export import BatchRequestCounts = API.BatchRequestCounts;
305+
export import BatchesPage = API.BatchesPage;
305306
export import BatchCreateParams = API.BatchCreateParams;
307+
export import BatchListParams = API.BatchListParams;
306308

307309
export import ErrorObject = API.ErrorObject;
308310
export import FunctionDefinition = API.FunctionDefinition;

Diff for: src/resources/batches.ts

+23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import * as Core from 'openai/core';
44
import { APIResource } from 'openai/resource';
5+
import { isRequestOptions } from 'openai/core';
56
import * as BatchesAPI from 'openai/resources/batches';
7+
import { CursorPage, type CursorPageParams } from 'openai/pagination';
68

79
export class Batches extends APIResource {
810
/**
@@ -19,6 +21,21 @@ export class Batches extends APIResource {
1921
return this._client.get(`/batches/${batchId}`, options);
2022
}
2123

24+
/**
25+
* List your organization's batches.
26+
*/
27+
list(query?: BatchListParams, options?: Core.RequestOptions): Core.PagePromise<BatchesPage, Batch>;
28+
list(options?: Core.RequestOptions): Core.PagePromise<BatchesPage, Batch>;
29+
list(
30+
query: BatchListParams | Core.RequestOptions = {},
31+
options?: Core.RequestOptions,
32+
): Core.PagePromise<BatchesPage, Batch> {
33+
if (isRequestOptions(query)) {
34+
return this.list({}, query);
35+
}
36+
return this._client.getAPIList('/batches', BatchesPage, { query, ...options });
37+
}
38+
2239
/**
2340
* Cancels an in-progress batch.
2441
*/
@@ -27,6 +44,8 @@ export class Batches extends APIResource {
2744
}
2845
}
2946

47+
export class BatchesPage extends CursorPage<Batch> {}
48+
3049
export interface Batch {
3150
id: string;
3251

@@ -217,9 +236,13 @@ export interface BatchCreateParams {
217236
metadata?: Record<string, string> | null;
218237
}
219238

239+
export interface BatchListParams extends CursorPageParams {}
240+
220241
export namespace Batches {
221242
export import Batch = BatchesAPI.Batch;
222243
export import BatchError = BatchesAPI.BatchError;
223244
export import BatchRequestCounts = BatchesAPI.BatchRequestCounts;
245+
export import BatchesPage = BatchesAPI.BatchesPage;
224246
export import BatchCreateParams = BatchesAPI.BatchCreateParams;
247+
export import BatchListParams = BatchesAPI.BatchListParams;
225248
}

Diff for: src/resources/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
export * from './chat/index';
44
export * from './shared';
55
export { Audio } from './audio/audio';
6-
export { Batch, BatchError, BatchRequestCounts, BatchCreateParams, Batches } from './batches';
6+
export {
7+
Batch,
8+
BatchError,
9+
BatchRequestCounts,
10+
BatchCreateParams,
11+
BatchListParams,
12+
BatchesPage,
13+
Batches,
14+
} from './batches';
715
export { Beta } from './beta/beta';
816
export {
917
Completion,

Diff for: tests/api-resources/batches.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ describe('resource batches', () => {
5151
);
5252
});
5353

54+
test('list', async () => {
55+
const responsePromise = openai.batches.list();
56+
const rawResponse = await responsePromise.asResponse();
57+
expect(rawResponse).toBeInstanceOf(Response);
58+
const response = await responsePromise;
59+
expect(response).not.toBeInstanceOf(Response);
60+
const dataAndResponse = await responsePromise.withResponse();
61+
expect(dataAndResponse.data).toBe(response);
62+
expect(dataAndResponse.response).toBe(rawResponse);
63+
});
64+
65+
test('list: request options instead of params are passed correctly', async () => {
66+
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
67+
await expect(openai.batches.list({ path: '/_stainless_unknown_path' })).rejects.toThrow(
68+
OpenAI.NotFoundError,
69+
);
70+
});
71+
72+
test('list: request options and params are passed correctly', async () => {
73+
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
74+
await expect(
75+
openai.batches.list({ after: 'string', limit: 0 }, { path: '/_stainless_unknown_path' }),
76+
).rejects.toThrow(OpenAI.NotFoundError);
77+
});
78+
5479
test('cancel', async () => {
5580
const responsePromise = openai.batches.cancel('string');
5681
const rawResponse = await responsePromise.asResponse();

0 commit comments

Comments
 (0)