-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_api.js
46 lines (42 loc) · 1.46 KB
/
google_api.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
// Google API helper module
//
var request = require('request');
exports.callApiAsync = function(context, endpoint, params, callback)
{
var url = endpoint + "?";
if (params)
{
Object.keys(params).forEach(function (key)
{
url += key + "=" + encodeURIComponent(params[key]) + "&";
});
}
url += "key=" + Synchro.getConfig(context, "GOOGLE_CIVICS_KEY");
request({ url: url, timeout: 5000 }, function (err, response, body)
{
var jsonResponse = (!err && body) ? JSON.parse(body) : null;
if (!err)
{
if (response.statusCode != 200)
{
// Non-200 status is logical/protocol/service error
//
if (jsonResponse && jsonResponse.error)
{
// Google "Standard Error Response" - https://developers.google.com/civic-information/docs/v2/standard_errors
err = new Error(jsonResponse.error.message);
err.statusCode = jsonResponse.error.code;
err.response = jsonResponse;
}
else
{
// General purpose HTTP error...
err = new Error(jsonResponse.statusMessage);
err.statusCode = response.statusCode;
}
jsonResponse = null;
}
}
callback(err, jsonResponse);
});
}