-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
57 lines (51 loc) · 1.05 KB
/
service.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
const request = require('request-promise-native');
module.exports.api = (apikey) => {
return endpointReply(apikey);
};
/**
* @param apiKey
* @returns {{send: (function(*=, *=, *=))}}
*/
const endpointReply = (apiKey) => {
return {
send: (method, endpoint, params) => {
return sendRequest(method, endpoint, params, apiKey);
}
};
};
/**
* Send request to reply
* @param method
* @param endpoint
* @param params
* @param apiKey
* @returns {*}
*/
function sendRequest(method, endpoint, params, apiKey) {
if (apiKey === null || apiKey === undefined) {
throw new Error('Missing api key');
}
const options = {
uri: `https://api.reply.io${endpoint}`,
port: 443,
method: method,
pathname: '',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Api-Key': apiKey
}
};
if (options.method === 'GET') {
options['qs'] = params;
} else {
options['body'] = JSON.stringify(params);
}
return request(options).then((data) => {
if (data) {
return JSON.parse(data);
} else {
return null;
}
});
}