forked from anthonypiazza/CL-EA-NodeJS-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (86 loc) · 2.56 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
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
const request = require("request");
const createRequest = (input, callback) => {
let url = process.env.URL || "https://min-api.cryptocompare.com/data/";
// Including an endpoint parameter is optional but common since
// different endpoints of the same API typically respond with
// data structured different from one another
const endpoint = input.data.endpoint || "price";
// Include a trailing slash "/" in your url if endpoint is in use
// and part of the URL
url = url + endpoint;
// Create additional input params here, for example:
const coin = input.data.coin || "ETH";
const market = input.data.market || "USD";
// Build your query object with the given input params, for example:
let queryObj = {
fsym: coin,
fsyms: coin,
tsym: market,
tsyms: market
}
// Use this to clean up unused input parameters
for (let key in queryObj) {
if (queryObj[key] === "") {
delete queryObj[key];
}
}
const options = {
url: url,
// Change the API_KEY key name to the name specified by the API
// Note: If the API only requires a request header to be specified
// for authentication, you can place this in the job's specification
// instead of writing an external adapter
/*
headers: {
"API_KEY": process.env.API_KEY
},
*/
qs: queryObj,
json: true
}
request(options, (error, response, body) => {
// Add any API-specific failure case here to pass that error back to Chainlink
if (error || response.statusCode >= 400) {
callback(response.statusCode, {
jobRunID: input.id,
status: "errored",
error: body,
statusCode: response.statusCode
});
} else {
callback(response.statusCode, {
jobRunID: input.id,
data: body,
statusCode: response.statusCode
});
}
});
};
// This is a wrapper to allow the function to work with
// GCP Functions
exports.gcpservice = (req, res) => {
createRequest(req.body, (statusCode, data) => {
res.status(statusCode).send(data);
});
};
// This is a wrapper to allow the function to work with
// AWS Lambda
exports.handler = (event, context, callback) => {
createRequest(event, (statusCode, data) => {
callback(null, data);
});
};
// This is a wrapper to allow the function to work with
// newer AWS Lambda implementations
exports.handlerv2 = (event, context, callback) => {
createRequest(JSON.parse(event.body), (statusCode, data) => {
callback(null, {
statusCode: statusCode,
body: JSON.stringify(data),
isBase64Encoded: false
});
});
};
// This allows the function to be exported for testing
// or for running in express
module.exports.createRequest = createRequest;