diff --git a/lib/helpers.js b/lib/helpers.js index a53e6a5..7375a57 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -6,7 +6,7 @@ function success(resolve, response) { } function fail(reject, response) { - reject(response.data.message); + reject(response); } exports.promisify = function(module, operation, parameters) { @@ -20,28 +20,32 @@ exports.promisify = function(module, operation, parameters) { return param.name === 'Authorization'; }); - var promise = new Promise(function(resolve, reject) { + var promise = function(access_token) { - if(require_auth) { + return new Promise(function(resolve, reject) { - if(! args[0]) - args[0] = {}; + if(access_token) { - args[0].Authorization = 'Bearer ' + module.access_token; + if(! args[0]) + args[0] = {}; - } + args[0].Authorization = 'Bearer ' + access_token; - args.push(success.bind(null, resolve)); - args.push(fail.bind(null, reject)); + } - operation.apply(self, args); + args.push(success.bind(null, resolve)); + args.push(fail.bind(null, reject)); - }); + operation.apply(self, args); + + }); + + }; if(require_auth) - return module.getAccessToken().then(function() { return promise; }); + return module.getAccessToken().then(promise); - return promise; + return promise(); }; diff --git a/lib/one.js b/lib/one.js index 371f388..3d24b2b 100644 --- a/lib/one.js +++ b/lib/one.js @@ -37,9 +37,10 @@ proto.init = function() { return new Promise(function(resolve, reject) { - self.client = new swagger({ + var client = new swagger({ url: self.swagger_url, success: function() { + self.client = client; helpers.processSwagger(self); resolve(self); }, @@ -56,11 +57,19 @@ proto.getAccessToken = function() { var self = this; - if(! this.access_token) - return this.authorization.createToken(this.credentials); - return new Promise(function(resolve, reject) { - resolve(self.access_token); + + if(self.access_token) + return resolve(self.access_token); + + self.authorization + .createToken(self.credentials) + .then(function(res) { + self.access_token = res.access_token; + resolve(self.access_token); + }) + .catch(reject); + }); };