Skip to content

Commit

Permalink
feat: add support for canceling requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Feb 20, 2024
1 parent 11b5343 commit 6a2bf6e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ const details: Request = await api.requestDetails('test-id');
const details: unknown = await api.requestDetails('test-id', false);
```

### Cancel a Test Request

documentation of - [`DELETE /requests/{request_id}`](https://api.dev.testing-farm.io/redoc#operation/delete_test_request_v0_1_requests__request_id__delete)

```typescript
const response: CancelRequestResponse = await cancelRequest('test-id');
const response: unknown = await cancelRequest('test-id', false);
```

### Composes Public Ranch

documentation of - [`GET /composes`](https://api.dev.testing-farm.io/redoc#operation/supported_composes_v0_1_composes_get)
Expand Down
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Ranch,
Request,
ErrorResponse,
CancelRequestResponse,
cancelRequestResponseSchema,
} from './schema';
import {
composesSchema,
Expand Down Expand Up @@ -75,6 +77,20 @@ export default class TestingFarmAPI {
return requestSchema.parse(await this.link.get(`requests/${id}`));
}

async cancelRequest(requestId: string): Promise<CancelRequestResponse>;
async cancelRequest(requestId: string, strict: boolean): Promise<unknown>;
async cancelRequest(requestId: string, strict?: boolean): Promise<unknown> {
const id = requestIdSchema.parse(requestId);

if (!this.isStrict(strict)) {
return this.link.delete(`requests/${id}`);
}

Check warning on line 87 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L86-L87

Added lines #L86 - L87 were not covered by tests

return cancelRequestResponseSchema.parse(
await this.link.delete(`requests/${id}`)
);

Check warning on line 91 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L91

Added line #L91 was not covered by tests
}

async composes(): Promise<Composes>;
async composes(strict: boolean): Promise<unknown>;
async composes(strict?: boolean): Promise<unknown> {
Expand Down
10 changes: 10 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ export const requestSchema = z.object({

export type Request = z.infer<typeof requestSchema>;

// TODO: make
export const cancelRequestResponseSchema = z.object({
id: requestIdSchema,
state: z.string(),
created: z.string(),
updated: z.string(),
});

export type CancelRequestResponse = z.infer<typeof cancelRequestResponseSchema>;

export const ranchSchema = z.union([z.literal('public'), z.literal('redhat')]);

export type Ranch = z.infer<typeof ranchSchema>;
Expand Down
34 changes: 34 additions & 0 deletions test/integration/cancel-request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect, test, describe } from 'vitest';

import TestingFarmAPI from '../../src/index';

describe('Test Testing Farm DELETE /requests/{request_id}', () => {
test.todo('safe response', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.1');

const response = await api.cancelRequest(
'f053796b-452e-4da2-b4e1-26eb2f3e721f'
);
expect(response).toMatchInlineSnapshot();
});

test.todo('unsafe response', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.1');

const response = await api.cancelRequest(
'f053796b-452e-4da2-b4e1-26eb2f3e721f',
false
);
expect(response).toBeTypeOf('object');
});

test('non-existent request_id', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.1');

await expect(
api.cancelRequest('request_id')
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"detail":[{"loc":["body"],"msg":"field required","type":"value_error.missing"}]}]`
);
});
});

0 comments on commit 6a2bf6e

Please sign in to comment.