Beispiel #1
0
        private async Task <LatestFetch> Fetch()
        {
            var previous = _Latest;
            var fetch    = new LatestFetch();

            try
            {
                var rates = await _Inner.GetRatesAsync();

                fetch.Latest      = rates;
                fetch.Expiration  = DateTimeOffset.UtcNow + ValidatyTime;
                fetch.NextRefresh = DateTimeOffset.UtcNow + RefreshRate;
            }
            catch (Exception ex)
            {
                if (previous != null)
                {
                    fetch.Latest     = previous.Latest;
                    fetch.Expiration = previous.Expiration;
                }
                else
                {
                    fetch.Expiration = DateTimeOffset.UtcNow;
                }
                fetch.NextRefresh = DateTimeOffset.UtcNow;
                fetch.Exception   = ex;
            }
            _Latest = fetch;
            fetch.GetResult(); // Will throw if not valid
            return(fetch);
        }
Beispiel #2
0
 public Task <ICollection <Rate> > GetRatesAsync()
 {
     return(_MemoryCache.GetOrCreateAsync("GLOBAL_RATES", (ICacheEntry entry) =>
     {
         entry.AbsoluteExpiration = DateTimeOffset.UtcNow + CacheSpan;
         return _Inner.GetRatesAsync();
     }));
 }
Beispiel #3
0
        public async Task <DataWrapper <NBitpayClient.Rate[]> > GetRates()
        {
            var allRates = (await _RateProvider.GetRatesAsync());

            return(new DataWrapper <NBitpayClient.Rate[]>
                       (allRates.Select(r =>
                                        new NBitpayClient.Rate()
            {
                Code = r.Currency,
                Name = _CurrencyNameTable.GetCurrencyData(r.Currency)?.Name,
                Value = r.Value
            }).Where(n => n.Name != null).ToArray()));
        }
Beispiel #4
0
        public async Task <ICollection <Rate> > GetRatesAsync()
        {
            List <Rate> rates = new List <Rate>();

            foreach (var rate in await rateProvider.GetRatesAsync())
            {
                var localRate = rate.Value;
                foreach (var rule in rateRules)
                {
                    localRate = rule.Apply(network, localRate);
                }
                rates.Add(new Rate(rate.Currency, localRate));
            }
            return(rates);
        }
            public async Task <PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
            {
                DateTimeOffset now = DateTimeOffset.UtcNow;

                try
                {
                    return(await _inner.GetRatesAsync(cancellationToken));
                }
                catch (Exception ex)
                {
                    Exception = ex;
                    return(Array.Empty <PairRate>());
                }
                finally
                {
                    Latency = DateTimeOffset.UtcNow - now;
                }
            }
Beispiel #6
0
            public async Task <ExchangeRates> GetRatesAsync(CancellationToken cancellationToken)
            {
                DateTimeOffset now = DateTimeOffset.UtcNow;

                try
                {
                    return(await _inner.GetRatesAsync(cancellationToken));
                }
                catch (Exception ex)
                {
                    Exception = ex;
                    return(new ExchangeRates());
                }
                finally
                {
                    Latency = DateTimeOffset.UtcNow - now;
                }
            }
Beispiel #7
0
            public Task <ExchangeRates> GetRatesAsync()
            {
                DateTimeOffset now = DateTimeOffset.UtcNow;

                try
                {
                    return(_inner.GetRatesAsync());
                }
                catch (Exception ex)
                {
                    Exception = ex;
                    return(Task.FromResult(new ExchangeRates()));
                }
                finally
                {
                    Latency = DateTimeOffset.UtcNow - now;
                }
            }
        private async Task <LatestFetch> Fetch(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var previous = _Latest;
            var fetch    = new LatestFetch();

            fetch.ExchangeName = ExchangeName;
            try
            {
                var rates = await _Inner.GetRatesAsync(cancellationToken);

                fetch.Latest      = rates;
                fetch.Updated     = DateTimeOffset.UtcNow;
                fetch.Expiration  = fetch.Updated + ValidatyTime;
                fetch.NextRefresh = fetch.Updated + RefreshRate;
            }
            catch (Exception ex)
            {
                if (previous != null)
                {
                    fetch.Latest     = previous.Latest;
                    fetch.Expiration = previous.Expiration;
                    fetch.Backoff    = previous.Backoff * 2;
                    if (fetch.Backoff > MaxBackoff)
                    {
                        fetch.Backoff = MaxBackoff;
                    }
                }
                else
                {
                    fetch.Expiration = DateTimeOffset.UtcNow;
                }
                fetch.NextRefresh = DateTimeOffset.UtcNow + fetch.Backoff;
                fetch.Exception   = ex;
            }
            _Latest = fetch;
            fetch.GetResult(); // Will throw if not valid
            return(fetch);
        }
        private async Task <LatestFetch> Fetch()
        {
            var fetch = new LatestFetch();

            try
            {
                var rates = await _Inner.GetRatesAsync();

                fetch.Latest = rates;
            }
            catch (Exception ex)
            {
                fetch.Exception = ex;
            }
            fetch.Timestamp = DateTimeOffset.UtcNow;
            _Latest         = fetch;
            if (fetch.Exception != null)
            {
                ExceptionDispatchInfo.Capture(fetch.Exception).Throw();
            }
            return(fetch);
        }