-
Notifications
You must be signed in to change notification settings - Fork 2
/
price.js
35 lines (31 loc) · 1.1 KB
/
price.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
import fetch from 'node-fetch'
import {get, upperCase} from 'lodash'
const API_ENDPOINT = "https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/"
const getCurrencyData = (data, path) => get(data, path, 0.0)
const getRelation =(base, divider) => parseFloat(base) / parseFloat(divider)
exports.handler = async (event, context) => {
const {base, divider} = event.queryStringParameters
return fetch(API_ENDPOINT)
.then(response => response.json())
.then(data => {
const baseData = getCurrencyData(get(data,upperCase(base), 'USD'), 'rates.last')
const dividerData = getCurrencyData(get(data, upperCase(divider), 'USD'),'rates.last')
try {
return {
statusCode: 200,
body: JSON.stringify({currencies: {
base: baseData,
divider: dividerData,
relation: getRelation(baseData, dividerData)
}
}, null, 2)
}
} catch (error) {
return {
statusCode: 400,
body: error
}
}
})
.catch(error => ({ statusCode: 422, body: String(error) }));
};