1 | const request = require('request');
|
2 | const requestPN = require('request-promise-native');
|
3 | const { getPortalConfig } = require('./lib/config');
|
4 | const { getRequestOptions } = require('./http/requestOptions');
|
5 | const { accessTokenForPersonalAccessKey } = require('./personalAccessKey');
|
6 | const { getOauthManager } = require('./oauth');
|
7 |
|
8 | const withOauth = async (portalId, portalConfig, requestOptions) => {
|
9 | const { headers } = requestOptions;
|
10 | const oauth = getOauthManager(portalId, portalConfig);
|
11 | const accessToken = await oauth.accessToken();
|
12 | return {
|
13 | ...requestOptions,
|
14 | headers: {
|
15 | ...headers,
|
16 | Authorization: `Bearer ${accessToken}`,
|
17 | },
|
18 | };
|
19 | };
|
20 |
|
21 | const withPersonalAccessKey = async (
|
22 | portalId,
|
23 | portalConfig,
|
24 | requestOptions
|
25 | ) => {
|
26 | const { headers } = requestOptions;
|
27 | const accessToken = await accessTokenForPersonalAccessKey(portalId);
|
28 | return {
|
29 | ...requestOptions,
|
30 | headers: {
|
31 | ...headers,
|
32 | Authorization: `Bearer ${accessToken}`,
|
33 | },
|
34 | };
|
35 | };
|
36 |
|
37 | const withPortalId = (portalId, requestOptions) => {
|
38 | const { qs } = requestOptions;
|
39 |
|
40 | return {
|
41 | ...requestOptions,
|
42 | qs: {
|
43 | ...qs,
|
44 | portalId,
|
45 | },
|
46 | };
|
47 | };
|
48 |
|
49 | const withAuth = async (portalId, options) => {
|
50 | const portalConfig = getPortalConfig(portalId);
|
51 | const { env, authType, apiKey } = portalConfig;
|
52 | const requestOptions = withPortalId(
|
53 | portalId,
|
54 | getRequestOptions({ env }, options)
|
55 | );
|
56 |
|
57 | if (authType === 'personalaccesskey') {
|
58 | return withPersonalAccessKey(portalId, portalConfig, requestOptions);
|
59 | }
|
60 |
|
61 | if (authType === 'oauth2') {
|
62 | return withOauth(portalId, portalConfig, requestOptions);
|
63 | }
|
64 | const { qs } = requestOptions;
|
65 |
|
66 | return {
|
67 | ...requestOptions,
|
68 | qs: {
|
69 | ...qs,
|
70 | hapikey: apiKey,
|
71 | },
|
72 | };
|
73 | };
|
74 |
|
75 | const addQueryParams = (requestOptions, params = {}) => {
|
76 | const { qs } = requestOptions;
|
77 | return {
|
78 | ...requestOptions,
|
79 | qs: {
|
80 | ...qs,
|
81 | ...params,
|
82 | },
|
83 | };
|
84 | };
|
85 |
|
86 | const getRequest = async (portalId, options) => {
|
87 | const { query, ...rest } = options;
|
88 | const requestOptions = addQueryParams(rest, query);
|
89 | return requestPN.get(await withAuth(portalId, requestOptions));
|
90 | };
|
91 |
|
92 | const postRequest = async (portalId, options) => {
|
93 | return requestPN.post(await withAuth(portalId, options));
|
94 | };
|
95 |
|
96 | const putRequest = async (portalId, options) => {
|
97 | return requestPN.put(await withAuth(portalId, options));
|
98 | };
|
99 |
|
100 | const deleteRequest = async (portalId, options) => {
|
101 | return requestPN.del(await withAuth(portalId, options));
|
102 | };
|
103 |
|
104 | const createGetRequestStream = ({ contentType }) => async (
|
105 | portalId,
|
106 | options,
|
107 | destination
|
108 | ) => {
|
109 | const { query, ...rest } = options;
|
110 | const requestOptions = addQueryParams(rest, query);
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | return new Promise(async (resolve, reject) => {
|
117 | try {
|
118 | const { headers, ...opts } = await withAuth(portalId, requestOptions);
|
119 | const req = request.get({
|
120 | ...opts,
|
121 | headers: {
|
122 | ...headers,
|
123 | 'content-type': contentType,
|
124 | accept: contentType,
|
125 | },
|
126 | json: false,
|
127 | });
|
128 | let response;
|
129 | req
|
130 | .on('error', reject)
|
131 | .on('response', r => {
|
132 | if (r.statusCode >= 200 && r.statusCode < 300) {
|
133 | response = r;
|
134 | } else {
|
135 | reject(r);
|
136 | }
|
137 | })
|
138 | .on('end', () => resolve(response))
|
139 | .pipe(destination);
|
140 | } catch (err) {
|
141 | reject(err);
|
142 | }
|
143 | });
|
144 | };
|
145 |
|
146 | module.exports = {
|
147 | getRequestOptions,
|
148 | request: requestPN,
|
149 | get: getRequest,
|
150 | getOctetStream: createGetRequestStream({
|
151 | contentType: 'application/octet-stream',
|
152 | }),
|
153 | post: postRequest,
|
154 | put: putRequest,
|
155 | delete: deleteRequest,
|
156 | };
|