Example #1
0
        public async Task <Price> GetPrice(string pair)
        {
            string supportedCur;

            try
            {
                supportedCur = await GetSupprtedCurrency(pair);
            }
            catch
            {
                supportedCur = null;
            }
            supportedCur = await GetSupprtedCurrency(pair);

            if (string.IsNullOrEmpty(supportedCur))
            {
                return(null);
            }

            var first    = pair.Substring(0, pair.Length - supportedCur.Length);
            var currency = await _coinGeckoRepository.GetCurrency(first);

            if (currency == null)
            {
                return(null);
            }

            var oPrice = new List <CoinGeckoMarketCurrency>();

            using (var client = new HttpClient())
                using (var requets = new HttpRequestMessage(HttpMethod.Get, $"{_coinGeckoOptions.APIUri}coins/markets?ids={currency.Id}&vs_currency={supportedCur}"))
                {
                    using (var response = await client.SendAsync(requets, HttpCompletionOption.ResponseHeadersRead))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            oPrice = await _httpResponseService.DeserializeJsonFromStream <List <CoinGeckoMarketCurrency> >(response);
                        }
                        else
                        {
                            var content = await _httpResponseService.StreamToStringAsync(await response.Content.ReadAsStreamAsync());

                            throw new ApiException(message: content)
                                  {
                                      StatusCode = (int)response.StatusCode,
                                      Content    = content
                                  };
                        }
                    }
                }

            var price = new Price()
            {
                Change = oPrice.FirstOrDefault().PriceChangePercentage24H.ToString(),
                High   = oPrice.FirstOrDefault().High24H.ToString(),
                Last   = oPrice.FirstOrDefault().CurrentPrice.ToString(),
                Low    = oPrice.FirstOrDefault().Low24H.ToString(),
                ATH    = oPrice.FirstOrDefault().Ath.ToString()
            };

            return(price);
        }