Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions lib/common/service-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ ServiceObject.prototype.create = function(options, callback) {

args.push(onCreate);

this.createMethod.apply(null, args);
return this.createMethod.apply(null, args);
};

/**
Expand All @@ -146,9 +146,7 @@ ServiceObject.prototype.delete = function(callback) {

// The `request` method may have been overridden to hold any special behavior.
// Ensure we call the original `request` method.
ServiceObject.prototype.request.call(this, reqOpts, function(err, resp) {
callback(err, resp);
});
return ServiceObject.prototype.request.call(this, reqOpts, callback);
};

/**
Expand Down Expand Up @@ -200,7 +198,7 @@ ServiceObject.prototype.get = function(config, callback) {
var autoCreate = config.autoCreate && is.fn(this.create);
delete config.autoCreate;

this.getMetadata(function(err, metadata) {
return this.getMetadata(function(err, metadata) {
if (err) {
if (err.code === 404 && autoCreate) {
var args = [callback];
Expand Down Expand Up @@ -240,16 +238,11 @@ ServiceObject.prototype.getMetadata = function(callback) {

// The `request` method may have been overridden to hold any special behavior.
// Ensure we call the original `request` method.
ServiceObject.prototype.request.call(this, reqOpts, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

return ServiceObject.prototype.request.call(this, reqOpts).then(function(resp) {
self.metadata = resp;

callback(null, self.metadata, resp);
});
return [self.metadata, resp];
}).asCallback(callback, { spread: true });
};

/**
Expand All @@ -264,8 +257,6 @@ ServiceObject.prototype.getMetadata = function(callback) {
ServiceObject.prototype.setMetadata = function(metadata, callback) {
var self = this;

callback = callback || util.noop;

var methodConfig = this.methods.setMetadata || {};

var reqOpts = extend(true, {
Expand All @@ -276,16 +267,11 @@ ServiceObject.prototype.setMetadata = function(metadata, callback) {

// The `request` method may have been overridden to hold any special behavior.
// Ensure we call the original `request` method.
ServiceObject.prototype.request.call(this, reqOpts, function(err, resp) {
if (err) {
callback(err, resp);
return;
}

return ServiceObject.prototype.request.call(this, reqOpts).then(function(resp) {
self.metadata = resp;

callback(null, resp);
});
return [resp];
}).asCallback(callback, { spread: true });
};

/**
Expand Down
80 changes: 44 additions & 36 deletions lib/common/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var arrify = require('arrify');
var extend = require('extend');
var Promise = require('bluebird');

/**
* @type {module:common/util}
Expand Down Expand Up @@ -68,42 +69,49 @@ function Service(config, options) {
* @param {function} callback - The callback function passed to `request`.
*/
Service.prototype.request = function(reqOpts, callback) {
var uriComponents = [
this.baseUrl
];

if (this.projectIdRequired) {
uriComponents.push('projects');
uriComponents.push(this.projectId);
}

uriComponents.push(reqOpts.uri);

reqOpts.uri = uriComponents
.map(function(uriComponent) {
var trimSlashesRegex = /^\/*|\/*$/g;
return uriComponent.replace(trimSlashesRegex, '');
})
.join('/')
// Some URIs have colon separators.
// Bad: https://.../projects/:list
// Good: https://.../projects:list
.replace(/\/:/g, ':');

// Interceptors should be called in the order they were assigned.
var combinedInterceptors = [].slice.call(this.globalInterceptors)
.concat(this.interceptors)
.concat(arrify(reqOpts.interceptors_));

var interceptor;

while ((interceptor = combinedInterceptors.shift()) && interceptor.request) {
reqOpts = interceptor.request(reqOpts);
}

delete reqOpts.interceptors_;

return this.makeAuthenticatedRequest(reqOpts, callback);
var self = this;
return new Promise(function (resolve, reject) {
var uriComponents = [
self.baseUrl
];

if (self.projectIdRequired) {
uriComponents.push('projects');
uriComponents.push(self.projectId);
}

uriComponents.push(reqOpts.uri);

reqOpts.uri = uriComponents
.map(function(uriComponent) {
var trimSlashesRegex = /^\/*|\/*$/g;
return uriComponent.replace(trimSlashesRegex, '');
})
.join('/')
// Some URIs have colon separators.
// Bad: https://.../projects/:list
// Good: https://.../projects:list
.replace(/\/:/g, ':');

// Interceptors should be called in the order they were assigned.
var combinedInterceptors = [].slice.call(self.globalInterceptors)
.concat(self.interceptors)
.concat(arrify(reqOpts.interceptors_));

var interceptor;

while ((interceptor = combinedInterceptors.shift()) && interceptor.request) {
reqOpts = interceptor.request(reqOpts);
}

delete reqOpts.interceptors_;
return self.makeAuthenticatedRequest(reqOpts, function (err, resp) {
if (err) {
err.response = resp;
}
return err ? reject(err) : resolve(resp);
});
}).asCallback(callback, { spread: true });
};

module.exports = Service;
29 changes: 10 additions & 19 deletions lib/prediction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var extend = require('extend');
var format = require('string-format-obj');
var is = require('is');
var nodeutil = require('util');
var Promise = require('bluebird');

/**
* @type {module:prediction/model}
Expand Down Expand Up @@ -183,21 +184,16 @@ Prediction.prototype.createModel = function(id, options, callback) {
delete body.type;
}

this.request({
return self.request({
method: 'POST',
uri: '/trainedmodels',
json: body
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

}).then(function(resp) {
var model = self.model(resp.id);
model.metadata = resp;

callback(null, model, resp);
});
return [model, resp];
}).asCallback(callback, { spread: true });
};

/**
Expand Down Expand Up @@ -270,20 +266,15 @@ Prediction.prototype.createModel = function(id, options, callback) {
Prediction.prototype.getModels = function(query, callback) {
var self = this;

if (is.fn(query)) {
if (is.fn(query) || !query) {
callback = query;
query = {};
}

this.request({
return self.request({
uri: '/trainedmodels/list',
qs: query
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

}).then(function(resp) {
var models = arrify(resp.items).map(function(model) {
var modelInstance = self.model(model.id);
modelInstance.metadata = model;
Expand All @@ -298,8 +289,8 @@ Prediction.prototype.getModels = function(query, callback) {
});
}

callback(null, models, nextQuery, resp);
});
return [models, nextQuery, resp];
}).asCallback(callback, { spread: true });
};

/**
Expand Down
28 changes: 9 additions & 19 deletions lib/prediction/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,16 @@ nodeutil.inherits(Model, ServiceObject);
* });
*/
Model.prototype.analyze = function(callback) {
this.request({
return this.request({
uri: '/analyze'
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

}).then(function(resp) {
var analysis = {
data: resp.dataDescription || {},
model: resp.modelDescription || {}
};

callback(null, analysis, resp);
});
return [analysis, resp];
}).asCallback(callback, { spread: true });
};

/**
Expand Down Expand Up @@ -331,20 +326,15 @@ Model.prototype.createWriteStream = function(label) {
* });
*/
Model.prototype.query = function(input, callback) {
this.request({
return this.request({
method: 'POST',
uri: '/predict',
json: {
input: {
csvInstance: arrify(input)
}
}
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

}).then(function(resp) {
var results = {
winner: resp.outputLabel || resp.outputValue,
scores: resp.outputMulti
Expand All @@ -357,8 +347,8 @@ Model.prototype.query = function(input, callback) {
})
};

callback(null, results, resp);
});
return [results, resp];
}).asCallback(callback, { spread: true });
};

/**
Expand All @@ -381,7 +371,7 @@ Model.prototype.query = function(input, callback) {
* });
*/
Model.prototype.train = function(label, input, callback) {
this.setMetadata({
return this.setMetadata({
output: label,
csvInstance: arrify(input)
}, callback);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"array-uniq": "^1.0.2",
"arrify": "^1.0.0",
"async": "^1.4.2",
"bluebird": "^3.3.3",
"camelize": "^1.0.0",
"concat-stream": "^1.5.0",
"create-error-class": "^2.0.1",
Expand Down Expand Up @@ -135,7 +136,8 @@
"scripts": {
"docs": "node ./scripts/docs.js",
"lint": "jshint lib/ system-test/ test/ && jscs lib/ system-test/ test/",
"test": "npm run docs && mocha test/*/*.js test/index.js test/docs.js",
"mocha": "mocha test/*/*.js test/index.js test/docs.js",
"test": "npm run docs && npm run mocha",
"system-test": "mocha system-test/* --timeout 60s --bail",
"cover": "istanbul cover -x 'system-test/*' _mocha -- --timeout 60s test/*/*.js test/docs.js system-test/*",
"coveralls": "istanbul cover -x 'system-test/*' _mocha --report lcovonly -- --timeout 60s test/*/*.js test/docs.js system-test/* -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
Expand Down
Loading