コード例 #1
0
ファイル: dal.cs プロジェクト: gadnandev/Material-Currency
        /// <summary>
        /// gets updated, live information about the supported currencies
        /// </summary>
        /// <returns>live information about the currencies</returns>
        public async Task <LiveCurrencyEntity> GetLiveCurrencyAsync()
        {
            string             url    = @"http://apilayer.net/api/live?access_key=" + GetKey() + "&currencies=EUR,GBP,ILS,BTC,JMD&format=1";
            LiveCurrencyEntity entity = new LiveCurrencyEntity();

            using (var wc = new WebClient()) {
                var jsonData = string.Empty;
                for (int i = 0; i < 4; i++) //try 4 times
                {
                    try {
                        jsonData = await wc.DownloadStringTaskAsync(url);

                        entity = JsonConvert.DeserializeObject <LiveCurrencyEntity>(jsonData);
                        if (jsonData.Contains("success\":true"))
                        {
                            break;
                        }
                    }
                    catch (Exception) {
                        throw new Exception("problem getting live information...");
                    }
                }
            }
            return(entity);
        }
コード例 #2
0
        /// <summary>
        /// gets updated current values of all the currency types
        /// </summary>
        /// <returns>an updated list of all the currency values</returns>
        public async Task <List <CurrencyFields> > GetLiveListAsync()
        {
            LiveCurrencyEntity res = await new Dal().GetLiveCurrencyAsync();

            return(await Task.Factory.StartNew(
                       () => (
                           from cur in Enum.GetNames(typeof(CurrencyType))
                           select new CurrencyFields {
                Code = cur, Value = 1 / res.GetValue(cur)
            }).ToList()
                       ));
        }