/// <summary> /// Implements the following business logic: /// 1. Interact with external web api called OpenWeather /// 2. fetches weather details in the format of json text /// 3. Maps the json data with model object called Weather /// </summary> /// <param name="strCityName"></param> /// <param name="strcountryName"></param> /// <returns>Returns model obejct Weather</returns> public async System.Threading.Tasks.Task <Weather> GetWeatherFromExternalAPI(string cityName, string countryname) { Weather weather = null; try { string apiKey = ConfigurationManager.AppSettings[Constants.OpenWeather_API_KEY]; using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { //Replace any white spaces in the value of input parameters countryname = System.Text.RegularExpressions.Regex.Replace(countryname, @"\s+", "%20"); cityName = System.Text.RegularExpressions.Regex.Replace(cityName, @"\s+", "%20"); //Construct url with registered API Key, valid key has to be configured in the web.config file string url = string.Format("{0}{1},{2}{3}{4}", "?q=", cityName, countryname, "&APPID=", apiKey); client.BaseAddress = new Uri(ConfigurationManager.AppSettings[Constants.OpenWeather_URI]); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var jsonString = await client.GetStringAsync(url).ConfigureAwait(false); weather = GlobalWeatherHelper.PopulateWeatherModelFromJsonText(jsonString); } } catch (HttpRequestException ex) { mLog.Error(ex.InnerException); throw ex; } catch (Exception ex) { mLog.Error(ex.InnerException); throw ex; } return(weather); }