Esempio n. 1
0
        /// <summary>
        /// Get weather map
        /// http://api.openweathermap.org
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public async Task <WeatherMapResponse <List <ResponseWeather> > > GetCityWeatherMap(string city)
        {
            var response        = new WeatherMapResponse <List <ResponseWeather> >();
            var objLilstWeather = new List <ResponseWeather>();

            using (var client = new HttpClient())
            {
                try
                {
                    client.BaseAddress = new Uri(WeatherMapConstants.BaseURL);
                    var apiResponse = await client.GetAsync($"/data/2.5/weather?q={city}&appid={WeatherMapConstants.APIKey}&units=metric");

                    apiResponse.EnsureSuccessStatusCode();
                    var stringResult = await apiResponse.Content.ReadAsStringAsync();

                    var rawWeather = JsonConvert.DeserializeObject <ResponseWeather>(stringResult);
                    objLilstWeather.Add(rawWeather);
                    response.Data         = objLilstWeather;
                    response.TotalRecords = Convert.ToString(objLilstWeather.Count);
                    response.Status       = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return(response);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get Wind speed
        /// Call WeatherMap web Api
        /// </summary>
        /// <returns></returns>
        public static WeatherMapResponse GetWindSpeed()
        {
            WeatherMapResponse weatherMapResponse = new WeatherMapResponse();

            try
            {
                using (var client = new HttpClient())
                {
                    WeatherMapRequest weatherMapRequest = new WeatherMapRequest()
                    {
                        CityName = WeatherMapConstants.City
                    };
                    var responseTask = client.GetAsync(ApiConstant.WebApUrl + ApiConstant.WindSpeedAction + weatherMapRequest.CityName);
                    responseTask.Wait();

                    var result = responseTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var readTask = result.Content.ReadAsStringAsync();
                        readTask.Wait();
                        var speed = readTask.Result;

                        var weatherInfo = JObject.Parse(speed).ToString();

                        var response = JsonConvert.DeserializeObject <WeatherMap>(weatherInfo);
                        weatherMapResponse.WindSpeed = response.Result.Data[0].Wind.Speed;
                        return(weatherMapResponse);
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionLogger exceptionLogger = new ExceptionLogger()
                {
                    ExceptionStackTrace = e.StackTrace,
                    ExceptionMessage    = e.Message,
                    LogTime             = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
                };
                weatherMapResponse.ExceptionLog = exceptionLogger;
            }

            return(weatherMapResponse);
        }