コード例 #1
0
        public async Task UpdateCurrenciesDictionary(Dictionary <string, Coin> dictionary)
        {
            #region GET
            string currencies = await client.GetStringAsync("http://apilayer.net/api/live?access_key=" + access_key + "&source=USD&format=1");

            if (names == "")
            {
                names = await client.GetStringAsync("http://apilayer.net/api/list?access_key=" + access_key + "&&format=1");
            }
            #endregion

            Console.WriteLine("in async");
            JObject currenciesJson = JObject.Parse(currencies);
            JObject namesJson      = JObject.Parse(names);
            dictionary.Clear();
            List <Coin> Currencies = new List <Coin>();

            foreach (var item in new List <string> {
                "AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BYR", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMK", "ZMW", "ZWL"
            })
            {
                string value = (((JObject)currenciesJson.GetValue("quotes")).GetValue("USD" + item)).ToString();
                string name  = (((JObject)namesJson.GetValue("currencies")).GetValue(item)).ToString();
                Coin   tmp   = new Coin(decimal.Parse(value, NumberStyles.Float), name, item);
                dictionary.Add(item, tmp);
                Currencies.Add(tmp);
            }

            using (DBmanager context = new DBmanager())
            {
                context.Coins.AddRange(Currencies);
                await context.SaveChangesAsync();
            }
        }
コード例 #2
0
        public async Task GetCurrenciesDictionaryByDate(Dictionary <string, HistoricalCoin> dictionary, DateTime dateTime)
        {
            try
            {
                string date = correctDate(dateTime);

                #region Check if record already exist

                using (var ctx = new DBmanager())
                {
                    var query = ctx.HistoricalCoins.Where(s => s.DateTime.Equals(dateTime)).FirstOrDefault <HistoricalCoin>();
                    if (query != null)
                    {
                        Console.WriteLine("this record is already exist");
                        return;
                    }
                }

                #endregion

                #region GET
                string responseString = await client.GetStringAsync("http://apilayer.net/api/historical?access_key=" + access_key + "&date=" + date + "&source=USD&format=1");

                if (names == "")
                {
                    names = await client.GetStringAsync("http://apilayer.net/api/list?access_key=" + access_key + "&&format=1");
                }
                #endregion

                Console.WriteLine("in async");
                JObject jObject   = JObject.Parse(responseString);
                JObject namesJson = JObject.Parse(names);
                dictionary.Clear();
                List <HistoricalCoin> Currencies = new List <HistoricalCoin>();
                string timestamp = (((JObject)jObject).GetValue("timestamp")).ToString();

                foreach (var item in new List <string> {
                    "AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BYR", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMK", "ZMW", "ZWL"
                })
                {
                    string name = (((JObject)namesJson.GetValue("currencies")).GetValue(item)).ToString();
                    try
                    {
                        string         value = (((JObject)jObject.GetValue("quotes")).GetValue("USD" + item)).ToString();
                        HistoricalCoin tmp   = new HistoricalCoin(decimal.Parse(value, NumberStyles.Float), name, item, dateTime);
                        dictionary.Add(item, tmp);
                        Currencies.Add(tmp);
                    }
                    catch (Exception)
                    {
                        dictionary.Add(item, new HistoricalCoin(-1, name, item, dateTime));
                    }
                }

                using (DBmanager context = new DBmanager())
                {
                    context.HistoricalCoins.AddRange(Currencies);
                    await context.SaveChangesAsync();
                }

                Console.WriteLine("this record added succesfully exist:    " + date);
                Console.WriteLine(DateTime.Now);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }