⚡ Refactors all Glances widgets to inherit from parent mixin
This commit is contained in:
36
src/mixins/GlancesMixin.js
Normal file
36
src/mixins/GlancesMixin.js
Normal 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;
|
||||
Reference in New Issue
Block a user