public Double GetCurrencyRate(String fromCurrency, String toCurrency, Double amount) { Double value = default(Double); WebClient web = new WebClient(); CommonValidation validation = new CommonValidation(); try { if (validation.IsCurrencyCodeValid(providerName, fromCurrency) && validation.IsCurrencyCodeValid(providerName, toCurrency)) { const string urlPattern = "http://finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1"; string url = String.Format(urlPattern, fromCurrency, toCurrency); // Get response as string string response = new WebClient().DownloadString(url); // Convert string to number decimal exchangeRate = decimal.Parse(response, System.Globalization.CultureInfo.InvariantCulture); value = Convert.ToDouble(exchangeRate) * amount; } } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(ex.Message), ReasonPhrase = ex.Message }; throw new HttpResponseException(resp); } return(value); }
public ICurrencyConverter GetServiceProvider(string serviceProviderName) { ICurrencyConverter serviceProvider = null; try { ServiceProviderEnum provider = EnumUtility.ParseEnum <ServiceProviderEnum>(serviceProviderName.ToUpper()); //If Enumeration parsing is not completed then throw the exception // check the currency provider is active is CommonValidation validation = new CommonValidation(); // if service provider is active then create the object if (validation.IsServiceProviderActive(serviceProviderName)) { switch (provider) { case ServiceProviderEnum.GOOGLE: serviceProvider = new GoogleServiceProvider(); break; case ServiceProviderEnum.OANDA: serviceProvider = new OandaServiceProvider(); break; case ServiceProviderEnum.XE: serviceProvider = new XEServiceProvider(); break; case ServiceProviderEnum.YAHOO: serviceProvider = new YahooServiceProvider(); break; } } } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(serviceProviderName + "provider Not Found"), ReasonPhrase = serviceProviderName + " Not Found" }; throw new HttpResponseException(resp); } return(serviceProvider); }