From bd9ba0fd2668f6a886eaab87dad82ed7cb2568df Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Wed, 6 Mar 2019 14:27:32 -0800 Subject: [PATCH] Switch currencyservice to static JSON file --- .../data/currency_conversion.json | 35 +++++++++++++++++++ src/currencyservice/server.js | 33 ++--------------- 2 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 src/currencyservice/data/currency_conversion.json diff --git a/src/currencyservice/data/currency_conversion.json b/src/currencyservice/data/currency_conversion.json new file mode 100644 index 0000000..bd28709 --- /dev/null +++ b/src/currencyservice/data/currency_conversion.json @@ -0,0 +1,35 @@ +{ + "EUR": "1.0", + "USD": "1.1305", + "JPY": "126.40", + "BGN": "1.9558", + "CZK": "25.592", + "DKK": "7.4609", + "GBP": "0.85970", + "HUF": "315.51", + "PLN": "4.2996", + "RON": "4.7463", + "SEK": "10.5375", + "CHF": "1.1360", + "ISK": "136.80", + "NOK": "9.8040", + "HRK": "7.4210", + "RUB": "74.4208", + "TRY": "6.1247", + "AUD": "1.6072", + "BRL": "4.2682", + "CAD": "1.5128", + "CNY": "7.5857", + "HKD": "8.8743", + "IDR": "15999.40", + "ILS": "4.0875", + "INR": "79.4320", + "KRW": "1275.05", + "MXN": "21.7999", + "MYR": "4.6289", + "NZD": "1.6679", + "PHP": "59.083", + "SGD": "1.5349", + "THB": "36.012", + "ZAR": "16.0583" +} \ No newline at end of file diff --git a/src/currencyservice/server.js b/src/currencyservice/server.js index 633078c..0b231b0 100644 --- a/src/currencyservice/server.js +++ b/src/currencyservice/server.js @@ -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); } /**