コード例 #1
0
        public static async Task <NbRbRates> GetRatesForDate(DateTime date)
        {
            string uri      = "http://www.nbrb.by/API/ExRates/Rates?onDate=" + $"{date:yyyy-M-d}" + "&Periodicity=0";
            var    response = await MyRequest.GetAsync(uri);

            var nbList = (List <NbRbSiteRate>)JsonConvert.DeserializeObject(response, typeof(List <NbRbSiteRate>));

            if (nbList.Count == 0)
            {
                return(null);
            }
            var result  = new NbRbRates();
            var usdRate = nbList.First(c => c.Cur_Abbreviation == "USD");

            result.Usd.Value = usdRate.Cur_OfficialRate;
            result.Usd.Unit  = usdRate.Cur_Scale;
            var euroRate = nbList.First(c => c.Cur_Abbreviation == "EUR");

            result.Euro.Value = euroRate.Cur_OfficialRate;
            result.Euro.Unit  = euroRate.Cur_Scale;
            var rubRate = nbList.First(c => c.Cur_Abbreviation == "RUB");

            result.Rur.Value = rubRate.Cur_OfficialRate;
            result.Rur.Unit  = rubRate.Cur_Scale;
            return(result);
        }
コード例 #2
0
        public static async Task <double> GetRateForDate(DateTime date)
        {
            string uri      = "http://www.cbr.ru/currency_base/daily/?date_req=" + $"{date:dd.MM.yyyy}";
            var    response = await MyRequest.GetAsync(uri);

            var index   = response.IndexOf("USD", StringComparison.CurrentCulture);
            var rateStr = response.Substring(index + 65, 7);

            return(double.TryParse(rateStr, NumberStyles.Any, new CultureInfo("ru-RU"), out double rate) ? rate : 0);
        }