Skip to content

Commit 5e43d13

Browse files
Check the status response from alchemy to detect errors #72
1 parent a10ad42 commit 5e43d13

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ APIs and SDKs that use cognitive computing to solve complex problems.
1818
* [Installation](#installation)
1919
* [Usage](#usage)
2020
* [Getting the Service Credentials](#getting-the-service-credentials)
21+
* [Alchemy Services](#alchemy-services)
22+
* [Alchemy Language](#alchemy-language)
2123
* [IBM Watson Services](#ibm-watson-services)
2224
* [Authorization](#authorization)
2325
* [Concept Expansion](#concept-expansion)
@@ -63,9 +65,7 @@ credentials; the wrapper will get them for you by looking at the `VCAP_SERVICES`
6365
environment variable.
6466

6567
### Getting the Service Credentials
66-
The credentials for the services are stored in the
67-
`VCAP_SERVICES` environment variable. To get them, you need
68-
to first create and bind the service to your application.
68+
The credentials for the services are stored in the `VCAP_SERVICES` environment variable. To get them, you need to first create and bind the service to your application.
6969

7070
There are two ways to get the credentials. You can use Bluemix to access your
7171
app and view the `VCAP_SERVICES` there or you can run:
@@ -95,6 +95,34 @@ Example output:
9595

9696
You need to copy `username` and `password`.
9797

98+
For Alchemy you only need an `api_key`, you can register for one [here](http://www.alchemyapi.com/api/register.html).
99+
100+
## Alchemy APIs
101+
102+
### Alchemy Language
103+
[Alchemy Language](alchemy_language) offers 12 API functions as part of its text analysis service, each of which uses sophisticated natural language processing techniques to analyze your content and add high-level semantic information.
104+
105+
Use the [Sentiment Analysis](sentiment_analysis) endpoint to identify positive/negative sentiment within a sample text document.
106+
107+
```javascript
108+
var watson = require('watson-developer-cloud');
109+
110+
var alchemy_language = watson.alchemy_language({
111+
api_key: '<api_key>'
112+
});
113+
114+
var params = {
115+
text: 'IBM Watson won the Jeopardy television show hosted by Alex Trebek'
116+
};
117+
118+
alchemy_language.sentiment(params, function (err, response) {
119+
if (err)
120+
console.log('error:', err);
121+
else
122+
console.log(JSON.stringify(response, null, 2));
123+
});
124+
```
125+
98126
## IBM Watson Services
99127
The Watson Developer Cloud offers a variety of services for building cognitive
100128
apps.
@@ -568,6 +596,8 @@ See [CONTRIBUTING](https://github.com/watson-developer-cloud/nodejs-wrapper/blob
568596
[tradeoff_analytics]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/tradeoff-analytics/
569597
[language_translation]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/language-translation/
570598

599+
[alchemy_language]: http://www.alchemyapi.com/products/alchemylanguage
600+
[sentiment_analysis]: http://www.alchemyapi.com/products/alchemylanguage/sentiment-analysis
571601
[wdc]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/
572602
[bluemix]: https://console.ng.bluemix.net
573603
[npm_link]: https://www.npmjs.com/package/watson-developer-cloud

examples/alchemy_language.v1.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
var watson = require('watson-developer-cloud');
4+
5+
var alchemy_language = watson.alchemy_language({
6+
api_key: '<api_key>'
7+
});
8+
9+
var params = {
10+
text: 'IBM Watson won the Jeopardy television show hosted by Alex Trebek'
11+
};
12+
13+
alchemy_language.sentiment(params, function (err, response) {
14+
if (err)
15+
console.log('error:', err);
16+
else
17+
console.log(JSON.stringify(response, null, 2));
18+
});

lib/requestwrapper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ function createRequest(parameters, callback) {
106106

107107
if (typeof(options.api_key) !== 'undefined') {
108108
// Alchemy uses the apikey(a.k.a api_key) as query parameter
109-
if (options.alchemy){
109+
if (options.alchemy) {
110110
options.qs = extend({ apikey : options.api_key }, options.qs);
111+
delete options.alchemy;
111112
} else {
112113
// IBM Watson uses Basic auth
113114
options.headers['Authorization'] = 'Basic ' + options.api_key;

services/alchemy_language/v1.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ var requestFactory = require('../../lib/requestwrapper');
2121
var endpoints = require('../../lib/alchemy_endpoints.json');
2222
var helper = require('../../lib/helper');
2323

24+
var errorFormatter = function(cb) {
25+
return function(err, result) {
26+
if (err) {
27+
cb(err, result);
28+
}
29+
else {
30+
if (result.status === 'OK')
31+
cb(err,result);
32+
else
33+
cb({error: result.statusInfo, code:400 }, null);
34+
}
35+
};
36+
};
37+
2438
function createRequest(method) {
2539
return function(_params, callback ) {
2640
var params = _params || {};
@@ -44,15 +58,14 @@ function createRequest(method) {
4458
},
4559
defaultOptions: this._options
4660
};
47-
return requestFactory(parameters, callback);
61+
return requestFactory(parameters, errorFormatter(callback));
4862
};
4963
}
5064

5165
function AlchemyLanguage(options) {
5266
// Default URL
5367
var serviceDefaults = {
54-
url: 'https://access.alchemyapi.com/calls',
55-
alchmemy: true
68+
url: 'https://access.alchemyapi.com/calls'
5669
};
5770
// Replace default options with user provided
5871
this._options = extend(serviceDefaults, options);
@@ -78,7 +91,7 @@ AlchemyLanguage.prototype.concepts = createRequest('concepts');
7891
* Calculates the sentiment for text, a URL or HTML.
7992
*/
8093
AlchemyLanguage.prototype.sentiment = function(params, callback) {
81-
var service = (params && params.target) ? 'sentiment' : 'sentiment_targeted';
94+
var service = (params && params.target) ? 'sentiment_targeted' : 'sentiment';
8295
return createRequest(service).call(this, params, callback);
8396
};
8497
/**

0 commit comments

Comments
 (0)