-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpClient.js
More file actions
144 lines (130 loc) · 6.84 KB
/
Copy pathhttpClient.js
File metadata and controls
144 lines (130 loc) · 6.84 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const axios = require('axios');
const governify = require('./index');
const utils = require('./utils');
const logger = governify.getLogger().tag(["commons", "http-request"]);
let requestLoggingEnabled = (process.env['GOV_LOG_REQUESTS'] ? process.env['GOV_LOG_REQUESTS'] === 'true' : true);
const serviceName = utils.getServiceName();
function appendCommonsLog(logTag, method, URL, responseStatus) {
if (requestLoggingEnabled && governify.isReady()) {
let logMsg = '[' + logTag + '] ' + serviceName + ' => ' + '[' + method + ']' + ' => ' + URL + ' => ' + responseStatus + '\n';
axios.patch(governify.infrastructure.getServiceURL('internal.assets') + '/api/v1/public/logs/commons.log',
{ operation: 'append', content: logMsg}
).catch(async (err) => {
if (err.response && err.response.status === 404){
axios.post(governify.infrastructure.getServiceURL('internal.assets') + '/api/v1/public/logs/commons.log?createDirectories=true','')
.catch(() => logger.error('Failed creating commons.log in assetsmanager'));
}
});
logger.debug(logMsg)
}
}
module.exports = async function (config) {
return axios(config).then(response => {
appendCommonsLog('HTTPRequest OK', config.method, config.url, response.status);
logger.debug('HTTP Request OK with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', config.method, config.url, status);
logger.error('Failed when calling service from Governify with config:', JSON.stringify(config))
throw err;
});
}
module.exports.request = async function request(config) {
return axios.request(config).then(response => {
appendCommonsLog('HTTPRequest OK', config.method, config.url, response.status);
logger.debug('HTTP Request OK (' + config.method + ') with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', config.method, config.url, status);
logger.error('Failed when calling service from Governify with config:', JSON.stringify(config))
throw err;
});
}
module.exports.get = async function get(url, config = {}) {
return axios.get(url, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'GET', url, response.status);
logger.debug('HTTP Request OK (GET) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'GET', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.delete = async function deleteF(url, config = {}) {
return axios.deleteF(url, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'DELETE', url, response.status);
logger.debug('HTTP Request OK (DELETE) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'DELETE', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.head = async function head(url, config = {}) {
return axios.head(url, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'HEAD', url, response.status);
logger.debug('HTTP Request OK (HEAD) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'HEAD', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.options = async function options(url, config = {}) {
return axios.options(url, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'OPTIONS', url, response.status);
logger.debug('HTTP Request OK (OPTIONS) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'OPTIONS', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.post = async function post(url, data = {}, config = {}) {
return axios.post(url, data, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'POST', url, response.status);
logger.debug('HTTP Request OK (POST) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'POST', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.put = async function put(url, data = {}, config = {}) {
return axios.put(url, data, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'PUT', url, response.status);
logger.debug('HTTP Request OK (PUT) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'PUT', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.patch = async function patch(url, data = {}, config = {}) {
return axios.patch(url, data, config).then(response => {
appendCommonsLog('HTTPRequest OK', 'PATCH', url, response.status);
logger.debug('HTTP Request OK (PATCH) with config: ', JSON.stringify(config));
return response;
}).catch(err => {
let status = (err && err.response) ? err.response.status : 'No error response';
appendCommonsLog('HTTPRequest FAIL', 'PATCH', url, status);
logger.error('Failed when calling service from Governify: ', url, ' with config:', JSON.stringify(config))
throw err;
});
}
module.exports.getRequestLogging = () => { return requestLoggingEnabled };
module.exports.setRequestLogging = (value) => { requestLoggingEnabled = value };