Mirror of https://github.com/roostorg/osprey
github.com/roostorg/osprey
1import HTTPUtils, { HTTPResponse } from '../utils/HTTPUtils';
2import { BulkJobTask } from '../types/BulkJobTypes';
3import { BulkActionJob, StartBulkActionRequest, StartBulkActionResponse } from '../types/BulkActionTypes';
4
5export async function getBulkJob(taskId: string | undefined): Promise<BulkJobTask[]> {
6 // null on page load
7 if (taskId == null) {
8 return [];
9 }
10
11 // valid job number
12 if (!isNaN(Number(taskId))) {
13 const response: HTTPResponse = await HTTPUtils.get(`/bulk_history/${taskId}`);
14
15 if (response.ok) {
16 return response.data;
17 }
18 }
19
20 return [];
21}
22
23// use numJobs in the future to fetch N number of jobs
24export async function getLastNBulkJobs(numJobs: number = 25): Promise<BulkJobTask[]> {
25 const response: HTTPResponse = await HTTPUtils.get(`/bulk_history`);
26
27 if (!response.ok) {
28 return [];
29 }
30
31 return response.data;
32}
33
34export async function startBulkAction(request: StartBulkActionRequest): Promise<StartBulkActionResponse> {
35 const response: HTTPResponse = await HTTPUtils.post('/bulk_action/start', request);
36
37 if (response.ok) {
38 return response.data;
39 }
40
41 throw new Error('Failed to start bulk action: ' + response.error);
42}
43
44export async function uploadFile(url: string, file: File): Promise<void> {
45 const formData = new FormData();
46 formData.append('file', file);
47
48 const response: HTTPResponse = await HTTPUtils.post(url, formData);
49
50 if (!response.ok) {
51 throw new Error('Failed to upload file: ' + response.error);
52 }
53}
54
55export async function uploadCompleted(jobId: string): Promise<void> {
56 const request = { job_id: jobId };
57
58 const response: HTTPResponse = await HTTPUtils.post(`/bulk_action/upload_completed`, request);
59
60 if (!response.ok) {
61 throw new Error('Failed to complete bulk action: ' + response.error);
62 }
63}
64
65export async function getJobs(): Promise<BulkActionJob[]> {
66 const response: HTTPResponse = await HTTPUtils.get(`/bulk_action/jobs`);
67
68 if (!response.ok) {
69 return [];
70 }
71
72 return response.data.jobs;
73}
74
75export async function cancelJob(jobId: string): Promise<void> {
76 const response: HTTPResponse = await HTTPUtils.post(`/bulk_action/jobs/${jobId}/cancel`);
77
78 if (!response.ok) {
79 throw new Error('Failed to cancel job: ' + response.error);
80 }
81}