예제 #1
0
        public static async Task <decimal> ConvertValue(decimal value, string firstCurrency, string secondCurrency)
        {
            string url = "http://download.finance.yahoo.com/d/quotes?f=sl1d1t1&s=" +
                         firstCurrency + secondCurrency + "=X";
            string result     = "";
            var    conversion = new CurrencyCall("", 0, "", "");

            try
            {
                var httpClient = new HttpClient();
                httpClient.Timeout = TimeSpan.FromSeconds(5);
                var request  = new HttpRequestMessage(HttpMethod.Get, url);
                var response = await httpClient.SendAsync(request);

                result = await response.Content.ReadAsStringAsync();

                string[] answer = result.Split(',');
                conversion = new CurrencyCall(answer[0].Replace("\"", ""), decimal.Parse(answer[1]), answer[2].Replace("\"", ""), answer[3].Replace("\"", "").Replace("\n", ""));
                //save time by saving currency rates
                calls.Add(conversion);
                Database.SaveCurrencyCall(conversion);
            }
            catch (Exception exception)
            {
                string currenciesToConvert = firstCurrency + secondCurrency + "=X";
                Debug.WriteLine("OFFLINE CONVERSION");
                Debug.WriteLine(exception);
                conversion = Database.GetCurrencyCallRate(currenciesToConvert).ElementAt(0);
            }

            return(decimal.Multiply(conversion.rate, value));
        }
예제 #2
0
        public int SaveCurrencyCall(CurrencyCall item)
        {
            List <CurrencyCall> calls = database.Query <CurrencyCall>("SELECT * FROM [CurrencyCall] WHERE [currencies] = ?", item.currencies);

            if (item.ID != 0 || calls.Count() > 0)
            {
                database.Update(item);
                return(item.ID);
            }
            else
            {
                return(database.Insert(item));
            }
        }