From 8489f1f83da5dcc5818401393b1f6a430eea677c Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Sun, 28 Sep 2025 15:08:14 +0300 Subject: Intial version --- static/api-client.js | 104 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 12 deletions(-) (limited to 'static/api-client.js') 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} */ - 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} */ - 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} */ - 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} */ - 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} + */ + static async getNetworkStatus() { + return ApiClient.get(ApiEndpoints.STATUS); + } + + /** + * Get configuration files list + * @static + * @returns {Promise} + */ + static async getConfigFiles() { + return ApiClient.get(ApiEndpoints.CONFIGS); + } + + /** + * Get configuration file content + * @static + * @param {string} name - File name + * @returns {Promise} + */ + 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} + */ + 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} + */ + static async saveConfig(name, content, restart) { + return ApiClient.post(ApiEndpoints.SAVE, { name, content, restart }); + } + + /** + * Get system logs + * @static + * @returns {Promise} + */ + static async getSystemLogs() { + return ApiClient.getText(ApiEndpoints.LOGS); + } + + /** + * Restart networkd service + * @static + * @returns {Promise} + */ + static async restartNetworkd() { + return ApiClient.post(ApiEndpoints.RELOAD); + } + + /** + * Reboot device + * @static + * @returns {Promise} + */ + static async rebootDevice() { + return ApiClient.post(ApiEndpoints.REBOOT); + } } export { ApiClient }; -- cgit v1.2.3-70-g09d2