Skip to content

Commit d739f41

Browse files
Merge pull request #75 from watson-developer-cloud/dev
Code review
2 parents 82f6fb5 + ddfd0b8 commit d739f41

File tree

15 files changed

+191
-60
lines changed

15 files changed

+191
-60
lines changed

examples/search_lifecycle.v1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async.series([
4444

4545
function deleteCluster(done) {
4646
console.log('Deleting Solr cluster ' + clusterId);
47-
search.deleteCluster({clusterId: clusterId}, function(err, res) {
47+
search.deleteCluster({clusterId: clusterId}, function(err) {
4848
printResponse(err, 'Error deleting Solr cluster: ', 'Deleted Solr cluster ' + clusterId, done);
4949
});
5050
}
@@ -57,7 +57,7 @@ function waitForCluster(clusterId, callback) {
5757
}
5858

5959
console.log('Waiting for Solr cluster ' + clusterId + ' to be ready...');
60-
if (res.solr_cluster_status == 'READY') {
60+
if (res.solr_cluster_status === 'READY') {
6161
console.log('Solr cluster ' + clusterId + ' is ready.');
6262
callback();
6363
} else {

examples/search_solr.v1.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ var solrClient = search.createSolrClient({
2828
async.series([
2929
function uploadConfig(done) {
3030
console.log('Uploading Solr config ' + configName);
31-
search.uploadConfig({clusterId: clusterId, configName: configName, configZipPath: configZipPath},
32-
function(err, res) {
33-
printResponse(err, 'Error uploading Solr config: ', 'Uploaded Solr config ' + configName, done);
34-
});
31+
search.uploadConfig({
32+
clusterId: clusterId,
33+
configName: configName,
34+
configZipPath: configZipPath
35+
},
36+
function(err) {
37+
printResponse(err, 'Error uploading Solr config: ', 'Uploaded Solr config ' + configName, done);
38+
});
39+
3540
},
3641

3742
function listConfigs(done) {
@@ -43,20 +48,29 @@ async.series([
4348

4449
function getConfig(done) {
4550
console.log('Getting Solr config ' + configName);
46-
search.getConfig({clusterId: clusterId, configName: configName}, function(err, res) {
51+
search.getConfig({
52+
clusterId: clusterId,
53+
configName: configName
54+
}, function(err) {
4755
if (err) {
4856
console.log('Error getting config: ' + JSON.stringify(err, null, 2));
4957
} else {
5058
// Save response to a local file here
5159
}
5260
done();
5361
});
62+
5463
},
5564

5665
function createCollection(done) {
57-
search.createCollection({clusterId: clusterId, collectionName: collectionName, configName: configName}, function(err, res) {
66+
search.createCollection({
67+
clusterId: clusterId,
68+
collectionName: collectionName,
69+
configName: configName
70+
}, function(err, res) {
5871
printResponse(err, 'Error creating Solr collection: ', res, done);
5972
});
73+
6074
},
6175

6276
function listCollections(done) {
@@ -68,13 +82,13 @@ async.series([
6882
function indexAndCommit(done) {
6983
console.log('Indexing a document...');
7084
var doc = { id : 1234, title_t : 'Hello', text: 'some text' };
71-
solrClient.add(doc, function(err, addResponse) {
85+
solrClient.add(doc, function(err) {
7286
if(err) {
7387
console.log('Error indexing document: ' + err);
7488
done();
7589
} else {
7690
console.log('Indexed a document.');
77-
solrClient.commit(function(err, commitResponse) {
91+
solrClient.commit(function(err) {
7892
if(err) {
7993
console.log('Error committing change: ' + err);
8094
} else {
@@ -86,7 +100,7 @@ async.series([
86100
});
87101
},
88102

89-
function search(done) {
103+
function _search(done) {
90104
console.log('Searching all documents.');
91105
var query = solrClient.createQuery();
92106
query.q({ '*' : '*' });
@@ -103,16 +117,24 @@ async.series([
103117

104118
function deleteCollection(done) {
105119
console.log('Deleting Solr collection ' + collectionName);
106-
search.deleteCollection({clusterId: clusterId, collectionName: collectionName}, function(err, res) {
120+
search.deleteCollection({
121+
clusterId: clusterId,
122+
collectionName: collectionName
123+
}, function(err) {
107124
printResponse(err, 'Error deleting collection: ', 'Deleted Solr collection ' + collectionName, done);
108125
});
126+
109127
},
110128

111129
function deleteConfig(done) {
112130
console.log('Deleting Solr config ' + configName);
113-
search.deleteConfig({clusterId: clusterId, configName: configName}, function(err, res) {
131+
search.deleteConfig({
132+
clusterId: clusterId,
133+
configName: configName
134+
}, function(err) {
114135
printResponse(err, 'Error deleting config: ', 'Deleted Solr config ' + configName, done);
115136
});
137+
116138
},
117139
]);
118140

lib/alchemy_endpoints.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,17 @@
8484
"html" : "/html/HTMLGetCombinedData"
8585
},
8686

87-
"image": {
88-
"url" : "/url/URLGetImage"
87+
"image_link": {
88+
"url" : "/url/URLGetImage",
89+
"html" : "/html/HTMLGetImage"
8990
},
9091

9192
"image_keywords": {
9293
"url" : "/url/URLGetRankedImageKeywords",
9394
"image" : "/image/ImageGetRankedImageKeywords"
9495
},
9596

96-
"image_face_tag": {
97+
"image_recognition": {
9798
"url" : "/url/URLGetRankedImageFaceTags",
9899
"image" : "/image/ImageGetRankedImageFaceTags"
99100
}

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ var watson = {};
114114

115115
// alchemy
116116
'alchemy_language'
117+
//'alchemy_vision'
117118

118119
].forEach(function(api) {
119120
watson[api] = createServiceAPI(api);

services/alchemy_language/v1.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ var endpoints = require('../../lib/alchemy_endpoints.json');
2222
var helper = require('../../lib/helper');
2323

2424
var errorFormatter = function(cb) {
25-
return function(err, result) {
25+
return function(err, result, response) {
2626
if (err) {
2727
cb(err, result);
2828
}
2929
else {
3030
if (result.status === 'OK')
3131
cb(err,result);
3232
else
33-
cb({error: result.statusInfo, code:400 }, null);
33+
cb({
34+
error: result.statusInfo || response['headers']['x-alchemyapi-error-msg'],
35+
code: 400
36+
}, null);
3437
}
3538
};
3639
};

services/alchemy_vision/v1.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var extend = require('extend');
20+
var requestFactory = require('../../lib/requestwrapper');
21+
var endpoints = require('../../lib/alchemy_endpoints.json');
22+
var helper = require('../../lib/helper');
23+
var pick = require('object.pick');
24+
var omit = require('object.omit');
25+
var fs = require('fs');
26+
27+
var errorFormatter = function(cb) {
28+
return function(err, result, response) {
29+
if (err) {
30+
cb(err, result);
31+
}
32+
else {
33+
if (result.status === 'OK')
34+
cb(err,result);
35+
else
36+
cb({
37+
error: result.statusInfo || response['headers']['x-alchemyapi-error-msg'],
38+
code: 400
39+
}, null);
40+
}
41+
};
42+
};
43+
44+
function createRequest(method) {
45+
return function(_params, callback ) {
46+
var params = _params || {};
47+
var accepted_formats = Object.keys(endpoints[method]);
48+
var format = helper.getFormat(params, accepted_formats);
49+
50+
if (format === null) {
51+
callback(new Error('Missing required parameters: ' +
52+
accepted_formats.join(', ') +
53+
' needs to be specified'));
54+
return;
55+
}
56+
57+
var parameters = {
58+
options: {
59+
url: endpoints[method][format],
60+
method: 'POST',
61+
json: true,
62+
qs: extend({outputMode: 'json'}, params) // change default output to json
63+
},
64+
defaultOptions: this._options
65+
};
66+
67+
return requestFactory(parameters, errorFormatter(callback));
68+
};
69+
}
70+
71+
function AlchemyVision(options) {
72+
// Default URL
73+
var serviceDefaults = {
74+
url: 'https://access.alchemyapi.com/calls'
75+
};
76+
// Replace default options with user provided
77+
this._options = extend(serviceDefaults, options);
78+
}
79+
80+
/**
81+
* Extracts images from a URL.
82+
*/
83+
AlchemyVision.prototype.imageLinks = createRequest('image_link');
84+
85+
/**
86+
* Tags image with keywords
87+
*/
88+
AlchemyVision.prototype.imageTags = function(_params, callback) {
89+
var params = _params || {};
90+
91+
if (typeof(params.image) !== 'undefined' && typeof(params.image) !== 'string') {
92+
callback(new Error('Invalid arguments: image needs to be a filename or base64 image'));
93+
return;
94+
}
95+
96+
if (typeof(params.image) !== 'string')
97+
params.imagePostMode = 'raw';
98+
99+
return createRequest('image_keywords').call(this, params, callback);
100+
};
101+
102+
/**
103+
* Face detection and Recognition
104+
*/
105+
AlchemyVision.prototype.imageFaces = function(_params, callback) {
106+
var params = _params || {};
107+
108+
if (typeof(params.image) !== 'string')
109+
params.imagePostMode = 'raw';
110+
111+
return createRequest('image_recognition').call(this, params, callback);
112+
};
113+
114+
module.exports = AlchemyVision;

0 commit comments

Comments
 (0)