public ShoppingCartItemDto AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItemItem) { var productIdentifier = shoppingCartItemItem.ProductIdentifier; if (!CryptocurrencyHelper.AllowedCurrencies.Contains(productIdentifier.ToLower())) { throw new InvalidProductIdentifierException(); } var response = Client.GetAsync($"https://data.messari.io/api/v1/assets/" + $"{productIdentifier}/metrics?fields=market_data/price_usd").Result; var data = response.DeserializeJsonToObject <CryptoCurrencyResponse>(true).Result; return(_shoppingCartRepository.AddCartItem(email, shoppingCartItemItem, data.PriceInUsd)); }
public async Task AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItem) { var identifier = shoppingCartItem.ProductIdentifier; if (identifier.ToUpper() != "BTC" && identifier.ToUpper() != "ETH" && identifier.ToUpper() != "USDT" && identifier.ToUpper() != "XMR") { throw new ResourceNotFoundException("Invalid product identifier."); } // Call the external API using the product identifier as an URL parameter to string URL = "https://data.messari.io/api/v1/assets/" + shoppingCartItem.ProductIdentifier + "/metrics"; string urlParameters = "?fields=id,symbol,name,slug,market_data/price_usd"; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json") ); // List data response. var response = await client.GetAsync(urlParameters); if (response.IsSuccessStatusCode) { // Receive the current price in USD for this particular cryptocurrency // Deserialize the response to a CryptoCurrencyDto model var responseObject = await HttpResponseMessageExtensions.DeserializeJsonToObject <CryptoCurrencyDto>(response, true); client.Dispose(); _shoppingCartRepository.AddCartItem(email, shoppingCartItem, responseObject.PriceInUsd); } else { throw new Exception("Response failed to external API."); } }
public void AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItemItem) { float priceInUsd = _cryptoCurrencyService.GetCryptocurrencyPrice(shoppingCartItemItem.ProductIdentifier).Result; _shoppingCartRepository.AddCartItem(email, shoppingCartItemItem, priceInUsd); }