public IEnumerable <ExchangeRate> GetExchangeRates(IEnumerable <Currency> currencies) { var requiredCodesCur = currencies.Select(c => c.Code).ToList(); var relevantExcRates = new List <ExchangeRate>(); Currency comparedCurency = new Currency(targetCurrency); var ratesRequest = new RateRequestModel() { Date = DateTime.Today.ToString(AcceptedDateFormat), RequestUrl = ExchangeSourceUrl }; ExchangeRateResponse ratesResponse = ratesReqClient.GetRatesData(ratesRequest); if (ratesResponse != null && ratesResponse.Table.RatesRows.Length > 0) { foreach (var rate in ratesResponse.Table.RatesRows) { if (requiredCodesCur.Contains(rate.Code)) { relevantExcRates.Add(new ExchangeRate(new Currency(rate.Code), comparedCurency, (decimal)Convert.ToDecimal(rate.Rate) / Convert.ToInt32(rate.Amount))); } else { continue; } } } return(relevantExcRates); }
public ExchangeRateResponse GetRatesData(RateRequestModel req, CancellationToken cancelToken = default(CancellationToken)) { string fullUrl = req.RequestUrl + req.Date; ExchangeRateResponse ratesResponse = null; try { using (client = new HttpClient()) { var response = client.GetAsync(fullUrl).Result; if (response.StatusCode == HttpStatusCode.OK) { var responseXML = response.Content.ReadAsStringAsync().Result; var bytes = Encoding.UTF8.GetBytes(responseXML); var stream = new MemoryStream(bytes); XmlSerializer _serializer = new XmlSerializer(typeof(ExchangeRateResponse)); using (XmlReader reader = XmlReader.Create(stream)) { ratesResponse = (ExchangeRateResponse)_serializer.Deserialize(reader); reader.Close(); } return(ratesResponse); } } } catch (Exception e) { Console.Error.WriteLine("Exception: " + e.Message); } return(null); }