-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.js
More file actions
97 lines (85 loc) · 3.68 KB
/
Client.js
File metadata and controls
97 lines (85 loc) · 3.68 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
var querystring = require('querystring');
var syncRequest = require('sync-request');
var validUrl = require('valid-url');
var js2xmlparser = require('js2xmlparser');
module.exports = class Client {
constructor() {
this.request = {
"endpoint": "",
"headers": "",
"method": "",
"path": "",
"queryParameters": {},
"body": ""
};
this.response = {
"statusCode": "",
"headers": "",
"body": "",
"rawbody": "",
"raw": ""
};
}
setEndpoint(endpoint) {
this.request.endpoint = endpoint;
}
setRequestData(request) {
// this.request.url = '';
this.request.method = request.method || undefined;
this.request.path = request.path || undefined;
this.request.headers = request.headers || '';
this.request.queryParameters = request.queryParameters || {};
this.request.body = request.body || '';
}
send() {
// make a querystring
var queryParamsString = querystring.stringify(this.request.queryParameters);
// set the request body
// this.request.body = querystring.stringify(this.request.body); // WTF?
if (this.request.headers['content-type'] == 'application/json') {
this.request.body = JSON.stringify(this.request.body);
}
// make a url
this.request.url = this.request.endpoint + this.request.path + '?' + queryParamsString;
// console.log(JSON.stringify(this.request, null, 4)); // for debug
if (validUrl.isUri(this.request.url)){
// send request
var rawResponse = syncRequest(this.request.method, this.request.url, {
// timeout: 200000,
headers: this.request.headers,
json: this.request.jsonbody,
body: this.request.body
});
try {
// this.response.value = new Buffer(res.getBody(), 'base64').toString('ascii'); // use it if you want an error on invalid status codes
this.response.raw = rawResponse;
this.response.rawbody = this.response.body = this.response.raw.body.toString();
this.response.statusCode = this.response.raw.statusCode;
this.response.headers = this.response.raw.headers;
} catch (err) {
// errorDetails += "\nresponse body: ", new Buffer(res.body, 'base64').toString('ascii');
throw new Error("\nerror while sending the request:\n" + this.response.raw + '\n' + err);
} finally {
var body;
try {
// delete === from response
body = this.response.body.replace(/([\s]?===[\s]?)+/g, '');
// parse response to JSON
this.response.json = JSON.parse(body);
// parse response to XML
this.response.xml = js2xmlparser("response", this.response.json);
} catch (err) {
// if (this.response.value != undefined && this.response.value != '') {
// console.log("\terror while parsing the response: probably the response is html page");
this.response.xml = body;
this.response.json = body;
// }
}
rawResponse = null;
}
} else {
// console.log(`BROKEN URL: ${this.request.url}`);
throw new Error(`BROKEN URL: ${this.request.url}`);
}
}
};