예제 #1
0
        private async Task <Models.WeatherForecast> FillWeatherForecastCache(string remoteIpAddress, string key)
        {
            var weatherClient = new WeatherAppClient();
            var weatherData   = await weatherClient.GetWeatherForecast(remoteIpAddress);

            if (!weatherData.IsSuccess || string.IsNullOrEmpty(weatherData.Data?.Today?.City))
            {
                throw new BadApiResponseException(weatherData.Error);
            }

            var now         = DateTime.UtcNow.Date;
            var itemToCache = new Models.WeatherForecast
            {
                City         = weatherData.Data.Today.City,
                State        = weatherData.Data.Today.State,
                DailyWeather = weatherData.Data.Daily
                               .Where(x => now.AddDays(-1) < x.UtcTime &&
                                      x.UtcTime < now.AddDays(4))
                               .Select(x => new WeatherInformation
                {
                    Description     = x.Description,
                    HighTemperature = x.HighTemperature,
                    LowTemperature  = x.LowTemperature,
                    WeatherTime     = x.UtcTime
                })
            };

            await UpsertKeyAsync(key, itemToCache);
            await SetKeyExpirationAsync(key, TimeSpan.FromHours(2));

            return(itemToCache);
        }
예제 #2
0
 public WeatherForecastController(ILogger <WeatherForecastController> logger, WeatherAppClient weatherAppClient)
 {
     _logger           = logger;
     _weatherAppClient = weatherAppClient;
 }