summaryrefslogtreecommitdiffstats
path: root/static/api-client.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/api-client.js')
-rw-r--r--static/api-client.js104
1 files changed, 92 insertions, 12 deletions
diff --git a/static/api-client.js b/static/api-client.js
index 4a1b81e..7a6ffbd 100644
--- a/static/api-client.js
+++ b/static/api-client.js
@@ -1,18 +1,20 @@
/* jshint esversion: 2024, module: true */
+import { ApiEndpoints } from './enums.js';
+
/**
- * API Client for network operations
+ * Static API Client for network operations
* @class ApiClient
*/
class ApiClient {
/**
* API utility function
- * @method request
+ * @static
* @param {string} path - API endpoint
* @param {Object} [options] - Fetch options
* @returns {Promise<Response>}
*/
- async request(path, options = {}) {
+ static async request(path, options = {}) {
const response = await fetch(path, options);
if (!response.ok) {
@@ -25,34 +27,34 @@ class ApiClient {
/**
* GET request returning JSON
- * @method get
+ * @static
* @param {string} path - API endpoint
* @returns {Promise<Object>}
*/
- async get(path) {
- const response = await this.request(path);
+ static async get(path) {
+ const response = await ApiClient.request(path);
return response.json();
}
/**
* GET request returning text
- * @method getText
+ * @static
* @param {string} path - API endpoint
* @returns {Promise<string>}
*/
- async getText(path) {
- const response = await this.request(path);
+ static async getText(path) {
+ const response = await ApiClient.request(path);
return response.text();
}
/**
* POST request with JSON body
- * @method post
+ * @static
* @param {string} path - API endpoint
* @param {Object} [data] - Request body
* @returns {Promise<Object>}
*/
- async post(path, data = null) {
+ static async post(path, data = null) {
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -62,9 +64,87 @@ class ApiClient {
options.body = JSON.stringify(data);
}
- const response = await this.request(path, options);
+ 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 };