コード例 #1
0
        public void GetFiiiCoinInfo(decimal btcToUSD, List <CurrencyConverterModel> currencyconvertermodel)
        {
            try
            {
                var cryptoCurr2 = cryptoCurrDAC.GetByCode("FIII");
                if (cryptoCurr2 == null)
                {
                    Log.Error(MethodBase.GetCurrentMethod().Name + "(): FIII is not found in database.");
                    throw new Exception("Error: FIII is not found in database");
                }

                if ((from x in currenciesDAC.GetAll()
                     where string.Compare(x.Code, "USD", StringComparison.OrdinalIgnoreCase) == 0
                     select x).FirstOrDefault() != null)
                {
                    decimal fiiiBTCPrice = GetFiiiToBTCPriceInfo();
                    if (fiiiBTCPrice != decimal.Zero)
                    {
                        decimal           fiiiUSD      = decimal.Multiply(fiiiBTCPrice, btcToUSD);
                        List <Currencies> currencylist = currenciesDAC.GetAll();
                        if (currencylist.Count == 0)
                        {
                            throw new Exception("Error: Currency table does not contains any data.");
                        }

                        foreach (Currencies item in currencylist)
                        {
                            CurrencyConverterModel usdtocurrencyrate = (from x in currencyconvertermodel
                                                                        where string.Compare(x.CurrencyPairTo, item.Code, true) == 0
                                                                        select x).FirstOrDefault();
                            if (usdtocurrencyrate != null)
                            {
                                if (!UpdateERCPriceInfo(item.ID, cryptoCurr2.Id,
                                                        Math.Round(usdtocurrencyrate.Value * fiiiUSD, 2)))
                                {
                                    Log.Error(MethodBase.GetCurrentMethod().Name +
                                              "(): Error: Unable to insert/update PriceInfo for FIII/" + item.Code +
                                              ".");
                                }
                            }
                            else
                            {
                                Log.Error(MethodBase.GetCurrentMethod().Name + "(): Unable to update.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(MethodBase.GetCurrentMethod().Name + "(): Fail with message: " + ex.Message +
                          ", StackTrace: " + ex.StackTrace);
            }
        }
コード例 #2
0
        public CurrencyConverterModel ConvertMultipleCurrency(string fromCurrency, string toCurrency1)
        {
            try
            {
                //CurrencyConverterModel dataList = new CurrencyConverterModel();
                //string currencyPairTwo = "";
                string jsonCurrencyParam = fromCurrency + "_" + toCurrency1;
                string apiURL            = "api/v6/convert?q=" + jsonCurrencyParam;

                //if (!string.IsNullOrWhiteSpace(toCurrency2))
                //{
                //    currencyPairTwo = fromCurrency + "_" + toCurrency2;
                //    apiURL = apiURL + "," + currencyPairTwo;
                //}

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(CurrencyCoverterAPIURL);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = client.GetAsync(apiURL).Result;

                if (response.IsSuccessStatusCode)
                {
                    var jsonData = response.Content.ReadAsStringAsync().Result;

                    // First currency pair to convert
                    var jsonResult = (decimal)JObject.Parse(jsonData)["results"][jsonCurrencyParam]["val"];

                    CurrencyConverterModel data = new CurrencyConverterModel
                    {
                        CurrencyPairFrom = fromCurrency,
                        CurrencyPairTo   = toCurrency1,
                        Value            = jsonResult
                    };

                    //dataList.Add(data);

                    // Second currency pair to convert
                    //if (!string.IsNullOrWhiteSpace(toCurrency2))
                    //{
                    //    var jsonResult2 = (decimal)JObject.Parse(jsonData)["results"][currencyPairTwo]["val"];

                    //    CurrencyConverterModel data2 = new CurrencyConverterModel
                    //    {
                    //        CurrencyPairFrom = fromCurrency,
                    //        CurrencyPairTo = toCurrency2,
                    //        Value = jsonResult2
                    //    };

                    //    dataList.Add(data2);
                    //}

                    return(data);
                }

                throw new Exception("Convert currency failed, FromCurrency: " + fromCurrency + ", ToCurrency: " + toCurrency1);
            }
            catch (Exception ex)
            {
                Log.Info(MethodBase.GetCurrentMethod().Name + "Message: " + ex.Message + "StackTrace: " + ex.StackTrace);
                throw new Exception("Message: " + ex.Message + "StackTrace: " + ex.StackTrace);
            }
        }