Skip to content
This repository has been archived by the owner on Mar 25, 2018. It is now read-only.

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumit Goel committed Sep 8, 2017
1 parent 10faf03 commit aca3c2b
Showing 1 changed file with 48 additions and 54 deletions.
102 changes: 48 additions & 54 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,77 @@ const req = require('./wrapper');

class CloudGenix {

/**
* CloudGenix API client class.
*
* @param {Object} params -
*/
constructor(params) {

constructor (params) {
this.params = params;
this.token = null;
this.versionMap = {
'profile': '',
'logout': '',
'query_events': ''
};
this.uriMap = {
'profile': '',
'logout': ''
};
this.roleMap = {};
this.tenantId = '';
} // eslint: constructor
}

paramsValidation() {
paramsValidation () {

// eslint-disable-next-line max-statements
return new Promise((resolve, reject) => {

const arr = ['user', 'pass'];
const arr = [
'user',
'pass'
];

// eslint-disable-next-line no-prototype-builtins
const hasUserPass = arr.every(item => this.params.hasOwnProperty(item));
const hasUserPass = arr.every((item) => Object.prototype.hasOwnProperty
.call(this.params, item));

if (hasUserPass) {

// eslint-disable-next-line no-ternary, no-prototype-builtins
this.params.url = this.params.hasOwnProperty('url') ? this.params.url : 'https://api.cloudgenix.com';
if (!Object.prototype.hasOwnProperty.call(this.params, 'url')) {
this.params.url = 'https://api.cloudgenix.com';
}

// eslint-disable-next-line no-ternary, no-prototype-builtins
this.params.apiVersion = this.params.hasOwnProperty('apiVersion') ? this.params.apiVersion : 'v2.0';
if (!Object.prototype.hasOwnProperty.call(this.params, 'apiVersion')) {
this.params.apiVersion = 'v2.0';
}

// eslint-disable-next-line no-ternary, no-prototype-builtins
this.params.options = this.params.hasOwnProperty('options') ? this.params.options : {};
if (!Object.prototype.hasOwnProperty.call(this.params, 'options')) {
this.params.options = {};
}

resolve(this.params);

} else {

reject(new Error('undefined: user and/or pass'));

}
});

}); // eslint: return promise
} // eslint: paramsValidation

/**
* This method queries the permissions endpoint and add resource maps.
*
* @returns {Promise} a promise which resolves to the http response.
*/
permissions() {
permissions () {

const opts = {
'uri': `${this.params.url}/${this.params.apiVersion}/api/permissions`,
'token': this.token,
'options': this.params.options
};

return req.get(opts).then(value => {

return req.get(opts).then((value) => {
this.versionMap = value.resource_version_map;
this.uriMap = value.resource_uri_map;
this.roleMap = value.resource_role_map;
return value;
});

} // eslint: permissions

/**
* This method queries the profile endpoint and add tenant_id.
*
* @returns {Promise} a promise which resolves to the http response.
*/
profile() {
profile () {

const version = this.versionMap.profile;
const baseUrl = this.uriMap.profile.replace('%s', version);
Expand All @@ -90,11 +83,11 @@ class CloudGenix {
'options': this.params.options
};

return req.get(opts).then(value => {

return req.get(opts).then((value) => {
this.tenantId = value.tenant_id;
return value;
});

} // eslint: profile

/**
Expand All @@ -103,7 +96,7 @@ class CloudGenix {
*
* @returns {Promise} a promise which resolves to the http response.
*/
login() {
login () {

const returnLoginPermResponse = {};

Expand All @@ -115,20 +108,21 @@ class CloudGenix {
'password': this.params.pass
},
'options': this.params.options
})).then(value => {

this.token = value.x_auth_token;
returnLoginPermResponse.login = value;
return this.permissions();
}).then(value => {
}))
.then((value) => {
this.token = value.x_auth_token;
returnLoginPermResponse.login = value;
return this.permissions();
})
.then((value) => {
returnLoginPermResponse.permissions = value;
return this.profile();
})
.then((value) => {
returnLoginPermResponse.profile = value;
return returnLoginPermResponse;
});

returnLoginPermResponse.permissions = value;
return this.profile();
}).then(value => {

returnLoginPermResponse.profile = value;
return returnLoginPermResponse;
});
} // eslint: login

/**
Expand All @@ -137,7 +131,7 @@ class CloudGenix {
* @param {string} body - HTTP POST body
* @returns {Promise} a promise which resolves to the http response.
*/
queryEvents(body) {
queryEvents (body) {

const version = this.versionMap.query_events;
const baseUrl = `/${version}/api/tenants/${this.tenantId}/events/query`;
Expand All @@ -150,6 +144,7 @@ class CloudGenix {
};

return req.post(opts);

} // eslint: queryEvents

/**
Expand All @@ -158,8 +153,7 @@ class CloudGenix {
*
* @returns {Promise} a promise which resolves to the http response.
*/
logout() {

logout () {
const version = this.versionMap.logout;
const baseUrl = this.uriMap.logout.replace('%s', version);

Expand All @@ -174,4 +168,4 @@ class CloudGenix {

} // eslint: class

module.exports = CloudGenix;
module.exports = CloudGenix;

0 comments on commit aca3c2b

Please sign in to comment.