-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiRequestHelper.js
78 lines (61 loc) · 2.24 KB
/
apiRequestHelper.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
/*
This program is part of MovieRand (https://github.com/destan/change-it-now)
change-it-now is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
change-it-now is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with change-it-now. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var http = require('http');
var apiKey = "api_key=" + properties.API_KEY;
module.exports = {
httpGetRequest: function (path, callback) {
/* As seen on http://docs.nodejitsu.com/articles/HTTP/clients/how-to-create-a-HTTP-request */
// append api key
if(path.indexOf('?') > -1){
path += '&' + apiKey;
}
else{
path += '?' + apiKey
}
var options = {
headers: {
accept: 'application/json'
},
host: 'api.themoviedb.org',
path: path
};
var internalResponseCallback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
if(typeof callback == 'function'){
//check api specific error and encapsulate it if exists
var apiResponseAsObj = JSON.parse(str);
if(typeof apiResponseAsObj.status_code != 'undefined'){
apiResponseAsObj.success = false;
}
callback(JSON.stringify(apiResponseAsObj));
}
});
};
var request = http.request(options, internalResponseCallback);
request.on('error', function(e) {
console.log("@apiRequestHelper#httpGetRequest: Error: " + e.message);
if(typeof callback == 'function'){
callback(JSON.stringify({"success": false}));
}
});
request.end();
}//end of httpGetRequest
};