Пример #1
0
        public OwForecast GetForecastPageData(int?owCityId = null)
        {
            //if (!owCityId.HasValue)
            //{
            //    owCityId = defaultOwCityId;
            //}

            OwForecast owForecast = null;

            var lastFetchUtc = CacheUtil.GetLastFetchUtc();

            if (owCityId.HasValue)
            {
                if (!lastFetchUtc.HasValue || lastFetchUtc.Value < DateTime.UtcNow.AddMinutes(-minutesBetweenRequests))
                {
                    var apiUrl = string.Format(apiUrlTemplate, owCityId);
                    var result = httpClient.GetAsync(apiUrl).Result;

                    if (result.IsSuccessStatusCode)
                    {
                        var resultString = result.Content.ReadAsStringAsync().Result;
                        owForecast = JsonConvert.DeserializeObject <OwForecast>(resultString);

                        lastFetchUtc = owForecast.FetchUtc = DateTime.UtcNow;

                        CacheUtil.SaveForecast(owForecast);
                    }
                }
                else
                {
                    owForecast = CacheUtil.GetForecast(owCityId.Value);
                }
            }

            if (owForecast == null)
            {
                owForecast = new OwForecast();
            }

            owForecast.AvailableFetchUtc = lastFetchUtc.HasValue ? lastFetchUtc.Value.AddMinutes(minutesBetweenRequests) : DateTime.UtcNow;

            return(owForecast);
        }