Skip to content

Commit

Permalink
Fix issue with promise not working properly. Add statusCode informati…
Browse files Browse the repository at this point in the history
…on in response returning from creator request.

Signed-off-by: Marek Kubiczek <marek.kubiczek@imgtec.com>
  • Loading branch information
Marek Kubiczek committed Dec 8, 2016
1 parent fa224e8 commit 95876e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
68 changes: 34 additions & 34 deletions lib/Client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
@module Client
This module represents an encapsulation of Creator Client Services
in the Creator REST API.
uses http request module
uses http request module
*/

var Q = require('q'),
Expand All @@ -25,13 +25,13 @@ var Q = require('q'),
- @member {string} host - host for the Creator Device Server REST API
**/
function Client(key, secret, host, timeout) {

/* Required client config */

/* if auth key and secret are passed in manually we might need to trim spaces */
this.authKey = key.replace(/ /g, '');
this.authSecret = secret.replace(/ /g, '');

/* Optional config */
this.host = host || config.defaultHost;
config.defaultHost = host || config.defaultHost;
Expand All @@ -56,18 +56,18 @@ Client.prototype.constructURL = function (extension) {
Make an authenticated request against the Creator Device Server.
*/
Client.prototype.request = function (options, callback) {
var client = this,
var client = this,
deferred = Q.defer();

/* Set Options fields */
options.headers = {'Accept': 'application/json', 'Accept-Charset': 'utf-8', 'User-Agent': 'creator-node/' + moduleinfo.version, };
if(!options.timeout) options.timeout = client.timeout; // default timeout
if(options.follow === undefined) options.follow = true; // following by default
if(options.follow === undefined) options.follow = true; // following by default
if(!options.method) options.method = "GET";
var rel = options.relativeEntryPoint;
if(rel)
if(rel.indexOf('http') > -1 || rel.indexOf('https') > -1 || rel.indexOf('www') > -1)
throw config.ONLY_RELATIVE_ERROR;
if(rel)
if(rel.indexOf('http') > -1 || rel.indexOf('https') > -1 || rel.indexOf('www') > -1)
throw config.ONLY_RELATIVE_ERROR;

switch (options.method) {
case "PUT":
Expand All @@ -77,7 +77,7 @@ Client.prototype.request = function (options, callback) {
}
break;
}

if(options.follow) {
if (!options.steps) throw config.STEPS_ERROR;
else {
Expand All @@ -86,39 +86,39 @@ Client.prototype.request = function (options, callback) {
this.authorization = t;

// Then navigate
Navigator.getURL(options.steps, this.authorization, options.relativeEntryPoint).then(function(url){
options.headers.Authorization = this.authorization.token; options.url = url;

// And finally send out the final request
request(options, function (err, response, body) {
if(err) throw 'There has been an error: ' + err;
else if(callback && body) callback(JSON.parse(body));
else callback();
});
});
Navigator.getURL(options.steps, this.authorization, options.relativeEntryPoint).then(
function (url) {
options.headers.Authorization = this.authorization.token;
options.url = url;

// And finally send out the final request
request(options, function (err, response, body) {
if (err)
deferred.reject(err);
else
deferred.resolve(new Client.prototype.response(response.statusCode, body));
});
},
function (err) {
deferred.reject(err);
})
});
}
}
else throw config.FOLLOW_FLAG_ERROR; // TODO: support absolute URL

// Return promise, but also support original node callback style
return deferred.promise.nodeify(callback);
deferred.promise.nodeify(callback);
return deferred.promise;
};

/**
* Store the response in a structured manner
* TODO
* TODO
*/
Client.prototype.response = function(){
this.payload = rawResponse.responseText;
this.errorCode = rawResponse.errorCode;
this.errorMsg = rawResponse.statusText;
this.rawResponse = rawResponse;

this.getStatusCode = function(){
return response.statusCode;
};
Client.prototype.response = function(statusCode, body) {
this.statusCode = statusCode;
this.body = body ? JSON.parse(body) : null;
};


module.exports = Client;
38 changes: 19 additions & 19 deletions lib/resources/navigator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
@module resources/Navigator
This module implements a navigator that can be used to
follow certain steps within the API to consturct a final URL
This module implements a navigator that can be used to
follow certain steps within the API to consturct a final URL
to perform an action on.
*/

Expand Down Expand Up @@ -30,10 +30,10 @@ module.exports.getURL = function(steps, authorization, relativeEntryPoint) {
function navigate(url, _steps, authorization, resolve, reject) {
var cacheKey = url + authorization.token.replace(' ', '');
var cachedData = mCache.get(cacheKey);

if(cachedData !== undefined) {
// Parse and forward onto navigateSteps
cachedData = JSON.parse(cachedData);
cachedData = JSON.parse(cachedData);
navigateSteps(cachedData, _steps, authorization, resolve, reject);
}
else {
Expand All @@ -52,13 +52,13 @@ function navigate(url, _steps, authorization, resolve, reject) {

// Store in the cache
mCache.set(cacheKey, data);

// Parse and forward onto navigateSteps
data = JSON.parse(data);
navigateSteps(data, _steps, authorization, resolve, reject);
}
navigateSteps(data, _steps, authorization, resolve, reject);
}
else {

}
}).catch(function (error) {
throw(error);
Expand All @@ -72,26 +72,26 @@ function navigateSteps(data, _steps, authorization, resolve, reject){
var keyToLook = Object.keys(_steps[0]);
if(data.Items) {
data.Items.some(function (item, index) {
if(_steps[0] && _steps[0][keyToLook] === item[keyToLook]) { // Match the dataType
if(_steps[0] && _steps[0][keyToLook] === item[keyToLook]) { // Match the dataType
_steps.shift();
if(_steps.length === 0) {
resolve(item.Links[0].href); // self href
return true; // .some()
return true; // .some()
}
else {
navigate(item.Links[0].href, _steps, authorization, resolve, reject);
return true; // .some()
return true; // .some()
}
}
else {
if(index + 1 == data.Items.length){
throw('You have objects available in this resource group but the filter {' + keyToLook + ": " + _steps[0][keyToLook] + '} did not return a valid result. Please note data types matter.');
reject(new Error('You have objects available in this resource group but the filter {' + keyToLook + ": " + _steps[0][keyToLook] + '} did not return a valid result. Please note data types matter.'));
}
}
});
}
else {
throw('{' + keyToLook + ': '+ _steps[0][keyToLook] +'} could not be found in the payload. You don\'t have any objects in this resource group.');
reject(new Error('{' + keyToLook + ': '+ _steps[0][keyToLook] +'} could not be found in the payload. You don\'t have any objects in this resource group.'));
}
}
else if(data.Links){
Expand All @@ -101,19 +101,19 @@ function navigateSteps(data, _steps, authorization, resolve, reject){
_steps.shift();
if(_steps.length === 0) {
resolve(link.href);
return true; // .some()
return true; // .some()
}
else {
navigate(link.href, _steps, authorization, resolve, reject);
return true; // .some()
return true; // .some()
}
}
else if(index + 1 == Links.length){
throw('The Link you specified could not be found in the payload');
reject(new Error('The Link you specified could not be found in the payload'));
}
});
}
else {
throw('The link you specified could not be found in the payload. Please check host, relativeEntryPoint and steps options.');
reject(new Error('The link you specified could not be found in the payload. Please check host, relativeEntryPoint and steps options.'));
}
}
}

0 comments on commit 95876e8

Please sign in to comment.