1 | const request = require('requestretry');
|
2 | const util = require('util');
|
3 | const SriClient = require('../sri-client.js');
|
4 | const commonUtils = require('../common-utils');
|
5 | const SriClientError = require('../sri-client-error');
|
6 |
|
7 | class NodeClient extends SriClient {
|
8 |
|
9 | constructor(config = {}) {
|
10 | super(config);
|
11 | const defaultOptions = {
|
12 | headers: config.headers || {}
|
13 | };
|
14 | if(config.username && config.password) {
|
15 | defaultOptions.auth = {
|
16 | user: config.username,
|
17 | pass: config.password
|
18 | };
|
19 | }
|
20 | if(config.accessToken) {
|
21 | defaultOptions.headers[config.accessToken.name] = config.accessToken.value;
|
22 | }
|
23 | if(config.timeout) {
|
24 | defaultOptions.timeout = config.timeout;
|
25 | } else {
|
26 | defaultOptions.timeout = 30000;
|
27 | }
|
28 | this.baseRequest = request.defaults(defaultOptions);
|
29 | this.cache.initialise();
|
30 | }
|
31 |
|
32 | getRaw(href, params, options = {}) {
|
33 | var baseUrl = this.getBaseUrl(options);
|
34 | const logging = options.logging || this.configuration.logging;
|
35 | if(/get/.test(logging)) {
|
36 | console.log('GET ' + baseUrl + commonUtils.parametersToString(href, params));
|
37 | }
|
38 | var stack = new Error().stack;
|
39 | return new Promise((resolve, reject) => {
|
40 | this.baseRequest({
|
41 | method: 'GET',
|
42 | url: baseUrl + commonUtils.parametersToString(href, params),
|
43 |
|
44 | json: true,
|
45 | maxAttempts: options.maxAttempts || 3,
|
46 | retryDelay: options.retryDelay || 5000,
|
47 | retryStrategy: options.retryStrategy || request.RetryStrategies.HTTPOrNetworkError,
|
48 | delayStrategy: options.delayStrategy,
|
49 | headers: options.headers,
|
50 | timeout: options.timeout || 10000
|
51 | }, (error, response, body) => {
|
52 | if(!error && response.statusCode >= 200 && response.statusCode < 400) {
|
53 | resolve(response.body);
|
54 | } else {
|
55 | reject(new SriClientError(this.handleError('GET ' + baseUrl + commonUtils.parametersToString(href, params), error, response, options, stack)));
|
56 | }
|
57 | });
|
58 | });
|
59 | }
|
60 |
|
61 | sendPayload(href, payload, options = {}, method) {
|
62 | const baseUrl = this.getBaseUrl(options);
|
63 | const logging = options.logging || this.configuration.logging;
|
64 | if((new RegExp(method.toLowerCase)).test(logging)) {
|
65 | console.log(method + ' ' + baseUrl + href + ':\n' + JSON.stringify(payload));
|
66 | }
|
67 | if(options.strip$$Properties !== false) {
|
68 | payload = commonUtils.strip$$Properties(payload);
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | }
|
75 | return new Promise((resolve, reject) => {
|
76 | this.baseRequest({
|
77 | method: method,
|
78 | url: baseUrl + href,
|
79 | body: payload,
|
80 | json:true,
|
81 | maxAttempts: options.maxAttempts || 3,
|
82 | retryDelay: options.retryDelay || 5000,
|
83 | retryStrategy: options.retryStrategy || request.RetryStrategies.HTTPOrNetworkError,
|
84 | delayStrategy: options.delayStrategy,
|
85 | headers: options.headers,
|
86 | timeout: options.timeout || (payload instanceof Array ? 120000 : 30000)
|
87 | }, (error, response) => {
|
88 | if(!error && response.statusCode >= 200 && response.statusCode < 400) {
|
89 | const body = (response && response.body && typeof response.body === 'object') ? response.body : {};
|
90 |
|
91 | body.getResponseHeader = function(header) {
|
92 | return response.headers[header];
|
93 | };
|
94 | body.getStatusCode = function() {
|
95 | return response.statusCode;
|
96 | };
|
97 | resolve(body);
|
98 | } else {
|
99 | reject(new SriClientError(this.handleError(method + ' ' + baseUrl + href, error, response, options)));
|
100 | }
|
101 | });
|
102 | });
|
103 | }
|
104 |
|
105 | delete(href, options) {
|
106 | const baseUrl = this.getBaseUrl(options);
|
107 | return new Promise((resolve, reject) => {
|
108 | this.baseRequest({
|
109 | method: 'DELETE',
|
110 | url: baseUrl + href,
|
111 | json:true,
|
112 | maxAttempts: options.maxAttempts || 3,
|
113 | retryDelay: options.retryDelay || 5000,
|
114 | retryStrategy: options.retryStrategy || request.RetryStrategies.HTTPOrNetworkError,
|
115 | headers: options.headers,
|
116 | timeout: options.timeout || 30000
|
117 | }, (error, response) => {
|
118 | if(!error && response.statusCode >= 200 && response.statusCode < 400) {
|
119 | resolve(response.body);
|
120 | } else {
|
121 | reject(new SriClientError(this.handleError('DELETE ' + baseUrl + href, error, response, options)));
|
122 | }
|
123 | });
|
124 | });
|
125 | }
|
126 |
|
127 | handleError(httpRequest, error, response = {}, options, stack) {
|
128 | const logging = options.logging || options.logging === false ? options.logging : this.configuration.logging;
|
129 | if(logging) {
|
130 | console.error(response.statusCode + ': An error occured for ' + httpRequest);
|
131 | if(response.body) {
|
132 | console.error(util.inspect(response.body, {depth: 7}));
|
133 | } else {
|
134 | console.error(error);
|
135 | }
|
136 | }
|
137 | return {
|
138 | status: response.statusCode || null,
|
139 | body: response.body || null,
|
140 | headers: response.headers || null,
|
141 | error,
|
142 | stack,
|
143 | };
|
144 | };
|
145 | };
|
146 |
|
147 |
|
148 |
|
149 | module.exports = function(configuration) {
|
150 | return new NodeClient(configuration);
|
151 | };
|