This repository has been archived by the owner on Oct 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
187 lines (162 loc) · 5.33 KB
/
main.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var https = require('https');
var url = require('url');
var settings = {
googleApiHostUrl: 'www.googleapis.com',
googleDisoveryApiVersion: 'v1',
apiKey: null,
debugMode: false
};
/****************
* node-google-api
*/
module.exports = function(key) {
if(key){
if(typeof key === 'string'){
settings.apiKey = key;
} else if(typeof key === 'object') {
settings.apiKey = key.apiKey || settings.apiKey;
settings.debugMode = key.debugMode || settings.debugMode;
settings.googleApiHostUrl = key.googleApiHostUrl || settings.googleApiHostUrl;
settings.googleDisoveryApiVersion = key.googleDisoveryApiVersion || settings.googleDisoveryApiVersion;
} else {
throw new Error('A parameter of type "' + typeof key + '" is not permitted.');
}
} else {
throw new Error('An API key is required to use this module: https://code.google.com/apis/console');
}
return {
build: function(options, callback) {
if(typeof options == 'function') {
callback = options;
options = {};
}
listAPIs(options, function(apiList) {
buildAPI(apiList, function(apiObj) {
// Removed them, they were confusing to see in the list of API's.
//apiObj.listAPIs = listAPIs;
//apiObj.getAPI = getRest;
//apiObj.request = sendRequest;
callback(apiObj);
});
});
}
};
}
// TODO: buildAPI is a big ugly method, I will break this down to organize and improve.
var buildAPI = function(apiList, callback) {
var remaining = apiList.length;
var apiObj = {};
for(var api in apiList) {
getRest(apiList[api], function(restAPI) {
var hostURL = restAPI.baseUrl;
apiObj[restAPI.name] = {};
// For each item in the API...
for(var resource in restAPI.resources) {
apiObj[restAPI.name][resource] = {};
// For each method in the item...
for(var method in restAPI.resources[resource].methods) {
// Make the method a usable function...
apiObj[restAPI.name][resource][method] = (function() {
var thisMethod = method;
return function (options, cb) {
if(!options)
options = {};
if(typeof cb == 'undefined'){
cb = function(){};
if(typeof options === 'function') {
cb = options;
options = {};
}
}
var query = '?key='+ settings.apiKey;
// Insert the URL variables...
var path = this[thisMethod].vars.path;
for(var term in options) {
if(path.indexOf(term) >= 0) {
path = path.replace('{'+term+'}', encodeURIComponent(options[term]));
} else {
query += '&' + term + '=' + encodeURIComponent(options[term]);
}
}
var parsedUrl = url.parse(hostURL + path + query);
sendRequest({
host: parsedUrl.hostname,
path: parsedUrl.path,
port: parsedUrl.port,
method: this[thisMethod].vars.httpMethod
}, function(data) {
if(cb) cb(data);
});
}
})();
apiObj[restAPI.name][resource][method].vars = restAPI.resources[resource].methods[method];
}
}
if(--remaining == 0) {
callback(apiObj);
}
});
}
}
module.exports.list = listAPIs = function (options, callback) {
if(typeof options == 'function') {
callback = options;
options = {};
}
var query = '?fields=items(discoveryRestUrl%2Cname%2Cversion)';
if(options) {
for(var o in options) {
if(options[o]) {
query += '&'+ o + '=' + options[o];
}
}
}
sendRequest({
host: settings.googleApiHostUrl,
path: '/discovery/' + settings.googleDisoveryApiVersion + '/apis'+query,
method: 'GET'
}, function(data) {
if(data.items) {
callback(data.items);
}
});
}
module.exports.getAPI = getRest = function(discoveryItem, callback) {
// baseUrl, resources/*/methods/*/path, resources/*/methods/*/httpMethod
var query = '?fields=name%2CbaseUrl%2C+resources%2F*%2Fmethods%2F*%2Fpath%2C+resources%2F*%2Fmethods%2F*%2FhttpMethod';
var apiURL = url.parse(discoveryItem.discoveryRestUrl + query);
sendRequest({
host: apiURL.hostname,
path: apiURL.path,
port: apiURL.port,
method: 'GET'
}, function(data) {
callback(data);
});
}
// Make the Request function externally available for easier testing...
module.exports.request = sendRequest = function (options, callback) {
https.request(options, function(res) {
res.setEncoding('utf8');
//console.log(options.host + options.path);
var result = '';
res.on('data', function (chunk) {
result += chunk;
}).on('end', function () {
var data = JSON.parse(result);
if(data.error && settings.debugMode){
throwResponseException(data);
}
if(callback) callback(data);
});
}).end();
}
function throwResponseException(errorResult){
var errorMessage = errorResult.error.code + ': ' + errorResult.error.message;
var stackTrace = '';
for(var e in errorResult.error.errors){
var error = errorResult.error.errors[e];
stackTrace += '[' + error.domain + '] ' + error.reason + ' -> ' + error.message + '\n';
}
throw new Error(errorMessage + '\n' + stackTrace);
}