Пример #1
0
        public async Task <List <Ticker> > UpdateTickersAsync(List <Product> products)
        {
            string json = null;

            try
            {
                ProcessLogBroadcast?.Invoke(ApplicationName, MessageType.General,
                                            "Updating Update Tickers Information.");
                if (products == null || !products.Any())
                {
                    return(Tickers);
                }
                if (Tickers == null)
                {
                    Tickers = new List <Ticker>();
                }
                //Get price of all products
                foreach (Product product in products)
                {
                    Request request = new Request(ConnectionAdapter.Authentication.EndpointUrl, "GET",
                                                  $"/products/{product.ID}/ticker");
                    json = await ConnectionAdapter.RequestAsync(request);

                    if (string.IsNullOrEmpty(json))
                    {
                        return(Tickers);
                    }
                    Ticker ticker = JsonSerializer.Deserialize <Ticker>(json);
                    Tickers?.RemoveAll(x => x.ProductID == product.ID);
                    if (ticker == null)
                    {
                        continue;
                    }
                    ticker.ProductID = product.ID;
                    Tickers.Add(ticker);
                }

                foreach (Ticker ticker in Tickers)
                {
                    if (decimal.TryParse(ticker.Price, out decimal decimalPrice))
                    {
                        CurrentPrices[ticker.ProductID] = decimalPrice;
                    }
                }
            }
            catch (Exception e)
            {
                ProcessLogBroadcast?.Invoke(ApplicationName, MessageType.Error,
                                            $"Method: UpdateTickersAsync\r\nException Stack Trace: {e.StackTrace}\r\nJSON: {json}");
            }

            return(Tickers);
        }