public async Task <ExchangeData> GetExchangeRateAsync(GetExchangeData request) { try { _logger.LogDebug($"Getting Exchange Rate Started. Request: {request}"); var response = await _httpClient.GetAsync($"latest?symbols={string.Join(',', request.TargetCurrencies)}&base={request.BaseCurrency}"); if (!response.IsSuccessStatusCode) { _logger.LogError($"Getting Exchange Rate Failed. Request: {request}"); return(null); } var result = JsonConvert.DeserializeObject <ExchangeData>(await response.Content.ReadAsStringAsync()); _logger.LogDebug($"Getting Exchange Rate Finished. Request: {request} Response: {result}"); return(result); } catch (Exception ex) { _logger.LogError(ex, "An Error Has Occureated In Getting Crypto Currencies."); return(null); } }
private async Task <GetCryptoCurrencyQuoteResponse> CryptoCurrencyQuoteWithExchangeApproach(string cryptoCurrencySymbol, int cryptoCurrencyId) { var result = new GetCryptoCurrencyQuoteResponse(); var cryptoCurrencyQuote = await GetCryptoCurrencyQuote(cryptoCurrencyId, _baseCurrency).ConfigureAwait(false); if (!cryptoCurrencyQuote.Success) { result.Message = cryptoCurrencyQuote.Message; return(result); } var getExchangeData = new GetExchangeData { BaseCurrency = _baseCurrency.ToString(), TargetCurrencies = _currencies }; var exchangeRates = await _exchangeRatesProxy.GetExchangeRateAsync(getExchangeData).ConfigureAwait(false); if (exchangeRates == null || exchangeRates.Rates == null) { result.Message = "Can Not Exchange Base Currency Rate "; return(result); } result.Quotes = _currencies.ToDictionary(a => a, t => t == _baseCurrency ? decimal.Round(cryptoCurrencyQuote.Price, _maximumFloatingPointDigit): GetPriceInTargetCurrency(t, cryptoCurrencyQuote.Price, exchangeRates)); result.Success = true; result.CryptoCurrencySymbol = cryptoCurrencySymbol; result.Message = "Success"; return(result); }
public async Task GetCryptoCurrencyQuoteAsync_CryptoCurrencyQuoteWithExchangeApproach_VerifyCoinMarketCapProxyMethodsVerifyExchangeRatesProxyOnce() { // Arrange Initialize(); _appSettings.CalculateQuoteApproach = "CryptoCurrencyQuoteWithExchangeApproach"; _appSettings.Currencies = new List <string> { "USD", "EUR" }; _appSettings.BaseCurrency = "USD"; _appSettings.MaximumFloatingPointDigit = 3; string cryptoCurrencySymbol = "BTN"; int cryptoCurrencyId = 1; var request = new GetCryptoCurrencyQuoteRequest { CryptoCurrencySymbol = cryptoCurrencySymbol }; GetCryptoCurrencies getCryptoCurrenciesResponse = new GetCryptoCurrencies { CryptoCurrencies = new List <CryptoCurrencyData> { new CryptoCurrencyData { Id = cryptoCurrencyId, Name = "Bitcoin", Symbol = cryptoCurrencySymbol, IsActive = true } }, Status = new StatusData { ErrorCode = 0 } }; _coinMarketCapProxy.Setup(x => x.GetCryptoCurrenciesAsync()).ReturnsAsync(getCryptoCurrenciesResponse); GetCryptoCurrencyQuotes getCryptoCurrencyQuotesRequestForUSD = null; decimal usdPrice = 100000; CryptoCurrencyQuotesData getCryptoCurrencyQuotesResponseForUSD = new CryptoCurrencyQuotesData { Data = new Dictionary <string, CryptoCurrencyQuoteData> { { cryptoCurrencyId.ToString(), new CryptoCurrencyQuoteData { Id = cryptoCurrencyId, Quotes = new Dictionary <string, QuoteData> { { "USD", new QuoteData { Price = usdPrice } } } } } } }; _coinMarketCapProxy.Setup(x => x.GetCryptoCurrencyQuotesAsync(It.IsAny <GetCryptoCurrencyQuotes>())) .Callback <GetCryptoCurrencyQuotes>(x => getCryptoCurrencyQuotesRequestForUSD = x) .ReturnsAsync(getCryptoCurrencyQuotesResponseForUSD); decimal eurRateToUsd = 0.1m; GetExchangeData exchangeRequest = null; ExchangeData exchangeResponse = new ExchangeData { Base = _appSettings.BaseCurrency, Rates = new Dictionary <string, decimal> { { "EUR", eurRateToUsd } } }; _exchangeRatesProxy.Setup(x => x.GetExchangeRateAsync(It.IsAny <GetExchangeData>())).Callback <GetExchangeData>(x => exchangeRequest = x).ReturnsAsync(exchangeResponse); // Act var result = await _cryptoCurrencyService.GetCryptoCurrencyQuoteAsync(request).ConfigureAwait(false); // Assert Assert.NotNull(result); Assert.True(result.Success); Assert.Equal("Success", result.Message); Assert.NotNull(result.Quotes); Assert.Equal(cryptoCurrencySymbol, result.CryptoCurrencySymbol); Assert.True(result.Quotes.Count() == 2); var usdQuote = result.Quotes.FirstOrDefault(x => x.Key == "USD"); Assert.NotNull(usdQuote); Assert.Equal(usdPrice, usdQuote.Value); var eurQuote = result.Quotes.FirstOrDefault(x => x.Key == "EUR"); Assert.NotNull(eurQuote); Assert.Equal(decimal.Round(eurRateToUsd * usdPrice, _appSettings.MaximumFloatingPointDigit), eurQuote.Value); _coinMarketCapProxy.Verify(x => x.GetCryptoCurrenciesAsync(), Times.Once); _exchangeRatesProxy.Verify(x => x.GetExchangeRateAsync(It.IsAny <GetExchangeData>()), Times.Once); Assert.NotNull(exchangeRequest); Assert.Equal(_appSettings.BaseCurrency, exchangeRequest.BaseCurrency); Assert.NotNull(exchangeRequest.TargetCurrencies); Assert.True(_appSettings.Currencies.Count() == exchangeRequest.TargetCurrencies.Count()); foreach (var currency in _appSettings.Currencies) { Assert.Contains(currency, exchangeRequest.TargetCurrencies); } _coinMarketCapProxy.Verify(x => x.GetCryptoCurrencyQuotesAsync(It.IsAny <GetCryptoCurrencyQuotes>()), Times.Once); Assert.NotNull(getCryptoCurrencyQuotesRequestForUSD); Assert.NotNull(getCryptoCurrencyQuotesRequestForUSD.CryptoCurrencies); Assert.True(getCryptoCurrencyQuotesRequestForUSD.CryptoCurrencies.Count() == 1); Assert.Contains(cryptoCurrencyId, getCryptoCurrencyQuotesRequestForUSD.CryptoCurrencies); Assert.NotNull(getCryptoCurrencyQuotesRequestForUSD.ConvertCurrencies); Assert.True(getCryptoCurrencyQuotesRequestForUSD.ConvertCurrencies.Count() == 1); Assert.Contains("USD", getCryptoCurrencyQuotesRequestForUSD.ConvertCurrencies); }