Skip to content

Commit 4aa2362

Browse files
authored
Add support for predictions.cancel endpoint (#52)
1 parent a94439e commit 4aa2362

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ declare module 'replicate' {
113113
webhook_events_filter?: WebhookEventType[];
114114
}): Promise<Prediction>;
115115
get(prediction_id: string): Promise<Prediction>;
116+
cancel(prediction_id: string): Promise<Prediction>;
116117
list(): Promise<Page<Prediction>>;
117118
};
118119

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Replicate {
5454
this.predictions = {
5555
create: predictions.create.bind(this),
5656
get: predictions.get.bind(this),
57+
cancel: predictions.cancel.bind(this),
5758
list: predictions.list.bind(this),
5859
};
5960

index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,41 @@ describe('Replicate client', () => {
150150
// Add more tests for error handling, edge cases, etc.
151151
});
152152

153+
describe('predictions.cancel', () => {
154+
test('Calls the correct API route with the correct payload', async () => {
155+
nock(BASE_URL)
156+
.post('/predictions/ufawqhfynnddngldkgtslldrkq/cancel')
157+
.reply(200, {
158+
id: 'ufawqhfynnddngldkgtslldrkq',
159+
version:
160+
'5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa',
161+
urls: {
162+
get: 'https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq',
163+
cancel:
164+
'https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel',
165+
},
166+
created_at: '2022-04-26T22:13:06.224088Z',
167+
started_at: '2022-04-26T22:13:06.224088Z',
168+
completed_at: '2022-04-26T22:14:06.224088Z',
169+
status: 'canceled',
170+
input: {
171+
text: 'Alice',
172+
},
173+
output: null,
174+
error: null,
175+
logs: null,
176+
metrics: {},
177+
});
178+
179+
const prediction = await client.predictions.cancel(
180+
'ufawqhfynnddngldkgtslldrkq'
181+
);
182+
expect(prediction.status).toBe('canceled');
183+
});
184+
185+
// Add more tests for error handling, edge cases, etc.
186+
});
187+
153188
describe('predictions.list', () => {
154189
test('Calls the correct API route with the correct payload', async () => {
155190
nock(BASE_URL)

lib/predictions.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ async function getPrediction(prediction_id) {
3939
});
4040
}
4141

42+
/**
43+
* Cancel a prediction by ID
44+
*
45+
* @param {string} prediction_id - Required. The training ID
46+
* @returns {Promise<object>} Resolves with the data for the training
47+
*/
48+
async function cancelPrediction(prediction_id) {
49+
return this.request(`/predictions/${prediction_id}/cancel`, {
50+
method: 'POST',
51+
});
52+
}
53+
4254
/**
4355
* List all predictions
4456
*
@@ -53,5 +65,6 @@ async function listPredictions() {
5365
module.exports = {
5466
create: createPrediction,
5567
get: getPrediction,
68+
cancel: cancelPrediction,
5669
list: listPredictions,
5770
};

0 commit comments

Comments
 (0)