-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-proxy.js
69 lines (62 loc) · 2.13 KB
/
api-proxy.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
/* global process */
(function () {
"use strict";
var rp = require("request-promise"),
_ = require("lodash"),
Buffer = require('buffer/').Buffer,
q = require("q");
require("stringformat").extendString("format");
var proxy = (function (baseUrl) {
var cookieJar = rp.jar(),
request = rp.defaults({
proxy: process.env.WASTELINE_PROXY || null,
jar: cookieJar,
rejectUnauthorized: false,
followRedirect: true,
followAllRedirects: true,
headers: {
"accept": "application/json, text/javascript, */*",
"user-agent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
"dnt": "1",
"accept-language": "en-US,en;q=0.8",
"x-requested-with": "XMLHttpRequest",
"content-type": "application/x-www-form-urlencoded",
"connection": "keep-alive",
"origin": baseUrl,
"referer": baseUrl
}
});
return {
getCycle: function (coordinates) {
var deferred = q.defer();
try {
request({
method: "GET",
uri: "{0}&geofilter.distance={1}%2C{2}%2C0".format(baseUrl, coordinates[0], coordinates[1])
}).then(function (response) {
try {
var json = JSON.parse(response);
if (json && json.records && json.records.length && json.records[0].fields && Object.keys(json.records[0].fields).length) {
deferred.resolve(json.records[0].fields);
}
else {
deferred.reject("Unexpected JSON response format: records[0].fields not found");
}
}
catch (ex) {
deferred.reject("Cannot parse response as JSON");
}
}, deferred.reject)
.catch(function (ex) {
deferred.reject(ex.message);
});
}
catch (ex) {
deferred.reject(ex.message);
}
return deferred.promise;
}
};
})("https://data.townofcary.org/api/records/1.0/search/?dataset=solid-waste-and-recycling-collection-routes&rows=1");
module.exports = proxy;
})();