diff --git a/lib/api.js b/lib/api.js index bb1e859..8f4cf44 100644 --- a/lib/api.js +++ b/lib/api.js @@ -152,7 +152,18 @@ Api.prototype = { if (!requestOptions.url) throw new Error('url must be specified when doing a manual request'); request = self.client[method](requestOptions.url, requestOptions, function(data, response){ - if (response.statusCode < 200 || response.statusCode > 299) { + if (response.statusCode === 401) { + // Authorization error + self.authorize(function(){ + self.method.call(self, method, function(data, response){ + // Check if we have to wrap data into a model + if (_.isFunction(callback)) { + callback(data, response); + } + resolve(data, response); + }, options) + }); + } else if (response.statusCode < 200 || response.statusCode > 299) { if (_.isFunction(callback)) callback(data, response); resolve(data.Message); self.errorHandler(data.Message, data); @@ -165,7 +176,18 @@ Api.prototype = { }); } else { request = self.client.methods[method](requestOptions, function(data, response){ - if (response.statusCode < 200 || response.statusCode > 299) { + if (response.statusCode === 401) { + // Authorization error + self.authorize(function(){ + self.method.call(self, method, function(data, response){ + // Check if we have to wrap data into a model + if (_.isFunction(callback)) { + callback(data, response); + } + resolve(data, response); + }, options) + }); + } else if (response.statusCode < 200 || response.statusCode > 299) { if (_.isFunction(callback)) callback(data, response); resolve(data, response); self.errorHandler(data.Message, data);