Ejemplo n.º 1
0
        public async Task <IActionResult> GetForecast(string city = "Dnipro")
        {
            string stringResult;

            try
            {
                stringResult = await OpenWeather.GetForecast(city, "forecast", _configuration["MyKey"]);
            }
            catch (HttpRequestException httpRequestException)
            {
                if (httpRequestException.Message.Contains("404 (Not Found)"))
                {
                    return(NotFound(httpRequestException.Message));
                }
                if (httpRequestException.Message.Contains("401 (Unauthorized)"))
                {
                    return(Unauthorized(httpRequestException.Message));
                }
                return(BadRequest(httpRequestException.Message));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }

            var rawWeather = JsonSerializer.Deserialize <OpenWeatherResponses>(stringResult);
            var forecasts  = rawWeather.Forecasts.Select(forecast => new ForecastModel(forecast)).ToList();

            return(Ok(forecasts));
        }