public ExchangeRateResult GetExchangeRate(string baseCurrencyCode, string targetCurrencyCode, DateTime conversionDateUtc) { if (string.IsNullOrWhiteSpace(baseCurrencyCode) || string.IsNullOrWhiteSpace(targetCurrencyCode)) { throw new ArgumentException("The base and target currency codes must be provided"); } if (conversionDateUtc == default(DateTime)) { throw new ArgumentException("Please prove a valid conversion date."); } if (baseCurrencyCode.Equals(targetCurrencyCode, StringComparison.InvariantCultureIgnoreCase)) { return(new ExchangeRateResult { Base = baseCurrencyCode, Date = DateTime.UtcNow, Rate = 1.0f, Target = targetCurrencyCode }); } // if the conversion date is in the future we will use today if (conversionDateUtc > DateTime.UtcNow) { conversionDateUtc = DateTime.UtcNow; } try { var result = MSWebRequest.RequestJson( string.Format(FIXER_RATE_URL_BASE, conversionDateUtc.ToString("yyyy-MM-dd"), baseCurrencyCode)); var rate = result.rates[targetCurrencyCode]; var exchangeRate = new ExchangeRateResult { Base = result.@base, Date = DateTime.ParseExact(result.date.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None), Target = targetCurrencyCode, Rate = (float)result.rates[targetCurrencyCode] }; return(exchangeRate); } catch (Exception e) { throw new RequestExchangeRateException( $"Failed to request exchange rate. From {baseCurrencyCode} to {targetCurrencyCode} on {conversionDateUtc.ToShortDateString()}.", e); } }
public JsonResult ExchangeRate() { ExchangeRateResult rate = new ExchangeRateResult(); XmlDocument xmlExchange = new XmlDocument(); xmlExchange.Load("http://www.tcmb.gov.tr/kurlar/today.xml"); decimal dolar = Convert.ToDecimal(xmlExchange.SelectSingleNode(string.Format("Tarih_Date/Currency[@Kod='{0}']/ForexSelling", "USD")).InnerText.Replace('.', ',')); decimal Euro = Convert.ToDecimal(xmlExchange.SelectSingleNode(string.Format("Tarih_Date/Currency[@Kod='{0}']/ForexSelling", "EUR")).InnerText.Replace('.', ',')); rate.Result = true; rate.Dollar = dolar; rate.Euro = Euro; return(Json(rate, JsonRequestBehavior.AllowGet)); }
private ExchangeRateResult ConvertToExchangeRateResult(ExchangeRate excRate) { var result = new ExchangeRateResult { Documentation = excRate.documentation, Result = excRate.result, TermsOfUse = excRate.terms_of_use, TimeLastUpdate = excRate.time_last_update, TimeNextUpdate = excRate.time_next_update, TimeZone = excRate.result, ConversionRates = new Dictionary <string, double>() }; Type myClassType = excRate.conversion_rates.GetType(); PropertyInfo[] properties = myClassType.GetProperties(); foreach (PropertyInfo property in properties) { result.ConversionRates.Add(property.Name, (double)property.GetValue(excRate.conversion_rates, null)); } return(result); }