-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyRequestClient.js
78 lines (67 loc) · 1.91 KB
/
MyRequestClient.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
const _ = require('lodash');
const qs = require('qs');
const axios = require('axios');
/**
* Custom HTTP Client
* Based on: /twilio/lib/base/RequestClient.js
*/
class MyRequestClient {
constructor(timeout) {
this.timeout = timeout;
}
request(opts) {
opts = opts || {};
if (!opts.method) {
throw new Error('http method is required');
}
if (!opts.uri) {
throw new Error('uri is required');
}
// Axios auth option will use HTTP Basic auth by default
if (opts.username && opts.password) {
this.auth = {
username: opts.username,
password: opts.password,
};
}
// Options for axios config
const options = {
url: opts.uri,
method: opts.method,
headers: opts.headers,
auth: this.auth,
timeout: this.timeout,
};
// Use 'qs' to support x-www-form-urlencoded with axios
// Construct data request body option for axios config
if (!_.isNull(opts.data)) {
options.headers = { 'content-type': 'application/x-www-form-urlencoded' };
options.data = qs.stringify(opts.data, { arrayFormat: 'repeat' });
}
// Use 'qs' to support x-www-form-urlencoded with axios
// Construct URL params option for axios config
if (!_.isNull(opts.params)) {
options.params = opts.params;
options.paramsSerializer = (params) => {
return qs.stringify(params, { arrayFormat: 'repeat' });
};
}
return axios(options)
.then((response) => {
if (opts.logLevel === 'debug') {
console.log(`response.statusCode: ${response.status}`);
console.log(`response.headers: ${JSON.stringify(response.headers)}`);
}
return {
statusCode: response.status,
body: response.data,
};
})
.catch((error) => {
console.error(error);
throw error;
});
}
}
module.exports = MyRequestClient;