Skip to content
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

feat(api): add message batch delete endpoint #640

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 19
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-9563716c7b08b8936ba450ad05005d12cf5ca3b9a37fab8126ed372e422d6de6.yml
configured_endpoints: 21
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-fd67aea6883f1ee9e46f31a42d3940f0acb1749e787055bd9b9f278b20fa53ec.yml
4 changes: 4 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Methods:

Types:

- <code><a href="./src/resources/messages/batches.ts">DeletedMessageBatch</a></code>
- <code><a href="./src/resources/messages/batches.ts">MessageBatch</a></code>
- <code><a href="./src/resources/messages/batches.ts">MessageBatchCanceledResult</a></code>
- <code><a href="./src/resources/messages/batches.ts">MessageBatchErroredResult</a></code>
Expand All @@ -81,6 +82,7 @@ Methods:
- <code title="post /v1/messages/batches">client.messages.batches.<a href="./src/resources/messages/batches.ts">create</a>({ ...params }) -> MessageBatch</code>
- <code title="get /v1/messages/batches/{message_batch_id}">client.messages.batches.<a href="./src/resources/messages/batches.ts">retrieve</a>(messageBatchId) -> MessageBatch</code>
- <code title="get /v1/messages/batches">client.messages.batches.<a href="./src/resources/messages/batches.ts">list</a>({ ...params }) -> MessageBatchesPage</code>
- <code title="delete /v1/messages/batches/{message_batch_id}">client.messages.batches.<a href="./src/resources/messages/batches.ts">delete</a>(messageBatchId) -> DeletedMessageBatch</code>
- <code title="post /v1/messages/batches/{message_batch_id}/cancel">client.messages.batches.<a href="./src/resources/messages/batches.ts">cancel</a>(messageBatchId) -> MessageBatch</code>
- <code title="get /v1/messages/batches/{message_batch_id}/results">client.messages.batches.<a href="./src/resources/messages/batches.ts">results</a>(messageBatchId) -> Response</code>

Expand Down Expand Up @@ -172,6 +174,7 @@ Methods:

Types:

- <code><a href="./src/resources/beta/messages/batches.ts">BetaDeletedMessageBatch</a></code>
- <code><a href="./src/resources/beta/messages/batches.ts">BetaMessageBatch</a></code>
- <code><a href="./src/resources/beta/messages/batches.ts">BetaMessageBatchCanceledResult</a></code>
- <code><a href="./src/resources/beta/messages/batches.ts">BetaMessageBatchErroredResult</a></code>
Expand All @@ -186,5 +189,6 @@ Methods:
- <code title="post /v1/messages/batches?beta=true">client.beta.messages.batches.<a href="./src/resources/beta/messages/batches.ts">create</a>({ ...params }) -> BetaMessageBatch</code>
- <code title="get /v1/messages/batches/{message_batch_id}?beta=true">client.beta.messages.batches.<a href="./src/resources/beta/messages/batches.ts">retrieve</a>(messageBatchId, { ...params }) -> BetaMessageBatch</code>
- <code title="get /v1/messages/batches?beta=true">client.beta.messages.batches.<a href="./src/resources/beta/messages/batches.ts">list</a>({ ...params }) -> BetaMessageBatchesPage</code>
- <code title="delete /v1/messages/batches/{message_batch_id}?beta=true">client.beta.messages.batches.<a href="./src/resources/beta/messages/batches.ts">delete</a>(messageBatchId, { ...params }) -> BetaDeletedMessageBatch</code>
- <code title="post /v1/messages/batches/{message_batch_id}/cancel?beta=true">client.beta.messages.batches.<a href="./src/resources/beta/messages/batches.ts">cancel</a>(messageBatchId, { ...params }) -> BetaMessageBatch</code>
- <code title="get /v1/messages/batches/{message_batch_id}/results?beta=true">client.beta.messages.batches.<a href="./src/resources/beta/messages/batches.ts">results</a>(messageBatchId, { ...params }) -> Response</code>
52 changes: 52 additions & 0 deletions src/resources/beta/messages/batches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ export class Batches extends APIResource {
});
}

/**
* This endpoint is idempotent and can be used to poll for Message Batch
* completion. To access the results of a Message Batch, make a request to the
* `results_url` field in the response.
*/
delete(
messageBatchId: string,
params?: BatchDeleteParams,
options?: Core.RequestOptions,
): Core.APIPromise<BetaDeletedMessageBatch>;
delete(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaDeletedMessageBatch>;
delete(
messageBatchId: string,
params: BatchDeleteParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.APIPromise<BetaDeletedMessageBatch> {
if (isRequestOptions(params)) {
return this.delete(messageBatchId, {}, params);
}
const { betas } = params;
return this._client.delete(`/v1/messages/batches/${messageBatchId}?beta=true`, {
...options,
headers: {
'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),
...options?.headers,
},
});
}

/**
* Batches may be canceled any time before processing ends. Once cancellation is
* initiated, the batch enters a `canceling` state, at which time the system may
Expand Down Expand Up @@ -155,6 +184,20 @@ export class Batches extends APIResource {

export class BetaMessageBatchesPage extends Page<BetaMessageBatch> {}

export interface BetaDeletedMessageBatch {
/**
* ID of the Message Batch.
*/
id: string;

/**
* Deleted object type.
*
* For Message Batches, this is always `"message_batch_deleted"`.
*/
type: 'message_batch_deleted';
}

export interface BetaMessageBatch {
/**
* Unique object identifier.
Expand Down Expand Up @@ -628,6 +671,13 @@ export interface BatchListParams extends PageParams {
betas?: Array<BetaAPI.AnthropicBeta>;
}

export interface BatchDeleteParams {
/**
* Optional header to specify the beta version(s) you want to use.
*/
betas?: Array<BetaAPI.AnthropicBeta>;
}

export interface BatchCancelParams {
/**
* Optional header to specify the beta version(s) you want to use.
Expand All @@ -646,6 +696,7 @@ Batches.BetaMessageBatchesPage = BetaMessageBatchesPage;

export declare namespace Batches {
export {
type BetaDeletedMessageBatch as BetaDeletedMessageBatch,
type BetaMessageBatch as BetaMessageBatch,
type BetaMessageBatchCanceledResult as BetaMessageBatchCanceledResult,
type BetaMessageBatchErroredResult as BetaMessageBatchErroredResult,
Expand All @@ -658,6 +709,7 @@ export declare namespace Batches {
type BatchCreateParams as BatchCreateParams,
type BatchRetrieveParams as BatchRetrieveParams,
type BatchListParams as BatchListParams,
type BatchDeleteParams as BatchDeleteParams,
type BatchCancelParams as BatchCancelParams,
type BatchResultsParams as BatchResultsParams,
};
Expand Down
2 changes: 2 additions & 0 deletions src/resources/beta/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export {
BetaMessageBatchesPage,
Batches,
type BetaDeletedMessageBatch,
type BetaMessageBatch,
type BetaMessageBatchCanceledResult,
type BetaMessageBatchErroredResult,
Expand All @@ -14,6 +15,7 @@ export {
type BatchCreateParams,
type BatchRetrieveParams,
type BatchListParams,
type BatchDeleteParams,
type BatchCancelParams,
type BatchResultsParams,
} from './batches';
Expand Down
4 changes: 4 additions & 0 deletions src/resources/beta/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import * as BatchesAPI from './batches';
import {
BatchCancelParams,
BatchCreateParams,
BatchDeleteParams,
BatchListParams,
BatchResultsParams,
BatchRetrieveParams,
Batches,
BetaDeletedMessageBatch,
BetaMessageBatch,
BetaMessageBatchCanceledResult,
BetaMessageBatchErroredResult,
Expand Down Expand Up @@ -1115,6 +1117,7 @@ export declare namespace Messages {

export {
Batches as Batches,
type BetaDeletedMessageBatch as BetaDeletedMessageBatch,
type BetaMessageBatch as BetaMessageBatch,
type BetaMessageBatchCanceledResult as BetaMessageBatchCanceledResult,
type BetaMessageBatchErroredResult as BetaMessageBatchErroredResult,
Expand All @@ -1127,6 +1130,7 @@ export declare namespace Messages {
type BatchCreateParams as BatchCreateParams,
type BatchRetrieveParams as BatchRetrieveParams,
type BatchListParams as BatchListParams,
type BatchDeleteParams as BatchDeleteParams,
type BatchCancelParams as BatchCancelParams,
type BatchResultsParams as BatchResultsParams,
};
Expand Down
24 changes: 24 additions & 0 deletions src/resources/messages/batches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export class Batches extends APIResource {
return this._client.getAPIList('/v1/messages/batches', MessageBatchesPage, { query, ...options });
}

/**
* This endpoint is idempotent and can be used to poll for Message Batch
* completion. To access the results of a Message Batch, make a request to the
* `results_url` field in the response.
*/
delete(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<DeletedMessageBatch> {
return this._client.delete(`/v1/messages/batches/${messageBatchId}`, options);
}

/**
* Batches may be canceled any time before processing ends. Once cancellation is
* initiated, the batch enters a `canceling` state, at which time the system may
Expand Down Expand Up @@ -80,6 +89,20 @@ export class Batches extends APIResource {

export class MessageBatchesPage extends Page<MessageBatch> {}

export interface DeletedMessageBatch {
/**
* ID of the Message Batch.
*/
id: string;

/**
* Deleted object type.
*
* For Message Batches, this is always `"message_batch_deleted"`.
*/
type: 'message_batch_deleted';
}

export interface MessageBatch {
/**
* Unique object identifier.
Expand Down Expand Up @@ -540,6 +563,7 @@ Batches.MessageBatchesPage = MessageBatchesPage;

export declare namespace Batches {
export {
type DeletedMessageBatch as DeletedMessageBatch,
type MessageBatch as MessageBatch,
type MessageBatchCanceledResult as MessageBatchCanceledResult,
type MessageBatchErroredResult as MessageBatchErroredResult,
Expand Down
1 change: 1 addition & 0 deletions src/resources/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export {
MessageBatchesPage,
Batches,
type DeletedMessageBatch,
type MessageBatch,
type MessageBatchCanceledResult,
type MessageBatchErroredResult,
Expand Down
2 changes: 2 additions & 0 deletions src/resources/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BatchCreateParams,
BatchListParams,
Batches,
DeletedMessageBatch,
MessageBatch,
MessageBatchCanceledResult,
MessageBatchErroredResult,
Expand Down Expand Up @@ -1059,6 +1060,7 @@ export declare namespace Messages {

export {
Batches as Batches,
type DeletedMessageBatch as DeletedMessageBatch,
type MessageBatch as MessageBatch,
type MessageBatchCanceledResult as MessageBatchCanceledResult,
type MessageBatchErroredResult as MessageBatchErroredResult,
Expand Down
29 changes: 29 additions & 0 deletions tests/api-resources/beta/messages/batches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,35 @@ describe('resource batches', () => {
).rejects.toThrow(Anthropic.NotFoundError);
});

test('delete', async () => {
const responsePromise = client.beta.messages.batches.delete('message_batch_id');
const rawResponse = await responsePromise.asResponse();
expect(rawResponse).toBeInstanceOf(Response);
const response = await responsePromise;
expect(response).not.toBeInstanceOf(Response);
const dataAndResponse = await responsePromise.withResponse();
expect(dataAndResponse.data).toBe(response);
expect(dataAndResponse.response).toBe(rawResponse);
});

test('delete: request options instead of params are passed correctly', async () => {
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
await expect(
client.beta.messages.batches.delete('message_batch_id', { path: '/_stainless_unknown_path' }),
).rejects.toThrow(Anthropic.NotFoundError);
});

test('delete: request options and params are passed correctly', async () => {
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
await expect(
client.beta.messages.batches.delete(
'message_batch_id',
{ betas: ['string'] },
{ path: '/_stainless_unknown_path' },
),
).rejects.toThrow(Anthropic.NotFoundError);
});

test('cancel', async () => {
const responsePromise = client.beta.messages.batches.cancel('message_batch_id');
const rawResponse = await responsePromise.asResponse();
Expand Down
18 changes: 18 additions & 0 deletions tests/api-resources/messages/batches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ describe('resource batches', () => {
).rejects.toThrow(Anthropic.NotFoundError);
});

test('delete', async () => {
const responsePromise = client.messages.batches.delete('message_batch_id');
const rawResponse = await responsePromise.asResponse();
expect(rawResponse).toBeInstanceOf(Response);
const response = await responsePromise;
expect(response).not.toBeInstanceOf(Response);
const dataAndResponse = await responsePromise.withResponse();
expect(dataAndResponse.data).toBe(response);
expect(dataAndResponse.response).toBe(rawResponse);
});

test('delete: request options instead of params are passed correctly', async () => {
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
await expect(
client.messages.batches.delete('message_batch_id', { path: '/_stainless_unknown_path' }),
).rejects.toThrow(Anthropic.NotFoundError);
});

test('cancel', async () => {
const responsePromise = client.messages.batches.cancel('message_batch_id');
const rawResponse = await responsePromise.asResponse();
Expand Down