Switch currencyservice to static JSON file (#173)
This commit is contained in:
parent
d5db024763
commit
f7580958cc
2 changed files with 38 additions and 30 deletions
|
@ -30,8 +30,6 @@ require('@google-cloud/debug-agent').start({
|
|||
|
||||
const path = require('path');
|
||||
const grpc = require('grpc');
|
||||
const request = require('request');
|
||||
const xml2js = require('xml2js');
|
||||
const pino = require('pino');
|
||||
const protoLoader = require('@grpc/proto-loader');
|
||||
|
||||
|
@ -39,7 +37,6 @@ const MAIN_PROTO_PATH = path.join(__dirname, './proto/demo.proto');
|
|||
const HEALTH_PROTO_PATH = path.join(__dirname, './proto/grpc/health/v1/health.proto');
|
||||
|
||||
const PORT = 7000;
|
||||
const DATA_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
|
||||
|
||||
const shopProto = _loadProto(MAIN_PROTO_PATH).hipstershop;
|
||||
const healthProto = _loadProto(HEALTH_PROTO_PATH).grpc.health.v1;
|
||||
|
@ -69,36 +66,12 @@ function _loadProto (path) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Helper function that gets currency data from an XML webpage
|
||||
* Helper function that gets currency data from a stored JSON file
|
||||
* Uses public data from European Central Bank
|
||||
*/
|
||||
let _data;
|
||||
function _getCurrencyData (callback) {
|
||||
if (!_data) {
|
||||
logger.info('Fetching currency data...');
|
||||
request(DATA_URL, (err, res) => {
|
||||
if (err) {
|
||||
throw new Error(`Error getting data: ${err}`);
|
||||
}
|
||||
|
||||
const body = res.body.split('\n').slice(7, -2).join('\n');
|
||||
xml2js.parseString(body, (err, resJs) => {
|
||||
if (err) {
|
||||
throw new Error(`Error parsing HTML: ${err}`);
|
||||
}
|
||||
|
||||
const array = resJs['Cube']['Cube'].map(x => x['$']);
|
||||
const results = array.reduce((acc, x) => {
|
||||
acc[x['currency']] = x['rate'];
|
||||
return acc;
|
||||
}, { 'EUR': '1.0' });
|
||||
_data = results;
|
||||
callback(_data);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
callback(_data);
|
||||
}
|
||||
const data = require('./data/currency_conversion.json');
|
||||
callback(data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue