-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectionManager.js
99 lines (92 loc) · 2.78 KB
/
connectionManager.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
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
/*
* Connection Manager wrapper for https connections
* @class ConnectionManager
* @author Nazar Kulyk
* @version 1.0
*/
var ConnectionManager = function(options) {
this.transport = require('https');
this.options = options || null;
};
ConnectionManager.VERSION = "1.0";
/*
* Catch data sended as response from http connection
* @method _onGetResponse
* @private
* @param {Object} res http response
* @param {Function} callback that should be called after response
*/
ConnectionManager.prototype._onGetResponse = function(res, callback) {
var data = "";
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
if(res.statusCode === 200) {
var obj = null;
try{
obj = JSON.parse(data);
}catch(e) {
console.log(e);
}
process.nextTick(function() {
callback(obj);
});
} else throw new Error("Transport return code that is not ok : " + res.statusCode + "\n" + data);
});
};
/*
* Change http options, see nodejs doc for more info about options
* @method setOptions
* @param {Object} options http options object
*/
ConnectionManager.prototype.setOptions = function(options) {
this.options = options;
};
/*
* Start request and pass response to callback
* @method get
* @param {String} path relative url used for request
* @param {Object} args
* @param {Function} callback should be called if everything goes as expected
* @param {Boolean} post make post request
*/
ConnectionManager.prototype.get = function(path, args, callback, post) {
var options = this.options,
req, reqString;
if(!options)
throw new Error("Transport options has to be filled.");
if(post)
options.method = "POST";
if(!post && args) {
reqString = require('querystring').stringify(args);
}
options.path = reqString ? path + "?" + reqString: path;
console.log(options.path);
options.headers["Content-Length"] = 0;
req = this.transport.request(options, function(res) {
this._onGetResponse(res, callback);
}.bind(this)).on('error', function(e){
throw e;
});
req.end();
};
/*
* Start request and pass response to callback
* @method get
* @param {String} path relative url used for request
* @param {Object} args
* @param {Function} callback should be called if everything goes as expected
*/
ConnectionManager.prototype.post = function(path, args, callback) {
this.get(path, args, callback, true);
};
/*
* Return string for debug and trace
* @method toString
* @return {String} ID of the class
*/
ConnectionManager.prototype.toString = function() {
return "ConnectionManager v."+ConnectionManager.VERSION;
};
module.exports = ConnectionManager;