Spaces:
Running
Running
File size: 3,241 Bytes
d263994 |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
function listApis() {
apiGet('/api/list')
.then(data => {
const body = document.getElementById('api-body');
body.innerHTML = '';
const keys = Object.keys(data);
if (keys.length === 0) {
const row = document.createElement('tr');
row.innerHTML = `<td colspan="4" class="text-danger">No APIs defined.</td>`;
body.appendChild(row);
return;
}
keys.forEach(apiName => {
const api = data[apiName];
const row = document.createElement('tr');
row.innerHTML = `
<td>${apiName}</td>
<td>${api.url}</td>
<td>${api.method}</td>
<td>
<button class="btn btn-sm btn-warning" onclick="editApi('${apiName}')">Edit</button>
<button class="btn btn-sm btn-danger" onclick="deleteApi('${apiName}')">Delete</button>
</td>
`;
body.appendChild(row);
});
})
.catch(err => {
console.error(err);
showResult('api-result', { detail: 'Failed to load API list.' });
});
}
function addApi() {
const apiName = prompt('Enter new API name:');
if (!apiName) return;
const url = prompt('Enter API URL:');
const method = prompt('Enter HTTP method (GET, POST, etc.):');
if (!url || !method) {
alert('URL and method are required.');
return;
}
const apiData = {
url: url,
method: method,
headers: [],
body: {},
timeout: 5,
retry_count: 0
};
apiPost('/api/add', { api_name: apiName, api_data: apiData })
.then(data => {
showResult('api-result', data);
listApis();
})
.catch(err => {
console.error(err);
showResult('api-result', { detail: 'Failed to add API.' });
});
}
function editApi(apiName) {
const url = prompt('Enter new API URL:');
const method = prompt('Enter new HTTP method:');
if (!url || !method) {
alert('URL and method are required.');
return;
}
const apiData = {
url: url,
method: method,
headers: [],
body: {},
timeout: 5,
retry_count: 0
};
apiPost('/api/update', { api_name: apiName, api_data: apiData })
.then(data => {
showResult('api-result', data);
listApis();
})
.catch(err => {
console.error(err);
showResult('api-result', { detail: 'Failed to update API.' });
});
}
function deleteApi(apiName) {
if (!confirm(`Are you sure you want to delete API '${apiName}'?`)) return;
apiPost('/api/delete', { api_name: apiName })
.then(data => {
showResult('api-result', data);
listApis();
})
.catch(err => {
console.error(err);
if (err.detail) {
alert(`❗ ${err.detail}`);
}
showResult('api-result', { detail: 'Failed to delete API.' });
});
}
|