Exemple #1
0
        /// <summary>
        /// Retrieves multiple days forecast by city name
        /// </summary>
        /// <param name="cityName"></param>
        /// <param name="countryCode"></param>
        /// <returns cref="ForecastModel"></returns>
        public async Task <ForecastModel> GetForecastByCityName(string cityName, string countryCode)
        {
            try
            {
                if (String.IsNullOrEmpty(cityName) || String.IsNullOrEmpty(countryCode))
                {
                    throw new ArgumentNullException("Invalid input parameters.");
                }
                //returns uri to get 5*24 hours forecast by city name and country code
                Uri forecastRequestUri = _externalApiUriProvider.GetForecastByCityUri(cityName, countryCode);

                //get forecast from external api or cache
                var forecast = await _forecastApiRetriever.GetData(forecastRequestUri, cacheForecastContext);

                //convert result from external api to ForecastModel
                return(_forecastResponseConverter.Convert(forecast));
            }
            catch (HttpResponseWithStatusCodeException ex)
            {
                _logger.LogError($"ApiMessage:{ex.ApiMessage}", ex);
                throw ex;
            }
            catch (Exception ex)
            {
                _logger.LogError("Something went wrong during getting data from external api.", ex);
                throw new HttpResponseWithStatusCodeException(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
        /// <summary>
        /// Retrieves current weather by city name
        /// </summary>
        /// <param name="cityName"></param>
        /// <param name="countryCode"></param>
        /// <returns cref="CurrentWeatherModel"></returns>
        public async Task <CurrentWeatherModel> GetCurrentWeatherByCityName(string cityName, string countryCode)
        {
            try
            {
                if (String.IsNullOrEmpty(cityName) || String.IsNullOrEmpty(countryCode))
                {
                    throw new ArgumentNullException("Invalid input parameters.");
                }
                //generate uri to get current weather by city name and country code
                Uri weatherRequestUri = _externalApiUriProvider.GetCurrentWeatherByCityUri(cityName, countryCode);

                //get weather data from external api or cache
                var weather = await _weatherApiRetriever.GetData(weatherRequestUri, cacheWeatherContext);

                //convert result from external api to CurrentWeatherModel
                return(_weatherResponseConverter.Convert(weather));
            }
            catch (HttpResponseWithStatusCodeException ex)
            {
                _logger.LogError($"ApiMessage:{ex.ApiMessage}", ex);
                throw ex;
            }
            catch (Exception ex)
            {
                _logger.LogError("Something went wrong during getting data from external api.", ex);
                throw new HttpResponseWithStatusCodeException(HttpStatusCode.InternalServerError, ex.Message);
            }
        }