Skip to content

Commit

Permalink
fix: update trainer api call uri and add auth key and token
Browse files Browse the repository at this point in the history
  • Loading branch information
tcnguyen committed May 4, 2018
1 parent 1be679e commit f49d76f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 42 deletions.
14 changes: 14 additions & 0 deletions fixtures/api.botfuel.io-443/152544996920928601
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET /trainer/api/v0/classify?sentence=hello
host: api.botfuel.io
accept: application/json

HTTP/1.1 200 OK
server: openresty/1.11.2.5
date: Fri, 04 May 2018 16:06:09 GMT
content-type: application/json
content-length: 118
connection: close
access-control-allow-origin: *
via: 1.1 vegur

[{"id":"5aec7cf6ece6d400110ade38","label":"greetings","probability":1.0,"resolvePrompt":"greetings","type":"Intent"}]
14 changes: 14 additions & 0 deletions fixtures/api.botfuel.io-443/15254499693937262
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET /trainer/api/v0/classify?sentence=delivery
host: api.botfuel.io
accept: application/json

HTTP/1.1 200 OK
server: openresty/1.11.2.5
date: Fri, 04 May 2018 16:06:09 GMT
content-type: application/json
content-length: 217
connection: close
access-control-allow-origin: *
via: 1.1 vegur

[{"answers":[[{"payload":{"value":"Delivery Information"},"type":"text","value":"Delivery Information"}]],"id":"5aec7cdbece6d400110ade33","label":"delivery","probability":1.0,"resolvePrompt":"delivery","type":"Qna"}]
14 changes: 0 additions & 14 deletions fixtures/trainer-api-staging.herokuapp.com-443/152344254134967555

This file was deleted.

14 changes: 0 additions & 14 deletions fixtures/trainer-api-staging.herokuapp.com-443/152344272970966058

This file was deleted.

15 changes: 12 additions & 3 deletions packages/botfuel-dialog/src/nlus/botfuel-trainer-nlu.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ class BotfuelTrainerNlu extends Nlu {
this.extractor = null;

if (!process.env.BOTFUEL_APP_TOKEN) {
throw new SdkError('BOTFUEL_APP_TOKEN are required for using the nlu service');
throw new SdkError('BOTFUEL_APP_TOKEN is required for using the nlu service');
}

if (!process.env.BOTFUEL_APP_ID) {
throw new SdkError('BOTFUEL_APP_ID is required for using the nlu service');
}

if (!process.env.BOTFUEL_APP_KEY) {
throw new SdkError('BOTFUEL_APP_KEY is required for using the nlu service');
}
}

Expand Down Expand Up @@ -89,8 +97,7 @@ class BotfuelTrainerNlu extends Nlu {
const messageEntities = await this.computeEntities(sentence);

// compute intents
let trainerUrl =
process.env.BOTFUEL_TRAINER_API_URL || 'https://trainer-api-staging.herokuapp.com/api/v0';
let trainerUrl = process.env.BOTFUEL_TRAINER_API_URL || 'https://api.botfuel.io/trainer/api/v0';

if (trainerUrl.slice(-1) !== '/') {
trainerUrl += '/';
Expand All @@ -103,6 +110,8 @@ class BotfuelTrainerNlu extends Nlu {
},
headers: {
'Botfuel-Bot-Id': process.env.BOTFUEL_APP_TOKEN,
'App-Id': process.env.BOTFUEL_APP_ID,
'App-Key': process.env.BOTFUEL_APP_KEY,
},
json: true,
};
Expand Down
31 changes: 20 additions & 11 deletions packages/botfuel-dialog/tests/nlus/botfuel-trainer-nlu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ const BotfuelTrainerNlu = require('../../src/nlus/botfuel-trainer-nlu');
const SdkError = require('../../src/errors/sdk-error');

describe('Botfuel Trainer Nlu', () => {
const sandbox = sinon.sandbox.create();
describe('check credentials', () => {
const sandbox = sinon.sandbox.create();

afterEach(() => {
sandbox.restore();
});
afterEach(() => {
sandbox.restore();
});

test('to throw error if missing BOTFUEL_APP_TOKEN', () => {
sandbox.stub(process, 'env').value({ BOTFUEL_APP_ID: '', BOTFUEL_APP_TOKEN: '' });
expect(() => new BotfuelTrainerNlu()).toThrowError(SdkError);
});

test('to throw error if missing BOTFUEL_APP_TOKEN', () => {
sandbox.stub(process, 'env').value({ BOTFUEL_APP_TOKEN: undefined });
expect(() => new BotfuelTrainerNlu()).toThrowError(SdkError);
test('to throw error if missing BOTFUEL_APP_ID', () => {
sandbox.stub(process, 'env').value({ BOTFUEL_APP_KEY: '', BOTFUEL_APP_TOKEN: '' });
expect(() => new BotfuelTrainerNlu()).toThrowError(SdkError);
});

test('to throw error if missing BOTFUEL_APP_KEY', () => {
sandbox.stub(process, 'env').value({ BOTFUEL_APP_ID: '', BOTFUEL_APP_KEY: '' });
expect(() => new BotfuelTrainerNlu()).toThrowError(SdkError);
});
});

// IMPORTANT: for REPLAY record, use SDKTest app with ID = 2ecc774b
describe('compute', () => {
test('to correctly detect intents ', async () => {
// use app token = 1409617651128 to record trainer api response
sandbox.stub(process, 'env').value({ BOTFUEL_APP_TOKEN: '1409617651128' });
const nlu = new BotfuelTrainerNlu();
// fake extractor
nlu.extractor = new CompositeExtractor({
Expand All @@ -47,8 +58,6 @@ describe('Botfuel Trainer Nlu', () => {
});

test('to correctly detect qnas', async () => {
// use app token = 1409617651128 to record trainer api response
sandbox.stub(process, 'env').value({ BOTFUEL_APP_TOKEN: '1409617651128' });
const nlu = new BotfuelTrainerNlu();
// fake extractor
nlu.extractor = new CompositeExtractor({
Expand Down

0 comments on commit f49d76f

Please sign in to comment.