예제 #1
0
        public async Task <List <WeatherData> > GetWeatherData(WeatherDataRequestEntity request)
        {
            if (request.StartDate > request.EndDate)
            {
                return(new List <WeatherData>());
            }

            // WeatherSource API has a limit of 1 year for the date range
            var currentStartDate = request.StartDate;
            var currentEndDate   = GetCurrentEndDate(currentStartDate, request.EndDate);
            var returnList       = new List <WeatherData>();

            do
            {
                var weatherDataEntities = await _weatherSourceServiceAgent.GetWeatherData(new WeatherDataRequestEntity
                {
                    StartDate = currentStartDate,
                    EndDate   = currentEndDate,
                    Latitude  = request.Latitude,
                    Longitude = request.Longitude
                });

                returnList.AddRange(_mapper.Map <List <WeatherData> >(weatherDataEntities));
                currentStartDate = currentEndDate.AddDays(1);
                currentEndDate   = GetCurrentEndDate(currentStartDate, request.EndDate);
            } while (currentEndDate > currentStartDate);

            return(returnList);
        }
예제 #2
0
        public async Task <List <WeatherDataEntity> > GetWeatherData(WeatherDataRequestEntity request)
        {
            using (var weatherSourceClient = _httpClientFactory.CreateClient(HttpClientEnum.WeatherSource.ToString()))
            {
                var requestUrl =
                    $"points/{request.Latitude},{request.Longitude}/history.json?timestamp_between={request.StartDate.ToString("s", System.Globalization.CultureInfo.InvariantCulture)},{request.EndDate.ToString("s", System.Globalization.CultureInfo.InvariantCulture)}&fields=tempAvg,relHumAvg,spcHumAvg";
                var response = await weatherSourceClient.GetAsync(requestUrl);

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(new List <WeatherDataEntity>());
                }

                response.EnsureSuccessStatusCode();
                return(JsonConvert.DeserializeObject <List <WeatherDataEntity> >(await response.Content.ReadAsStringAsync()));
            }
        }