예제 #1
0
    private async Task AssertProviderAsync(IExchangeRateProvider provider)
    {
        IEnumerable <ExchangeRate> rates = await provider.GetExchangeRateAsync();

        var usdRate = Assert.Single(rates, x => x.Ticker == "USD");

        Assert.NotEqual(0.0m, usdRate.Rate);
    }
예제 #2
0
        private async Task <decimal> GetRate(ExchangeRateRequest request)
        {
            if (request.From.Equals(request.To))
            {
                return(1);
            }

            var initialRate = (await ExchangeRateProvider.GetExchangeRateAsync(request)).Value;

            if (initialRate > 0)
            {
                return(initialRate);
            }

            throw new CurrencyExchangeException(request.From, request.To);
        }
예제 #3
0
        public async Task <IEnumerable <ExchangeRate> > GetExchangeRatesAsync()
        {
            if (!_cache.TryGetValue(nameof(GetExchangeRatesAsync), out List <ExchangeRate> exchangeRates))
            {
                exchangeRates = await _exchangeRateProvider.GetExchangeRateAsync();

                if (exchangeRates == null)
                {
                    throw new HttpRequestException("BTC/USD exchange rate is not available.");
                }

                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetAbsoluteExpiration(TimeSpan.FromSeconds(20));

                _cache.Set(nameof(GetExchangeRatesAsync), exchangeRates, cacheEntryOptions);
            }

            return(exchangeRates);
        }
예제 #4
0
 public async Task <ExchangeResult> Index(string from, string to)
 {
     return(await _exchangeRateProvider.GetExchangeRateAsync(new ExchangeRateRequest(Currency.GetCurrency(from),
                                                                                     Currency.GetCurrency(to))));
 }
        public async Task <decimal> ConvertCurrency(string currency, decimal value)
        {
            var exchangeRate = await _exchangeRateProvider.GetExchangeRateAsync(currency);

            return(value * exchangeRate);
        }