-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
123 lines (111 loc) · 3.37 KB
/
handler.js
File metadata and controls
123 lines (111 loc) · 3.37 KB
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
"use strict";
// TODO: Include multiple redundant API endpoints to EOS networks
const BLOCK_PRODUCER_TABLE = process.env.BLOCK_PRODUCER_TABLE;
const EOS_API_ENDPOINT = process.env.EOS_API_ENDPOINT;
const AWS = require("aws-sdk");
const request = require("request-promise-native");
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const EosApi = require("eosjs-api");
const eos = EosApi({ httpEndpoint: EOS_API_ENDPOINT });
module.exports.cacheBpJson = async (event, context, callback) => {
try {
const bpList = await eos.getProducers({ json: true });
const cachedResponse = await _cacheBpJsonFiles(bpList.rows);
let response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
data: { success: `${cachedResponse.length} items cached.` }
};
callback(null, response);
} catch (err) {
console.log("Error in cacheBpJson handler function: ", err);
}
};
module.exports.getAllProducers = async (event, context, callback) => {
try {
const bpList = await eos.getProducers({ json: true }).then(res => res.rows);
const cachedBpJsonFiles = await _dynamoGetAll(BLOCK_PRODUCER_TABLE);
const responseData = bpList.map(bp => {
cachedBpJsonFiles.forEach(bpJson => {
if (bp.owner === bpJson.producer_account_name) {
bp.bpJson = bpJson;
}
});
return bp;
});
let response = {
isBase64Encoded: false,
statusCode: 200,
headers: { "Access-Control-Allow-Origin": "*" },
body: JSON.stringify(responseData)
};
callback(null, response);
} catch (err) {
console.log("Error in getAllProducers handler function: ", err);
}
};
async function _cacheBpJsonFiles(bpList) {
const reqList = bpList.map(bp => {
const reqUrl = _joinHostResource(bp.url, "bp.json");
const producerAccountName = bp.owner;
return request(reqUrl)
.then(resData => {
return _dynamoPut(BLOCK_PRODUCER_TABLE, producerAccountName, resData);
})
.catch(err => {
console.log("Error in _cacheBpJsonFiles helper function: ", err);
const note = `Couldn't retrieve bp.json from ${reqUrl}. Please contact this Block Producer and ask them nicely to make their bp.json file available!`;
console.log("IN _cacheBEPSJONFILE ERROR", BLOCK_PRODUCER_TABLE);
return _dynamoPut(BLOCK_PRODUCER_TABLE, producerAccountName, note);
});
});
return Promise.all(reqList);
}
function _dynamoPut(tableName, bpAccountName, data) {
return dynamoDb
.put({
TableName: tableName,
Item: {
producer_account_name: bpAccountName,
data: data,
timestamp: Date.now()
}
})
.promise()
.then(data => {
console.log("Dynamo Put Success!", data);
})
.catch(err => {
console.log("Dynamo Put Error: ", err);
});
}
async function _dynamoGetAll(tableName) {
return dynamoDb
.scan({ TableName: tableName })
.promise()
.then(data => {
return data.Items;
})
.catch(err => {
console.log("Error retrieving records from DynamoDB: ", err);
});
}
function _isJsonString(str) {
try {
JSON.parse(str);
} catch (err) {
return false;
}
return true;
}
function _joinHostResource(host, resource) {
let endpoint;
if (host[host.length - 1] === "/") {
endpoint = host + resource;
} else {
endpoint = host + "/" + resource;
}
return endpoint;
}