Пример #1
0
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        /// <param name="source"><inheritdoc/></param>
        /// <param name="dest"><inheritdoc/></param>
        /// <param name="convStatus"><inheritdoc/></param>
        /// <returns><inheritdoc/></returns>
        public virtual TimedQuote GetQuote(Currencies source, Currencies dest, ConversionBag convStatus)
        {
            if (source == dest)
            {
                return new TimedQuote()
                       {
                           BaseCurrency = source, Midpoint = 1, QuoteCurrency = dest, UpdatedAtUTC = DateTime.UtcNow.AddYears(1)
                       }
            }
            ;

            if (CanConvert(source, dest, convStatus) == true)
            {
                TimedQuote quote = getQuote(source, dest, convStatus);

                if (quote != null && quote.UpdatedAtUTC.AddMilliseconds(ConversionProviderFactory.QuotesValidForMillis) >= DateTime.UtcNow)
                {
                    if (this is CacheConversionProvider == false)                        // if the provider who got us the quote is not the cache provider
                    {
                        ConversionProviderFactory.AddToCache(quote, quote.UpdatedAtUTC); // add the quote to the cache
                    }
                    return(quote);
                }
            }

            if (TryNext == false)
            {
                return(null);
            }

            return(ConversionProviderFactory.GetNext(this)?.GetQuote(source, dest, convStatus));
        }
Пример #2
0
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        /// <param name="source"><inheritdoc/></param>
        /// <param name="dest"><inheritdoc/></param>
        /// <param name="amount"><inheritdoc/></param>
        /// <returns><inheritdoc/></returns>
        protected override async Task <TimedQuote> getQuoteAsync(Currencies source, Currencies dest, ConversionBag convStatus)
        {
            // conditions of convertibility have been done already, but convStatus can be null
            if (convStatus == null)
            {
                convStatus = new ConversionBag();
            }

            string idKey = $"{source}{dest}{DefaultCurrency}{this.GetHashCode()}";

            convStatus.Bag.Add(idKey, "yes");

            var qs = await ConversionProviderFactory.GetConversionProvider().GetQuoteAsync(source, DefaultCurrency, convStatus);

            if (qs == null)
            {
                return(null);
            }
            var qd = await ConversionProviderFactory.GetConversionProvider().GetQuoteAsync(dest, DefaultCurrency, convStatus);

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

            return(new TimedQuote()
            {
                BaseCurrency = source,
                QuoteCurrency = dest,
                Midpoint = qs.Midpoint / qd.Midpoint,
                UpdatedAtUTC = qs.UpdatedAtUTC < qd.UpdatedAtUTC ? qs.UpdatedAtUTC : qd.UpdatedAtUTC
            });
        }