-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
134 lines (123 loc) · 6.07 KB
/
tools.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
'use strict';
const domain = 'https://dgd-oipa.blue4you.be';
let request = require('request-promise');
let Promise = require('bluebird');
let cacheProvider = require('./cache-provider');
let Url = require('url');
module.exports = {
getOptions: function (url) {
return {
method: 'GET',
url: url,
json: true,
headers: {
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Content-Language': 'en',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Content-Language, Accept',
'Accept-Encoding': '',
'Accept-Language': 'en-US,en;q=0.8'
}
}
},
query: function (endpoint, type, output) {
return request(module.exports.getOptions(endpoint)).then(function (data) {
switch (type) {
case "pager":
if (!output) output = data.results;
else output = output.concat(data.results);
break;
case "sectors":
return Promise.map(data.results, function (result) {
let url_parts = Url.parse(result.url);
let sector_url = domain + url_parts.pathname;
return Promise.resolve(module.exports.query(sector_url).catch(function (err) {
return module.exports.errorHandler(err, sector_url);
}));
}, {concurrency: 5}).then(function (data) {
return data;
});
case "documents":
if (!output) output = [];
data.results.map(function (docs) {
docs.document_links.map(function (doc) {
output.push(doc);
});
});
break;
case "countries":
if (!output) output = {};
let cc = {},
url = domain + "/api/transactions/aggregations/?format=json" +
"&group_by=recipient_country" +
"&aggregations=activity_count,disbursement_expenditure" +
"&order_by=recipient_country" +
"&page_size=400" +
"&transaction_date_year=" + (new Date()).getFullYear();
if (!output["results"] && !output["country_data"]) {
output["results"] = [];
output["country_data"] = {};
}
data.results.map(function (result) {
output["results"].push(result);
// Get the countries at activity level and build a array.
// This is used to determine the polygon for valid locations.
if (result.recipient_countries.length > 0) {
result.recipient_countries.map(function (country) {
if (output["country_data"].length <= 0 || output["country_data"][country.country.code] === undefined) {
let budget_url = url + "&recipient_country=" + country.country.code;
output["country_data"][country.country.code] = {
"country": country.country.name,
"id": country.country.code,
"budget": new Promise(function (resolve, reject) {
return module.exports.query(budget_url).then(function (budget_data) {
if (budget_data.results === undefined || budget_data.results.length <= 0)
return reject("No data for - " + budget_url);
return resolve({
"budget": budget_data.results[0].disbursement_expenditure,
"activity_count": budget_data.results[0].activity_count
});
});
}),
"flag": "https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.1.0/flags/1x1/" + country.country.code.toLowerCase() + ".svg"
};
}
});
}
});
break;
default:
return data;
}
if (data.next !== null) {
return module.exports.query(data.next, type, output);
}
return output;
}).catch(function (err) {
return module.exports.errorHandler(err, endpoint);
});
},
setCache: function (key, obj) {
const CACHE_DURATION = 86400;
cacheProvider.instance().set(key, obj, CACHE_DURATION, function (err, success) {
if (!err && success) {
console.log("Cache entry on " + key + " has been set.");
}
});
},
errorHandler: function (err, url) {
switch (err.message) {
case '400 - {"detail":"Bad Request Error."}':
console.log('HTTP to HTTPS not supported: ' + url);
throw err;
case '404 - {"detail":"Not found."}':
console.log('Detail not found on request: ' + url);
return;
default:
console.log('Error on request: ' + url);
throw err;
}
}
};