public List <CurrencyDTO> GetAllCurrencies() { List <Currency> currencies = null; string urlPath = _apiUrl; using (var client = new HttpClient()) using (var response = client.GetAsync(urlPath).Result) using (var content = response.Content) { if (response.IsSuccessStatusCode) { try { var apiResponse = content.ReadAsStringAsync().Result; currencies = JsonConvert.DeserializeObject <List <Currency> >(apiResponse); } catch (JsonSerializationException) { // Add additional exception handling here if we do not wont to just return null object. } } } List <CurrencyDTO> result = new List <CurrencyDTO>(); foreach (var currency in currencies) { if (currency != null) { result.Add(CurrencyDTO.ConvertToCurrencyDTO(currency)); } } return(result); }
public async Task <CurrencyDTO> GetCurrencyByIdAsync(string currencyId) { Currency currency = null; string urlPath = _apiUrl + currencyId; using (var client = new HttpClient()) using (var response = await client.GetAsync(urlPath)) using (var content = response.Content) { if (response.IsSuccessStatusCode) { try { var apiResponse = await content.ReadAsStringAsync(); var serializedResponse = JsonConvert.DeserializeObject <List <Currency> >(apiResponse); currency = serializedResponse[0]; } catch (JsonSerializationException) { // Add additional exception handling here if we do not wont to just return null object. } } } return(CurrencyDTO.ConvertToCurrencyDTO(currency)); }