122 lines
4.7 KiB
JavaScript
122 lines
4.7 KiB
JavaScript
// --- static/api.js ---
|
|
|
|
// ==========================================
|
|
// 1. 鎖定管理 (Lock Management)
|
|
// ==========================================
|
|
export async function apiAcquireLock(path, userId, username) {
|
|
const response = await fetch('/api/v1/locks/acquire', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ path, user_id: userId, username })
|
|
});
|
|
return { status: response.status, data: await response.json() };
|
|
}
|
|
|
|
export async function apiReleaseLock(path, userId) {
|
|
return fetch('/api/v1/locks/release', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ path, user_id: userId })
|
|
});
|
|
}
|
|
|
|
export async function apiSendHeartbeat(path, userId) {
|
|
return fetch('/api/v1/locks/heartbeat', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ path, user_id: userId })
|
|
});
|
|
}
|
|
|
|
// ==========================================
|
|
// 2. 樹狀圖與選項快取 (Tree & Options)
|
|
// ==========================================
|
|
export async function apiGetFullConfig(host, username, password, skipFilter = false) {
|
|
let url = `/api/v1/cmts-full-config?host=${encodeURIComponent(host)}&username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
|
|
if (skipFilter) url += '&skip_filter=true';
|
|
const response = await fetch(url);
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiGetLeafOptions() {
|
|
const response = await fetch('/api/v1/cmts-leaf-options');
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiSyncLeafOptions(paths) {
|
|
return fetch('/api/v1/cmts-leaf-options/sync', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ leaf_paths: paths })
|
|
});
|
|
}
|
|
|
|
export async function apiClearCache(paths) {
|
|
const response = await fetch('/api/v1/clear_cache', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(paths)
|
|
});
|
|
if (!response.ok) throw new Error(`伺服器回應錯誤: ${response.status}`);
|
|
return response.json();
|
|
}
|
|
|
|
// ==========================================
|
|
// 3. 指令生成與執行 (CLI Generation & Execution)
|
|
// ==========================================
|
|
export async function apiGenerateCli(diffs, interfaces) {
|
|
const response = await fetch('/api/v1/cmts-generate-cli', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ diffs, interfaces_with_admin_state: interfaces })
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiExecuteConfig(script, host, username, password) {
|
|
const response = await fetch('/api/v1/cmts-config', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ script, host, username, password })
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
// ==========================================
|
|
// 4. 系統設定與過濾器 (System Settings)
|
|
// ==========================================
|
|
export async function apiGetTreeFilters() {
|
|
const response = await fetch('/api/v1/settings/tree-filters');
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiSaveTreeFilters(hiddenKeys) {
|
|
const response = await fetch('/api/v1/settings/tree-filters', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ hidden_keys: hiddenKeys })
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
// ==========================================
|
|
// 5. MAC Domain 與綜合查詢 (MAC Domain & Query)
|
|
// ==========================================
|
|
export async function apiGetMacDomainList(host, username, password) {
|
|
const url = `/api/v1/cmts-mac-domain-list?host=${encodeURIComponent(host)}&username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
|
|
const response = await fetch(url);
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiGetMacDomainConfig(target, host, username, password) {
|
|
const url = `/api/v1/cmts-mac-domain-config?target=${encodeURIComponent(target)}&host=${encodeURIComponent(host)}&username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
|
|
const response = await fetch(url);
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiExecuteQuery(queryType, target, host, username, password) {
|
|
const url = `/api/v1/cmts-query?query_type=${queryType}&target=${encodeURIComponent(target)}&host=${encodeURIComponent(host)}&username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
|
|
const response = await fetch(url);
|
|
return response.json();
|
|
}
|