133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
// API Client for communicating with the backend
|
|
|
|
class ApiClient {
|
|
constructor() {
|
|
this.baseUrl = 'http://localhost:3001'; // Backend server URL
|
|
}
|
|
|
|
async getClusterMembers() {
|
|
try {
|
|
const response = await fetch(`${this.baseUrl}/api/cluster/members`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
throw new Error(`Request failed: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async getDiscoveryInfo() {
|
|
try {
|
|
const response = await fetch(`${this.baseUrl}/api/discovery/nodes`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
throw new Error(`Request failed: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async selectRandomPrimaryNode() {
|
|
try {
|
|
const response = await fetch(`${this.baseUrl}/api/discovery/random-primary`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
throw new Error(`Request failed: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async getNodeStatus(ip) {
|
|
try {
|
|
const response = await fetch(`${this.baseUrl}/api/node/status/${encodeURIComponent(ip)}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
throw new Error(`Request failed: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async getTasksStatus(ip) {
|
|
try {
|
|
const url = ip
|
|
? `${this.baseUrl}/api/tasks/status?ip=${encodeURIComponent(ip)}`
|
|
: `${this.baseUrl}/api/tasks/status`;
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
throw new Error(`Request failed: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async uploadFirmware(file, nodeIp) {
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
|
|
const response = await fetch(`${this.baseUrl}/api/node/update?ip=${encodeURIComponent(nodeIp)}`, {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.message || `HTTP ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
} catch (error) {
|
|
throw new Error(`Upload failed: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Global API client instance
|
|
window.apiClient = new ApiClient();
|