ciyidogan commited on
Commit
d263994
·
verified ·
1 Parent(s): 159cd81

Create api.js

Browse files
Files changed (1) hide show
  1. static/js/api.js +111 -0
static/js/api.js ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function listApis() {
2
+ apiGet('/api/list')
3
+ .then(data => {
4
+ const body = document.getElementById('api-body');
5
+ body.innerHTML = '';
6
+ const keys = Object.keys(data);
7
+ if (keys.length === 0) {
8
+ const row = document.createElement('tr');
9
+ row.innerHTML = `<td colspan="4" class="text-danger">No APIs defined.</td>`;
10
+ body.appendChild(row);
11
+ return;
12
+ }
13
+
14
+ keys.forEach(apiName => {
15
+ const api = data[apiName];
16
+ const row = document.createElement('tr');
17
+ row.innerHTML = `
18
+ <td>${apiName}</td>
19
+ <td>${api.url}</td>
20
+ <td>${api.method}</td>
21
+ <td>
22
+ <button class="btn btn-sm btn-warning" onclick="editApi('${apiName}')">Edit</button>
23
+ <button class="btn btn-sm btn-danger" onclick="deleteApi('${apiName}')">Delete</button>
24
+ </td>
25
+ `;
26
+ body.appendChild(row);
27
+ });
28
+ })
29
+ .catch(err => {
30
+ console.error(err);
31
+ showResult('api-result', { detail: 'Failed to load API list.' });
32
+ });
33
+ }
34
+
35
+ function addApi() {
36
+ const apiName = prompt('Enter new API name:');
37
+ if (!apiName) return;
38
+
39
+ const url = prompt('Enter API URL:');
40
+ const method = prompt('Enter HTTP method (GET, POST, etc.):');
41
+
42
+ if (!url || !method) {
43
+ alert('URL and method are required.');
44
+ return;
45
+ }
46
+
47
+ const apiData = {
48
+ url: url,
49
+ method: method,
50
+ headers: [],
51
+ body: {},
52
+ timeout: 5,
53
+ retry_count: 0
54
+ };
55
+
56
+ apiPost('/api/add', { api_name: apiName, api_data: apiData })
57
+ .then(data => {
58
+ showResult('api-result', data);
59
+ listApis();
60
+ })
61
+ .catch(err => {
62
+ console.error(err);
63
+ showResult('api-result', { detail: 'Failed to add API.' });
64
+ });
65
+ }
66
+
67
+ function editApi(apiName) {
68
+ const url = prompt('Enter new API URL:');
69
+ const method = prompt('Enter new HTTP method:');
70
+
71
+ if (!url || !method) {
72
+ alert('URL and method are required.');
73
+ return;
74
+ }
75
+
76
+ const apiData = {
77
+ url: url,
78
+ method: method,
79
+ headers: [],
80
+ body: {},
81
+ timeout: 5,
82
+ retry_count: 0
83
+ };
84
+
85
+ apiPost('/api/update', { api_name: apiName, api_data: apiData })
86
+ .then(data => {
87
+ showResult('api-result', data);
88
+ listApis();
89
+ })
90
+ .catch(err => {
91
+ console.error(err);
92
+ showResult('api-result', { detail: 'Failed to update API.' });
93
+ });
94
+ }
95
+
96
+ function deleteApi(apiName) {
97
+ if (!confirm(`Are you sure you want to delete API '${apiName}'?`)) return;
98
+
99
+ apiPost('/api/delete', { api_name: apiName })
100
+ .then(data => {
101
+ showResult('api-result', data);
102
+ listApis();
103
+ })
104
+ .catch(err => {
105
+ console.error(err);
106
+ if (err.detail) {
107
+ alert(`❗ ${err.detail}`);
108
+ }
109
+ showResult('api-result', { detail: 'Failed to delete API.' });
110
+ });
111
+ }