-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpHelper.js
More file actions
executable file
·115 lines (106 loc) · 3.2 KB
/
Copy pathhttpHelper.js
File metadata and controls
executable file
·115 lines (106 loc) · 3.2 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
/**
* A class that handles all HTTP communication with Thingdom.
*/
var https = require( 'https' );
var url = { host: 'api.thingdom.io', path: '/1.1' };
var requestCounter = 0;
/**
* HttpHelper constructor.
*
* @constructor
*/
exports.HttpHelper = function () {
};
/**
* Perform a HTTP GET request.
*
* @param request - A request containing the endpoint and a optional query string.
* @param callback - A callback function that gets invoked with the request results.
*/
exports.HttpHelper.prototype.getData = function ( request, callback ) {
var options = {
host: url.host,
path: url.path + '/' + request
};
doRequest( options, function( result ) {
callback( result );
});
}
/**
* Perform a HTTP POST request.
*
* @param request - A request containing the endpoint and a optional query string.
* @param data - An object containing the data to be posted.
* @param callback - A callback function that gets invoked with the request results.
*/
exports.HttpHelper.prototype.postData = function ( request, data, callback ) {
var options = {
host: url.host,
path: url.path + '/' + request,
method: 'POST',
headers: { 'Content-Type': 'application/json' }
};
//
// Add counter and date/time to each request.
//
data.counter = ++requestCounter;
data.time = getDateAndTime();
doRequest( options, data, function( result ) {
callback( result );
});
}
// ****************************************************************************
// Private Helper Methods
// ****************************************************************************
//
// Perform the HTTP request.
//
var doRequest = function ( options, data, callback ) {
var req = https.request( options, function ( response ) {
var jsonResponse = '';
response.on( 'data', function ( chunk ) {
jsonResponse += chunk;
} );
response.on( 'end', function () {
var objResponse = JSON.parse( jsonResponse );
callback( objResponse );
} );
} );
if ( typeof data == 'function' ) {
callback = data;
}
if ( typeof data == 'object' ) {
var jsonData = JSON.stringify( data );
req.write( jsonData );
}
req.end();
};
//
// Format the current date and time as yyyy/mm/dd hh:mm:ss
//
var getDateAndTime = function () {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var day = '0'+day;
}
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}
var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
return dateTime;
};