Skip to content

Commit de3493a

Browse files
* Fix unused variables and imports thanks to codacity.
Now our code is “A”. which is good.
1 parent 629bd6b commit de3493a

File tree

13 files changed

+186
-58
lines changed

13 files changed

+186
-58
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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict';
22

33
var watson = require('watson-developer-cloud');
4-
var fs = require('fs');
54
var async = require('async');
6-
var solr = require('solr-client');
75

86
var username = 'INSERT YOUR USERNAME FOR THE SERVICE HERE';
97
var password = 'INSERT YOUR PASSWORD FOR THE SERVICE HERE';
@@ -30,10 +28,15 @@ var solrClient = search.createSolrClient({
3028
async.series([
3129
function uploadConfig(done) {
3230
console.log('Uploading Solr config ' + configName);
33-
search.uploadConfig({clusterId: clusterId, configName: configName, configZipPath: configZipPath},
34-
function(err, res) {
35-
printResponse(err, 'Error uploading Solr config: ', 'Uploaded Solr config ' + configName, done);
36-
});
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+
3740
},
3841

3942
function listConfigs(done) {
@@ -45,20 +48,29 @@ async.series([
4548

4649
function getConfig(done) {
4750
console.log('Getting Solr config ' + configName);
48-
search.getConfig({clusterId: clusterId, configName: configName}, function(err, res) {
51+
search.getConfig({
52+
clusterId: clusterId,
53+
configName: configName
54+
}, function(err) {
4955
if (err) {
5056
console.log('Error getting config: ' + JSON.stringify(err, null, 2));
5157
} else {
5258
// Save response to a local file here
5359
}
5460
done();
5561
});
62+
5663
},
5764

5865
function createCollection(done) {
59-
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) {
6071
printResponse(err, 'Error creating Solr collection: ', res, done);
6172
});
73+
6274
},
6375

6476
function listCollections(done) {
@@ -70,13 +82,13 @@ async.series([
7082
function indexAndCommit(done) {
7183
console.log('Indexing a document...');
7284
var doc = { id : 1234, title_t : 'Hello', text: 'some text' };
73-
solrClient.add(doc, function(err, addResponse) {
85+
solrClient.add(doc, function(err) {
7486
if(err) {
7587
console.log('Error indexing document: ' + err);
7688
done();
7789
} else {
7890
console.log('Indexed a document.');
79-
solrClient.commit(function(err, commitResponse) {
91+
solrClient.commit(function(err) {
8092
if(err) {
8193
console.log('Error committing change: ' + err);
8294
} else {
@@ -88,7 +100,7 @@ async.series([
88100
});
89101
},
90102

91-
function search(done) {
103+
function _search(done) {
92104
console.log('Searching all documents.');
93105
var query = solrClient.createQuery();
94106
query.q({ '*' : '*' });
@@ -105,16 +117,24 @@ async.series([
105117

106118
function deleteCollection(done) {
107119
console.log('Deleting Solr collection ' + collectionName);
108-
search.deleteCollection({clusterId: clusterId, collectionName: collectionName}, function(err, res) {
120+
search.deleteCollection({
121+
clusterId: clusterId,
122+
collectionName: collectionName
123+
}, function(err) {
109124
printResponse(err, 'Error deleting collection: ', 'Deleted Solr collection ' + collectionName, done);
110125
});
126+
111127
},
112128

113129
function deleteConfig(done) {
114130
console.log('Deleting Solr config ' + configName);
115-
search.deleteConfig({clusterId: clusterId, configName: configName}, function(err, res) {
131+
search.deleteConfig({
132+
clusterId: clusterId,
133+
configName: configName
134+
}, function(err) {
116135
printResponse(err, 'Error deleting config: ', 'Deleted Solr config ' + configName, done);
117136
});
137+
118138
},
119139
]);
120140

lib/alchemy_endpoints.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
},
8686

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

9192
"image_keywords": {

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ var watson = {};
113113
'user_modeling',
114114

115115
// alchemy
116-
'alchemy_language'
116+
'alchemy_language',
117+
'alchemy_vision'
117118

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

services/alchemy_vision/v1.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 imageOptions = {};
58+
59+
if (typeof(_params.image) !== 'string') {
60+
params.imagePostMode = 'raw';
61+
imageOptions.formData = { attachments: [_params.image] };
62+
} else {
63+
imageOptions.form = pick(params,['image']);
64+
}
65+
66+
var parameters = extend({
67+
options: {
68+
url: endpoints[method][format],
69+
method: 'POST',
70+
json: true,
71+
qs: extend({outputMode: 'json'}, omit(params,['image'])) // change default output to json
72+
},
73+
defaultOptions: this._options
74+
}, imageOptions);
75+
76+
return requestFactory(parameters, errorFormatter(callback));
77+
};
78+
}
79+
80+
function AlchemyVision(options) {
81+
// Default URL
82+
var serviceDefaults = {
83+
url: 'https://access.alchemyapi.com/calls'
84+
};
85+
// Replace default options with user provided
86+
this._options = extend(serviceDefaults, options);
87+
}
88+
89+
/**
90+
* Extracts images from a URL.
91+
*/
92+
AlchemyVision.prototype.imageLinks = createRequest('image');
93+
94+
/**
95+
* Tags image with keywords
96+
*/
97+
AlchemyVision.prototype.imageTags = function(_params, callback) {
98+
var params = _params || {};
99+
100+
101+
return createRequest('image_keywords').call(this, params, callback);
102+
};
103+
104+
/**
105+
* Face detection and Recognition
106+
*/
107+
AlchemyVision.prototype.imageFaces = function(_params, callback) {
108+
var params = _params || {};
109+
110+
if (typeof(params.image) !== 'string')
111+
params.imagePostMode = 'raw';
112+
113+
return createRequest('image_face_tag').call(this, params, callback);
114+
};
115+
116+
module.exports = AlchemyVision;

0 commit comments

Comments
 (0)