Files
parcer/src/api/storage.ts

270 lines
7.5 KiB
TypeScript
Raw Normal View History

2024-07-31 19:47:52 +05:30
import { default as axios } from "axios";
2024-07-31 21:10:52 +05:30
import { WorkflowFile } from "maxun-core";
2025-01-09 20:02:58 +05:30
import { RunSettings } from "../components/run/RunSettings";
2025-01-09 20:04:02 +05:30
import { ScheduleSettings } from "../components/robot/ScheduleSettings";
import { CreateRunResponse, ScheduleRunResponse } from "../pages/MainPage";
2024-11-01 08:25:14 +05:30
import { apiUrl } from "../apiConfig";
2024-07-31 19:47:52 +05:30
2025-01-23 01:17:38 +05:30
interface CredentialInfo {
value: string;
type: string;
}
interface Credentials {
2025-01-23 01:17:38 +05:30
[key: string]: CredentialInfo;
}
2024-07-31 19:47:52 +05:30
export const getStoredRecordings = async (): Promise<string[] | null> => {
try {
2024-11-01 08:25:14 +05:30
const response = await axios.get(`${apiUrl}/storage/recordings`);
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
return response.data;
} else {
throw new Error('Couldn\'t retrieve stored recordings');
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
return null;
}
};
2025-05-01 18:07:36 +05:30
export const updateRecording = async (id: string, data: {
name?: string;
limits?: Array<{pairIndex: number, actionIndex: number, argIndex: number, limit: number}>;
credentials?: Credentials;
targetUrl?: string;
// optional full workflow replacement (useful for action renames)
workflow?: any[];
2025-05-01 18:07:36 +05:30
}): Promise<boolean> => {
2024-11-17 02:29:09 +05:30
try {
const response = await axios.put(`${apiUrl}/storage/recordings/${id}`, data);
if (response.status === 200) {
return true;
} else {
throw new Error(`Couldn't update recording with id ${id}`);
}
} catch (error: any) {
console.error(`Error updating recording: ${error.message}`);
return false;
}
};
2024-11-17 14:05:52 +05:30
export const duplicateRecording = async (id: string, targetUrl: string): Promise<any> => {
try {
const response = await axios.post(`${apiUrl}/storage/recordings/${id}/duplicate`, {
targetUrl,
});
if (response.status === 201) {
return response.data; // Returns the duplicated robot details
} else {
throw new Error(`Couldn't duplicate recording with id ${id}`);
}
} catch (error: any) {
console.error(`Error duplicating recording: ${error.message}`);
return null;
}
};
2024-07-31 19:47:52 +05:30
export const getStoredRuns = async (): Promise<string[] | null> => {
try {
2024-11-01 08:25:14 +05:30
const response = await axios.get(`${apiUrl}/storage/runs`);
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
return response.data;
} else {
throw new Error('Couldn\'t retrieve stored recordings');
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
return null;
}
};
2024-10-17 15:28:52 +05:30
export const getStoredRecording = async (id: string) => {
2024-10-17 14:36:04 +05:30
try {
2024-11-01 08:25:14 +05:30
const response = await axios.get(`${apiUrl}/storage/recordings/${id}`);
2024-10-17 14:36:04 +05:30
if (response.status === 200) {
return response.data;
} else {
throw new Error(`Couldn't retrieve stored recording ${id}`);
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-10-17 14:36:04 +05:30
console.log(error);
return null;
}
}
2024-11-15 22:26:36 +05:30
export const checkRunsForRecording = async (id: string): Promise<boolean> => {
try {
2024-11-15 22:26:36 +05:30
const response = await axios.get(`${apiUrl}/storage/recordings/${id}/runs`);
const runs = response.data;
2024-11-15 22:26:36 +05:30
console.log(runs.runs.totalCount)
return runs.runs.totalCount > 0;
} catch (error) {
console.error('Error checking runs for recording:', error);
return false;
}
};
export const deleteRecordingFromStorage = async (id: string): Promise<boolean> => {
const hasRuns = await checkRunsForRecording(id);
2025-01-01 23:31:21 +05:30
if (hasRuns) {
2025-01-01 23:31:21 +05:30
return false;
}
2024-07-31 19:47:52 +05:30
try {
2024-11-01 08:25:14 +05:30
const response = await axios.delete(`${apiUrl}/storage/recordings/${id}`);
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
2025-01-01 23:31:21 +05:30
return true;
2024-07-31 19:47:52 +05:30
} else {
throw new Error(`Couldn't delete stored recording ${id}`);
2024-07-31 19:47:52 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
2025-01-01 23:31:21 +05:30
2024-07-31 19:47:52 +05:30
return false;
}
};
export const deleteRunFromStorage = async (id: string): Promise<boolean> => {
2024-07-31 19:47:52 +05:30
try {
2024-11-01 08:25:14 +05:30
const response = await axios.delete(`${apiUrl}/storage/runs/${id}`);
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
return response.data;
} else {
throw new Error(`Couldn't delete stored recording ${id}`);
2024-07-31 19:47:52 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
return false;
}
};
2024-10-10 03:48:10 +05:30
export const editRecordingFromStorage = async (browserId: string, id: string): Promise<WorkflowFile | null> => {
2024-07-31 19:47:52 +05:30
try {
2024-11-01 08:25:14 +05:30
const response = await axios.put(`${apiUrl}/workflow/${browserId}/${id}`);
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
return response.data;
} else {
2024-10-10 03:48:10 +05:30
throw new Error(`Couldn't edit stored recording ${id}`);
2024-07-31 19:47:52 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
return null;
}
};
2025-06-03 18:27:58 +05:30
export interface CreateRunResponseWithQueue extends CreateRunResponse {
queued?: boolean;
}
export const createAndRunRecording = async (id: string, settings: RunSettings): Promise<CreateRunResponseWithQueue> => {
try {
const response = await axios.put(
2025-06-03 18:43:46 +05:30
`${apiUrl}/storage/runs/${id}`,
2025-06-03 18:27:58 +05:30
{ ...settings, withCredentials: true }
);
if (response.status === 200) {
return response.data;
} else {
throw new Error(`Couldn't create and run recording ${id}`);
}
} catch (error: any) {
console.log(error);
return { browserId: '', runId: '', robotMetaId: '', queued: false };
}
}
export const createRunForStoredRecording = async (id: string, settings: RunSettings): Promise<CreateRunResponse> => {
2024-07-31 19:47:52 +05:30
try {
const response = await axios.put(
2024-11-01 08:25:14 +05:30
`${apiUrl}/storage/runs/${id}`,
2025-01-01 23:31:21 +05:30
{ ...settings });
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
return response.data;
} else {
throw new Error(`Couldn't create a run for a recording ${id}`);
2024-07-31 19:47:52 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
2025-01-29 23:52:39 +05:30
return { browserId: '', runId: '', robotMetaId: '' };
2024-07-31 19:47:52 +05:30
}
}
2024-10-10 02:24:23 +05:30
export const interpretStoredRecording = async (id: string): Promise<boolean> => {
2024-07-31 19:47:52 +05:30
try {
2024-11-01 08:25:14 +05:30
const response = await axios.post(`${apiUrl}/storage/runs/run/${id}`);
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
return response.data;
} else {
2024-10-10 02:24:23 +05:30
throw new Error(`Couldn't run a recording ${id}`);
2024-07-31 19:47:52 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
return false;
}
}
2025-06-12 11:17:59 +05:30
export const notifyAboutAbort = async (id: string): Promise<{ success: boolean; isQueued?: boolean }> => {
2024-07-31 19:47:52 +05:30
try {
2025-06-12 11:19:35 +05:30
const response = await axios.post(`${apiUrl}/storage/runs/abort/${id}`, { withCredentials: true });
2024-07-31 19:47:52 +05:30
if (response.status === 200) {
2025-06-12 11:17:59 +05:30
return {
success: response.data.success,
isQueued: response.data.isQueued
};
2024-07-31 19:47:52 +05:30
} else {
2024-10-10 02:24:23 +05:30
throw new Error(`Couldn't abort a running recording with id ${id}`);
2024-07-31 19:47:52 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-07-31 19:47:52 +05:30
console.log(error);
2025-06-12 11:17:59 +05:30
return { success: false };
2024-07-31 19:47:52 +05:30
}
}
2025-06-12 11:17:59 +05:30
2024-10-10 03:35:37 +05:30
export const scheduleStoredRecording = async (id: string, settings: ScheduleSettings): Promise<ScheduleRunResponse> => {
2024-09-12 23:41:33 +05:30
try {
const response = await axios.put(
2024-11-01 08:25:14 +05:30
`${apiUrl}/storage/schedule/${id}`,
2024-10-22 20:44:14 +05:30
{ ...settings });
2024-09-12 23:41:33 +05:30
if (response.status === 200) {
return response.data;
} else {
2024-10-10 03:35:37 +05:30
throw new Error(`Couldn't schedule recording ${id}. Please try again later.`);
2024-09-12 23:41:33 +05:30
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-09-12 23:41:33 +05:30
console.log(error);
2024-10-22 20:44:14 +05:30
return { message: '', runId: '' };
2024-09-12 23:41:33 +05:30
}
}
2024-10-22 20:43:49 +05:30
2024-10-23 02:36:04 +05:30
export const getSchedule = async (id: string) => {
2024-10-22 20:43:49 +05:30
try {
2024-11-01 08:25:14 +05:30
const response = await axios.get(`${apiUrl}/storage/schedule/${id}`);
2024-10-22 20:43:49 +05:30
if (response.status === 200) {
2024-10-23 03:16:57 +05:30
return response.data.schedule;
2024-10-22 20:43:49 +05:30
} else {
throw new Error(`Couldn't retrieve schedule for recording ${id}`);
}
2024-10-22 20:44:14 +05:30
} catch (error: any) {
2024-10-22 20:43:49 +05:30
console.log(error);
return null;
}
2024-10-22 21:05:52 +05:30
}
export const deleteSchedule = async (id: string): Promise<boolean> => {
try {
2024-11-01 08:25:14 +05:30
const response = await axios.delete(`${apiUrl}/storage/schedule/${id}`);
2024-10-22 21:05:52 +05:30
if (response.status === 200) {
return response.data;
} else {
throw new Error(`Couldn't delete schedule for recording ${id}`);
}
} catch (error: any) {
console.log(error);
return false;
}
2024-10-22 20:43:49 +05:30
}