Skip to content

Commit

Permalink
use node-fetch instead of requests (wit-ai#16)
Browse files Browse the repository at this point in the history
Using the standard fetch API will make it easier to go cross-engine in the future
  • Loading branch information
amowu authored and blandinw committed Apr 27, 2016
1 parent 549bf5a commit d4e748e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 42 deletions.
103 changes: 63 additions & 40 deletions lib/wit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const request = require('request');
const fetch = require('node-fetch');
const readline = require('readline');
const uuid = require('node-uuid');
const Logger = require('./logger').Logger;
Expand All @@ -11,29 +11,42 @@ const CALLBACK_TIMEOUT_MS = 10000;

let l = new Logger(logLevels.LOG);

const makeWitResponseHandler = (endpoint, l, cb) => (
(error, response, data) => {
const err = error ||
data.error ||
response.statusCode !== 200 && data.body + ' (' + response.statusCode + ')'
;
const makeWitResponseHandler = (endpoint, l, cb) => {
const error = err => {
l.error('[' + endpoint + '] Error: ' + err);
if (cb) {
process.nextTick(() => {
cb(err);
});
}
};

return rsp => {
if (rsp instanceof Error) {
return error(rsp);
}

const json = rsp[0];
const status = rsp[1];

if (json instanceof Error) {
return error(json);
}

const err = json.error || status !== 200 && json.body + ' (' + status + ')';

if (err) {
l.error('[' + endpoint + '] Error: ' + err);
if (cb) {
process.nextTick(() => {
cb(err);
});
}
return;
return error(err)
}
l.debug('[' + endpoint + '] Response: ' + JSON.stringify(data));

l.debug('[' + endpoint + '] Response: ' + JSON.stringify(json));
if (cb) {
process.nextTick(() => {
cb(null, data);
cb(null, json);
});
}
}
);
};

const validateActions = (actions) => {
const learnMore = 'Learn more at https://wit.ai/docs/quickstart';
Expand Down Expand Up @@ -101,42 +114,52 @@ const clone = (obj) => {
};

const Wit = function(token, actions, logger) {
this.req = request.defaults({
baseUrl: process.env.WIT_URL || 'https://api.wit.ai',
strictSSL: false,
json: true,
headers: {
'Authorization': 'Bearer ' + token,
},
});
const baseURL = process.env.WIT_URL || 'https://api.wit.ai';
const headers = {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json',
'Content-Type': 'application/json',
};
if (logger) {
l = logger;
}
this.actions = validateActions(actions);

this.message = (message, context, cb) => {
const options = {
uri: '/message',
method: 'GET',
qs: { q: message },
};
if (typeof context === 'function') {
cb = context;
context = undefined;
}
let qs = 'q=' + encodeURIComponent(message);
if (context) {
options.qs.context = JSON.stringify(context);
qs += '&context=' + encodeURIComponent(JSON.stringify(context));
}
this.req(options, makeWitResponseHandler('message', l, cb));
const handler = makeWitResponseHandler('message', l, cb);
fetch(baseURL + '/message?' + qs, {
method: 'GET',
headers: headers,
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
.catch(handler)
;
};

this.converse = (sessionId, message, context, cb) => {
const options = {
uri: '/converse',
method: 'POST',
qs: { 'session_id': sessionId },
json: context,
};
const handler = makeWitResponseHandler('converse', l, cb);
let qs = 'session_id=' + sessionId;
if (message) {
options.qs.q = message;
qs += '&q=' + encodeURIComponent(message);
}
this.req(options, makeWitResponseHandler('converse', l, cb));
fetch(baseURL + '/converse?' + qs, {
method: 'POST',
headers: headers,
body: JSON.stringify(context),
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
.catch(handler)
;
};

const makeCallback = (i, sessionId, message, context, cb) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"repository": "https://github.com/wit-ai/node-wit",
"author": "Julien Odent <[email protected]>",
"dependencies": {
"node-uuid": "^1.4.7",
"request": "^2.69.0"
"node-fetch": "^1.5.1",
"node-uuid": "^1.4.7"
}
}

0 comments on commit d4e748e

Please sign in to comment.