Esempio n. 1
0
        static async Task TestTickerAsync()
        {
            Console.WriteLine("Testing coin Ticker info:");

            Console.WriteLine("enter coin id:");

            var id = Console.ReadLine();

            Console.WriteLine($"fetching ticker for id {id} ...");

            var ticker = await _client.GetTickerForIdAsync(id);

            if (ticker.Error == null)
            {
                Console.WriteLine($"{ticker.Value.Name}({ticker.Value.Id}({ticker.Value.Symbol})) - {ticker.Value.Rank} - BTC:{ticker.Value.PriceBtc}/USD:{ticker.Value.PriceUsd} - PercentChange24h:{ticker.Value.PercentChange24H}");
                Console.WriteLine("Press any key to finish test...");
            }
            else
            {
                Console.WriteLine($"CoinPaprika returned an error: {ticker.Error.ErrorMessage}");
            }

            Console.ReadLine();
            Console.WriteLine("Bye!");
        }
Esempio n. 2
0
        public static async Task <TickerInfo> GetTickerInfo(IMemoryCache cache)
        {
            //This stops us spamming the API on failures
            if (_lastFailTime.HasValue && (DateTime.Now - _lastFailTime.Value).TotalMinutes <= 45)
            {
                if (_lastKnownInfo != null)
                {
                    return(_lastKnownInfo);
                }
            }

            if (!cache.TryGetValue("HomeV3Ticker", out object tickerModel))
            {
                bool success = await _lock.WaitAsync(TimeSpan.FromSeconds(1));

                if (!success)
                {
                    return(null);
                }
                try
                {
                    if (!cache.TryGetValue("HomeV3Ticker", out tickerModel))
                    {
                        CoinpaprikaAPI.Client client = new CoinpaprikaAPI.Client();
                        throw new Exception();

                        tickerModel = (await client.GetTickerForIdAsync(@"trac-origintrail")).Value;

                        cache.Set("HomeV3Ticker", tickerModel, TimeSpan.FromMinutes(3));
                        _lastFailTime = null;
                    }
                }
                catch (Exception)
                {
                    _lastFailTime = DateTime.Now;

                    if (_lastKnownInfo != null)
                    {
                        return(_lastKnownInfo);
                    }

                    return(null);
                }
                finally
                {
                    _lock.Release();
                }
            }

            var tickerInfo = _lastKnownInfo = (TickerInfo)tickerModel;

            return(tickerInfo);
        }