예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fromDate"></param>
        /// <param name="toDate"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">Throw if fromDate is greater than startDate</exception>
        public async Task <IEnumerable <OpenExchangeRateResult> > GetExchangeRateHistoryPeriod(DateTime fromDate, DateTime toDate, PeriodMode mode = PeriodMode.Monthly)
        {
            if (fromDate >= toDate)
            {
                throw new ArgumentException("fromDate cannot be greater than toDate", nameof(fromDate));
            }
            if (mode == PeriodMode.Monthly)
            {
                fromDate = GetMidMonthDay(fromDate);
            }

            var allTasks = new List <Task <OpenExchangeRateResult> >();

            while (fromDate <= toDate)
            {
                allTasks.Add(GetExchangeRateHistory(fromDate));
                fromDate = mode == PeriodMode.Monthly ? fromDate.AddMonths(1) : fromDate.AddDays(1);
            }

            return(await Task.WhenAll(allTasks));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="fromDate"></param>
        /// <param name="toDate"></param>
        /// <param name="mode"></param>
        /// <param name="overrideCache"></param>
        /// <returns></returns>
        /// <exception cref="NotEnoughSampleException">Throw if sample return is less than 2 items.</exception>
        public async Task <IEnumerable <OpenExchangeRateResult> > FetchSampleData(DateTime fromDate, DateTime toDate, PeriodMode mode = PeriodMode.Monthly, bool overrideCache = false)
        {
            if (!overrideCache && _cache.InMemoryData.Any())
            {
                return(_cache.InMemoryData);
            }

            var result = await _openExchangeClient.GetExchangeRateHistoryPeriod(fromDate, toDate, mode);

            if (result.Count() < 2)
            {
                throw new NotEnoughSampleException();
            }

            _cache.InMemoryData.AddRange(result);

            return(result);
        }