/
ApiBase.js
47 lines (36 loc) · 1001 Bytes
/
ApiBase.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
37
38
39
40
41
42
43
44
45
46
47
'use strict';
const _ = require('lodash');
const debug = require('debug')('voxa');
const rp = require('request-promise');
class ApiBase {
constructor(alexaEvent) {
this.alexaEvent = alexaEvent;
}
getToken() {
return _.get(this.alexaEvent, 'context.System.apiAccessToken');
}
getEndpoint() {
return _.get(this.alexaEvent, 'context.System.apiEndpoint');
}
getResult(path = '', method = 'GET', body = undefined) {
const options = {
uri: `${this.getEndpoint()}/${path}`,
method,
body,
headers: {
Authorization: `Bearer ${this.getToken()}`,
},
json: true, // Automatically parses the JSON string in the response
};
return rp(options);
}
checkError(err) {
debug(`${this.tag} Error %s`, JSON.stringify(err, null, 2));
if (err.statusCode === this.errorCodeSafeToIgnore ||
err.error.code === this.errorCodeSafeToIgnore) {
return undefined;
}
throw err;
}
}
module.exports = ApiBase;