Exemplo n.º 1
0
        public async Task <CurrencyInfo> GetSeriesOfLatestExchangeRates(string currencyCode,
                                                                        int topCount = 1)
        {
            if (topCount < 1 || topCount > GetSeriesOfExchangeRatesTopCount)
            {
                throw new ArgumentException(
                          $"Wrong number of exchange rates to download. Number should be between 1 and {GetSeriesOfExchangeRatesTopCount}");
            }

            HttpResponseMessage response = await httpClient.GetAsync($"exchangerates/rates/c/{currencyCode}/last/{topCount}/");

            CurrencyInfo currencyData = null;

            if (response.IsSuccessStatusCode)
            {
                string body = await response.Content.ReadAsStringAsync();

                ApiCurrency       apiData           = JsonConvert.DeserializeObject <ApiCurrency>(body);
                CurrencyProcessor currencyProcessor = new CurrencyProcessor(apiData);

                currencyData = new CurrencyInfo(currencyProcessor);
            }

            return(currencyData);
        }
Exemplo n.º 2
0
 public CurrencyProcessor(ApiCurrency apiCurrencyData)
 {
     Code          = apiCurrencyData.Code;
     Name          = apiCurrencyData.Name;
     StartDate     = apiCurrencyData.ExchangeRates.First().EffectiveDate;
     EndDate       = apiCurrencyData.ExchangeRates.Last().EffectiveDate;
     ExchangeRates = apiCurrencyData.ExchangeRates;
 }
Exemplo n.º 3
0
        public async Task <CurrencyInfo> GetSeriesOfExchangeRatesFromGivenPeriod(string currencyCode,
                                                                                 DateTime startDate,
                                                                                 DateTime endDate)
        {
            if (startDate > endDate) // swap dates if start date is greater than end date
            {
                DateTime temp = startDate;
                startDate = endDate;
                endDate   = temp;
            }

            if (startDate < CurrencyExchangeRatesCollectionStartDate)
            {
                throw new ArgumentException(
                          $"Start date cannot be earlier than {CurrencyExchangeRatesCollectionStartDate: d}");
            }

            if (endDate > DateTime.Now)
            {
                throw new ArgumentException($"End date cannot be later than current date");
            }

            TimeSpan givenPeriod = endDate - startDate;

            if (givenPeriod.Days > MaximumNumberOfDaysForDownloadingData)
            {
                throw new ArgumentException($"Given period cannot exceed {MaximumNumberOfDaysForDownloadingData} days");
            }

            HttpResponseMessage response = await httpClient.GetAsync(
                $"exchangerates/rates/c/{currencyCode}/{startDate: yyyy-MM-dd}/{endDate: yyyy-MM-dd}/");

            CurrencyInfo currencyData = null;

            if (response.IsSuccessStatusCode)
            {
                string body = await response.Content.ReadAsStringAsync();

                ApiCurrency       apiData           = JsonConvert.DeserializeObject <ApiCurrency>(body);
                CurrencyProcessor currencyProcessor = new CurrencyProcessor(apiData);

                currencyData = new CurrencyInfo(currencyProcessor);
            }

            return(currencyData);
        }