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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/* jshint esversion: 2024, module: true */
import { ApiEndpoints } from './enums.js';
/**
* Static API Client for network operations
* @class ApiClient
*/
class ApiClient {
/**
* API utility function
* @static
* @param {string} path - API endpoint
* @param {Object} [options] - Fetch options
* @returns {Promise<Response>}
*/
static async request(path, options = {}) {
const response = await fetch(path, options);
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return response;
}
/**
* GET request returning JSON
* @static
* @param {string} path - API endpoint
* @returns {Promise<Object>}
*/
static async get(path) {
const response = await ApiClient.request(path);
return response.json();
}
/**
* GET request returning text
* @static
* @param {string} path - API endpoint
* @returns {Promise<string>}
*/
static async getText(path) {
const response = await ApiClient.request(path);
return response.text();
}
/**
* POST request with JSON body
* @static
* @param {string} path - API endpoint
* @param {Object} [data] - Request body
* @returns {Promise<Object>}
*/
static async post(path, data = null) {
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
};
if (data) {
options.body = JSON.stringify(data);
}
const response = await ApiClient.request(path, options);
return response.json();
}
/**
* Get network status
* @static
* @returns {Promise<Object>}
*/
static async getNetworkStatus() {
return ApiClient.get(ApiEndpoints.STATUS);
}
/**
* Get configuration files list
* @static
* @returns {Promise<Object>}
*/
static async getConfigFiles() {
return ApiClient.get(ApiEndpoints.CONFIGS);
}
/**
* Get configuration file content
* @static
* @param {string} name - File name
* @returns {Promise<string>}
*/
static async getConfigFile(name) {
return ApiClient.getText(`${ApiEndpoints.CONFIG}/${encodeURIComponent(name)}`);
}
/**
* Validate configuration
* @static
* @param {string} name - File name
* @param {string} content - File content
* @returns {Promise<Object>}
*/
static async validateConfig(name, content) {
return ApiClient.post(ApiEndpoints.VALIDATE, { name, content });
}
/**
* Save configuration file
* @static
* @param {string} name - File name
* @param {string} content - File content
* @param {boolean} restart - Restart service
* @returns {Promise<Object>}
*/
static async saveConfig(name, content, restart) {
return ApiClient.post(ApiEndpoints.SAVE, { name, content, restart });
}
/**
* Get system logs
* @static
* @returns {Promise<string>}
*/
static async getSystemLogs() {
return ApiClient.getText(ApiEndpoints.LOGS);
}
/**
* Restart networkd service
* @static
* @returns {Promise<Object>}
*/
static async restartNetworkd() {
return ApiClient.post(ApiEndpoints.RELOAD);
}
/**
* Reboot device
* @static
* @returns {Promise<Object>}
*/
static async rebootDevice() {
return ApiClient.post(ApiEndpoints.REBOOT);
}
}
export { ApiClient };
|