Refactors all Glances widgets to inherit from parent mixin

This commit is contained in:
Alicia Sykes
2022-01-19 11:43:04 +00:00
parent ea3ffa5d36
commit dfea4e317c
14 changed files with 75 additions and 107 deletions

View File

@@ -0,0 +1,36 @@
/** Reusable mixin for all Glances widgets */
const WidgetMixin = {
computed: {
/* Required, hostname (e.g. IP + port) for Glances instance */
hostname() {
if (!this.options.hostname) this.error('You must specify a \'hostname\' for Glaces');
return this.options.hostname;
},
/* Optionally specify the API version, defaults to V 3 */
apiVersion() {
return this.options.apiVersion || 3;
},
/* Optionally specify basic auth credentials for Glances instance */
credentials() {
if (this.options.username && this.options.password) {
const stringifiedUser = `${this.options.username}:${this.options.password}`;
const headers = { Authorization: `Basic ${window.btoa(stringifiedUser)}` };
return { headers };
}
return null;
},
},
methods: {
/* Make the request to Glances API, and calls handler function with results
* Requires endpoint attribute and processData method to be implemented by child */
fetchData() {
this.makeRequest(this.endpoint, this.credentials).then(this.processData);
},
/* Returns URL to Glances API endpoint */
makeGlancesUrl(apiPath) {
return `${this.hostname}/api/${this.apiVersion}/${apiPath}`;
},
},
};
export default WidgetMixin;