Expenses.CurrencyProviders.ECBCurrencyProvider.GetExchangeRate C# (CSharp) Method

GetExchangeRate() public method

public GetExchangeRate ( string fromCurrency, string targetCurrency, System.DateTime day ) : Task
fromCurrency string
targetCurrency string
day System.DateTime
return Task
        public async Task<decimal> GetExchangeRate(string fromCurrency, string targetCurrency, DateTime day)
        {
            const string dailyRateUri = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
            // ECB only gives ur daily rates in the feed, so we are a bit limited with this provider

            if (string.Compare(fromCurrency, targetCurrency, 
                System.StringComparison.OrdinalIgnoreCase) == 0)
                return 1;

            var xml = await new HttpClient().GetStringAsync(dailyRateUri);
            var doc = XDocument.Parse(xml);

            var fromRate = 1.0m;
            if (fromCurrency.ToUpper() != "EUR")
                fromRate = GetRate(doc, fromCurrency.ToUpper());

            var toRate = 1.0m;
            if (targetCurrency.ToUpper() != "EUR")
                toRate = GetRate(doc, targetCurrency.ToUpper());

            // todo: check cross rates and inversions 
            return fromRate / toRate;
        }

Usage Example

Exemplo n.º 1
0
 public async Task TestEuroBase()
 {
     // arrange
     var provider = new ECBCurrencyProvider();
     // act
     var result = await provider.GetExchangeRate("EUR", "GBP", DateTime.Now.Date);
     // assert
     Assert.AreNotEqual(1, result, "1 should not have been returned");
 }
All Usage Examples Of Expenses.CurrencyProviders.ECBCurrencyProvider::GetExchangeRate
ECBCurrencyProvider