-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpython.ts
40 lines (32 loc) · 1.01 KB
/
python.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import axios from "axios";
import { globalServerConfig } from "./config.ts";
export const pingPythonModule = async (): Promise<boolean> => {
try {
const response = await axios.get(
`${globalServerConfig.modulePythonUrl}/ping`
);
return response.status === 200;
} catch (error) {
return false;
}
};
export const callPythonModule = async (
action: string,
data: Record<string, any> = {}
) => {
if (!(await pingPythonModule())) {
throw new Error("External Python module is not available");
}
const url = new URL(action, globalServerConfig.modulePythonUrl).href;
if (Object.keys(data).length === 0) {
const response = await axios.post(url);
return response.data as Record<string, any> | string;
}
const response = await axios.post(url, data, {
headers: {
"Content-Type": "application/json",
},
});
const result: Record<string, any> | string = response.data;
return result;
};