public object Calculate(RateDto rateDto) { object response = string.Empty; try { Rate rateValues = GetRatesInstance(rateDto); HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://free.currencyconverterapi.com"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //currencylayer only supports USD as the source/from currency HttpResponseMessage apiResponse = client.GetAsync("api/v5/convert?" + "q=" + rateValues.CurrencyOrigin + "_" + rateValues.CurrencyDestination).Result; // Blocking call if (apiResponse.IsSuccessStatusCode) { string jsonResponse = apiResponse.Content.ReadAsStringAsync().Result; var data = (JObject)JsonConvert.DeserializeObject(jsonResponse); response = data.Last.First.First.Last.SelectToken("val").Value <object>(); } } catch (Exception ex) { response = ex.ToString(); } return(response); }
public object CalculateRate(RateDto rateDto) { var rate = new Rate( rateDto.ValueOrigin, rateDto.CurrencyOrigin, rateDto.CurrencyDestination, rateDto.FormatMsg, rateDto.ApiKey); return(rateService.Calculate(rate)); }
public static Rate GetRatesInstance(RateDto inputRates) { Rate resultRates = null; try { //Get query string values string apikey = inputRates.ApiKey; double valueOrigin = inputRates.ValueOrigin; //string provider = inputRates.Provider; string currency = inputRates.CurrencyOrigin; string symbol = inputRates.CurrencyDestination; string format = inputRates.FormatMsg; /*if (string.IsNullOrEmpty(provider)) * { * provider = "Fixer"; * }*/ if (string.IsNullOrEmpty(currency)) { currency = "USD"; } if (string.IsNullOrEmpty(symbol)) { symbol = "BRL"; } if (string.IsNullOrEmpty(format)) { format = "text"; } //provider = provider.Trim().ToUpper(); currency = currency.Trim().ToUpper(); symbol = symbol.Trim().ToUpper(); format = format.Trim().ToUpper(); resultRates = new Rate(valueOrigin, currency, symbol, format, apikey); /*resultRates.ApiKey = apikey; * resultRates.Format = format; * resultRates.Fr = currency; * resultRates.To = symbol; * resultRates.Format = format; * resultRates.Provider = provider;*/ } catch { resultRates = null; } return(resultRates); }