forked from rahatarmanahmed/node-wolfram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (33 loc) · 999 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fetch = require('node-superfetch');
const xml2js = require('xml2js');
class Client {
constructor(appId) {
this.appId = appId;
}
query(query, callback) {
if (this.appId == null)
throw new Error('Cannot query without App ID');
const opt = { appid: this.appId };
if (query instanceof Object) {
for (const key in query) {
const value = query[key];
opt[key] = value;
}
} else {
opt.input = query;
}
fetch.get('http://api.wolframalpha.com/v2/query').query(opt).then(data => {
if (data.status !== 200)
return callback(`Error ${data.status}: ${data.statusText}`, null);
return xml2js.parseString(data.body, function(err, result) {
if (err != null)
return callback(err, result);
else if (result.queryresult.$.error)
return callback(result.queryresult.error, result);
else
return callback(null, result);
});
});
}
}
module.exports = Client;