This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
98 lines (90 loc) · 2.35 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
96
97
98
const rp = require('request-promise')
const retries = process.env.RETRIES || 3
const delay = process.env.RETRY_DELAY || 1000
const requestRetry = (options, retries) => {
return new Promise((resolve, reject) => {
const retry = (options, n) => {
return rp(options)
.then(response => {
if (response.body.error || response.body === []) {
if (n === 1) {
reject(response)
} else {
setTimeout(() => {
retries--
retry(options, retries)
}, delay)
}
} else {
return resolve(response)
}
})
.catch(error => {
if (n === 1) {
reject(error)
} else {
setTimeout(() => {
retries--
retry(options, retries)
}, delay)
}
})
}
return retry(options, retries)
})
}
const createRequest = (input, callback) => {
const url = 'https://api.nomics.com/v1/currencies/ticker'
const coin = input.data.coin || 'ETH'
const market = input.data.market || 'USD'
const queryObj = {
ids: coin,
convert: market,
key: process.env.API_KEY
}
const options = {
url: url,
qs: queryObj,
json: true,
resolveWithFullResponse: true
}
requestRetry(options, retries)
.then(response => {
const result = JSON.parse(response.body[0].price)
response.body.result = result
callback(response.statusCode, {
jobRunID: input.id,
data: response.body,
result: response.body.result,
statusCode: response.statusCode
})
})
.catch(error => {
callback(error.statusCode, {
jobRunID: input.id,
status: 'errored',
error,
statusCode: error.statusCode
})
})
}
exports.gcpservice = (req, res) => {
createRequest(req.body, (statusCode, data) => {
res.status(statusCode).send(data)
})
}
exports.handler = (event, context, callback) => {
createRequest(event, (statusCode, data) => {
callback(null, data)
})
}
exports.handlerv2 = (event, context, callback) => {
createRequest(JSON.parse(event.body), (statusCode, data) => {
callback(null, {
statusCode: statusCode,
body: JSON.stringify(data),
isBase64Encoded: false
})
})
}
module.exports.createRequest = createRequest