-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (59 loc) · 1.38 KB
/
index.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
const axios = require ('axios');
let baseUrl = 'https://api.icowatchlist.com/public/v1/'
let ICOWatchlist = {
getAll: async function() {
let response, path;
return await createRequest('');
},
getLive: async function() {
let response, path;
return await createRequest('live');
},
getUpcoming: async function() {
let response, path;
return await createRequest('upcoming');
},
getFinished: async function() {
let response, path;
return await createRequest('finished');
}
}
let createRequest = async function(methodName) {
try {
let url = baseUrl + methodName;
let response = await sendRequest(url);
let path = 'ico.' + methodName;
return await handleResponse(response, path);
} catch(e) {
throw('Error on ' + arguments.callee.name + ':' + e);
}
}
let handleResponse = async function(data, path){
try {
let response = data;
path.split('.').forEach(function(item){
if (item){
response = response[item];
}
})
return response;
} catch(e) {
throw('Error on ' + arguments.callee.name + ':' + e);
}
}
let sendRequest = async function(url){
try {
let config = {
timeout: 5000
};
let response = await axios.get(url, config);
if (response.status == 200) {
return response.data;
} else {
throw('Error on API request: ' + response);
}
} catch (e) {
throw('Error on ' + arguments.callee.name + ':' + e);
}
}
module.exports = ICOWatchlist;