forked from AirVantage/zuorajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
74 lines (56 loc) · 1.58 KB
/
http.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
const request = require('request-promise');
const requestBase = require('request');
class ZuoraClientError extends Error {
constructor(message, requestOptions) {
super(message);
this.requestOptions = requestOptions;
}
}
module.exports = (zuoraClient) => {
const baseUrl = `${zuoraClient.config.apiUrl}/${zuoraClient.config.apiVersion}/`;
const doRequest = async (method, url, body) => {
const accessToken = await zuoraClient.auth.getAccessToken();
const headers = {
Authorization: `Bearer ${accessToken}`,
'zuora-version': '215.0',
};
const options = {
method,
baseUrl,
url,
headers,
body,
json: true,
};
const response = await request(options);
if (response.success === false) {
const allErrorStrings = response.reasons.map((reason) => `Code: ${reason.code}: ${reason.message}`).join(' - ');
throw new ZuoraClientError(allErrorStrings, options);
}
return response;
};
const deleteRequest = (url) => doRequest('DELETE', url);
const get = (url) => doRequest('GET', url);
const post = (url, body) => doRequest('POST', url, body);
const put = (url, body) => doRequest('PUT', url, body);
const stream = async (url) => {
const accessToken = await zuoraClient.auth.getAccessToken();
const headers = {
Authorization: `Bearer ${accessToken}`,
'zuora-version': '215.0',
};
const options = {
baseUrl,
url,
headers,
};
return requestBase(options);
};
return {
deleteRequest,
get,
post,
put,
stream,
};
};