예제 #1
0
        /// <summary>
        /// Use MNB Client to retrieve Available Currencies
        /// </summary>
        private void GetAndProcessAvailableCurrencies()
        {
            try
            {
                var currencyResponse = client.GetCurrencies(new GetCurrenciesRequestBody()).GetCurrenciesResult;

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(currencyResponse);

                XmlNodeList nodes = xmlDoc.SelectNodes(MNB_CURRENCIES_PER_CURRENCIES_PER_CURR);

                foreach (XmlNode node in nodes)
                {
                    // Skip if node is null or empty
                    if (string.IsNullOrWhiteSpace(node.InnerText))
                    {
                        continue;
                    }

                    CurrencyWrapper currencyWrapper = new CurrencyWrapper()
                    {
                        CurrencyName = node.InnerText
                    };

                    bool currencyAlreadyPresent = CurrenciesRetrieved.Any(curr => curr.CurrencyName == node.InnerText);
                    if (!currencyAlreadyPresent)
                    {
                        CurrenciesRetrieved.Add(currencyWrapper);
                    }
                }
            }
            catch (XmlException)
            {
                ShowErrorMessage("Could not parse the response from MNB while querying available currencies");
            }
            catch (System.Xml.XPath.XPathException e)
            {
                ShowErrorMessage($"XML structure is inconsistent with assumption. Could not retrieve structure of {MNB_CURRENCIES_PER_CURRENCIES_PER_CURR}");
            }
            catch (Exception e)
            {
                ShowErrorMessage(e.Message);
            }
        }
예제 #2
0
        /// <summary>
        /// Query the MNB Client to retrieve Exchange rates for given currencies
        /// </summary>
        /// <param name="start">DateTime: The start date to be used in the query</param>
        /// <param name="end">DateTime: The end date to be used in the query</param>
        /// <param name="currencyNames">IList<string>: The names of the currencies to be used in the query</param>
        private void GetAndProcessExchangeRates(DateTime start, DateTime end, IList <string> currencyNames)
        {
            try
            {
                string startDate = start.ToString(DATE_REQUEST_FORMAT);
                string endDate   = end.ToString(DATE_REQUEST_FORMAT);

                var currencies = string.Join(",", currencyNames);

                GetExchangeRatesRequestBody requestBody = new GetExchangeRatesRequestBody()
                {
                    startDate     = startDate,
                    endDate       = endDate,
                    currencyNames = currencies
                };

                GetExchangeRatesResponseBody exchangeRatesResponseBody = client.GetExchangeRates(requestBody);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(exchangeRatesResponseBody.GetExchangeRatesResult);

                XmlNodeList rawNodes = xmlDoc.SelectNodes(MNB_EXCHANGE_RATES_PER_DAY);
                foreach (XmlNode rawNode in rawNodes)
                {
                    // Retrieve the day of the FX rate and store it in variable: date
                    XmlNode rawDate = rawNode.Attributes.GetNamedItem(MNB_DATE_ATTRIBUTE_NAME);
                    DateTime.TryParse(rawDate.Value, out DateTime date);

                    // check if date is not included
                    var dateIsAlreadyAdded = CurrencySnapshotDate.Any(item => item.Date == date);
                    if (!dateIsAlreadyAdded)
                    {
                        var newFxDate = new FxDateWrapper()
                        {
                            Date = date,
                        };
                        CurrencySnapshotDate.Add(newFxDate);
                    }

                    // Iterate through child nodes and retrieve the unit and curr from attributes - and fx rate from innerText
                    foreach (XmlNode currencyRate in rawNode.ChildNodes)
                    {
                        // Find currency by name
                        XmlNode currAttrib      = currencyRate.Attributes.GetNamedItem(MNB_CURRENCY_ATTRIBUTE_NAME);
                        var     currencyWrapper = CurrenciesRetrieved.FirstOrDefault(currName => currName.CurrencyName.Equals(currAttrib.Value, StringComparison.InvariantCultureIgnoreCase));

                        if (currencyWrapper is null)
                        {
                            continue;
                        }

                        // feed currency unit to currencyWrapper
                        XmlNode unitAttrib = currencyRate.Attributes.GetNamedItem(MNB_UNIT_ATTRIBUTE_NAME);
                        if (Int32.TryParse(unitAttrib.Value, out int result))
                        {
                            currencyWrapper.RateUnit = result;
                        }

                        // Feed rate into currencyWrapper
                        currencyWrapper.CurrencyRates.Add(new Currency()
                        {
                            Date    = date,
                            RawRate = currencyRate.InnerText
                        });
                    }
                }
            }
            catch (Exception e)
            {
                string error = e.Message;
                ShowErrorMessage(error);
            }
        }
예제 #3
0
 /// <summary>
 /// Query the MNB Client to retrieve Exchange rates for given currencies
 /// </summary>
 private void GetAndProcessExchangeRates()
 {
     GetAndProcessExchangeRates(startDate, endDate, CurrenciesRetrieved.Select(name => name.CurrencyName).ToList());
 }