Example #1
0
        public void Api_Submit_ValidWeatherRequest_ReturnsForecasts(
            int woeId,
            int expectedCount,
            ApiProxy apiProxy,
            IWeatherRequest weatherRequest,
            IWeatherResponse weatherResponse)
        {
            $"Given a woeId value of {woeId}"
            .x(() => weatherRequest = new WeatherRequest {
                WoeId = woeId
            });

            "And an ApiProxy"
            .x(() => apiProxy = new ApiProxy(_metaWeatherService));

            "When the weather request is submitted"
            .x(
                async() => weatherResponse =
                    await apiProxy.SubmitWeatherRequest(weatherRequest).ConfigureAwait(false));

            $"Then the weather response should return StatusCode HttpStatusCode.OK), and contain {expectedCount} Forecasts"
            .x(
                () =>
            {
                using (new AssertionScope())
                {
                    weatherResponse.StatusCode.Should().Be(HttpStatusCode.OK);
                    weatherResponse.Forecasts.Should().HaveCount(expectedCount);
                }
            });
        }
Example #2
0
        public void Api_Submit_InvalidWeatherRequest_ReturnsNotFound(
            int woeId,
            ApiProxy apiProxy,
            IWeatherRequest weatherRequest,
            IWeatherResponse weatherResponse)
        {
            $"Given a woeId value of {woeId}"
            .x(() => weatherRequest = new WeatherRequest {
                WoeId = woeId
            });

            "And an ApiProxy"
            .x(() => apiProxy = new ApiProxy(_metaWeatherService));

            "When the weather request is submitted"
            .x(
                async() => weatherResponse =
                    await apiProxy.SubmitWeatherRequest(weatherRequest).ConfigureAwait(false));

            "Then the weather response should return StatusCode 404, and Forecasts should be empty"
            .x(
                () =>
            {
                using (new AssertionScope())
                {
                    weatherResponse.StatusCode.Should().Be(HttpStatusCode.NotFound);
                    weatherResponse.Forecasts.Should().BeNullOrEmpty();
                }
            });
        }
 public TravelersController(IRepositoryWrapper repo, IWeatherRequest weatherRequest, ISearchRequest searchRequest, IGoogleServices googleServices, IHikingProject hikingService)
 {
     _repo           = repo;
     _searchRequest  = searchRequest;
     _googleServices = googleServices;
     _hikingService  = hikingService;
     _weatherRequest = weatherRequest;
 }
Example #4
0
        private Uri BuildCompleteRequest(IWeatherRequest request)
        {
            var builder = new UriBuilder(_config.BaseUri)
            {
                Query = request.Build()
            };

            return(builder.Uri);
        }
Example #5
0
        //public async Task<WeatherData> GetDataAsync(IWeatherRequest request)
        //{
        //    var requestUri = BuildCompleteRequest(request);
        //    var client = new WebClient();
        //    var task = await client.DownloadStringTaskAsync(requestUri);
        //    return await _parser.Parse(task);
        //}
        public WeatherData GetData(IWeatherRequest request)
        {
            var requestUri = BuildCompleteRequest(request);

            var client = new WebClient();
            var data = client.DownloadString(requestUri);
            var task = _parser.Parse(data);
            task.Wait(5000);
            return task.Result;
        }
Example #6
0
        //public async Task<WeatherData> GetDataAsync(IWeatherRequest request)
        //{
        //    var requestUri = BuildCompleteRequest(request);

        //    var client = new WebClient();
        //    var task = await client.DownloadStringTaskAsync(requestUri);
        //    return await _parser.Parse(task);
        //}

        public WeatherData GetData(IWeatherRequest request)
        {
            var requestUri = BuildCompleteRequest(request);

            var client = new WebClient();
            var data   = client.DownloadString(requestUri);
            var task   = _parser.Parse(data);

            task.Wait(5000);
            return(task.Result);
        }
        public WeatherData GetData(IWeatherRequest request)
        {
            var location = string.Join(",",
                request.Location.Build().Select(x => string.Format("{0}{1}", x.Key, x.Value)).ToArray());

            var times = string.Format("Start:{0}End:{1}",
                request.StartTimeUtc.HasValue ? request.StartTimeUtc.Value.ToLongDateString() : string.Empty,
                request.EndTimeUtc.HasValue ? request.EndTimeUtc.Value.ToLongDateString() : string.Empty);

            var key = MakeKey(location + times);
            return Get(key, () => _decorated.GetData(request));
        }
Example #8
0
        public WeatherData GetData(IWeatherRequest request)
        {
            var location = string.Join(",",
                                       request.Location.Build().Select(x => string.Format("{0}{1}", x.Key, x.Value)).ToArray());

            var times = string.Format("Start:{0}End:{1}",
                                      request.StartTimeUtc.HasValue ? request.StartTimeUtc.Value.ToLongDateString() : string.Empty,
                                      request.EndTimeUtc.HasValue ? request.EndTimeUtc.Value.ToLongDateString() : string.Empty);

            var key = MakeKey(location + times);

            return(Get(key, () => _decorated.GetData(request)));
        }
Example #9
0
        public async Task <WeatherResponse> SubmitWeatherRequest(IWeatherRequest weatherRequest)
        {
            try
            {
                using (var apiResponse = await _metaWeatherService.GetWeatherByWoeId(weatherRequest.WoeId)
                                         .ConfigureAwait(false))
                {
                    if (!apiResponse.IsSuccessStatusCode)
                    {
                        return(new WeatherResponse {
                            StatusCode = apiResponse.StatusCode, Forecasts = null
                        });
                    }

                    var forecasts = apiResponse.Content.Forecasts;

                    if (forecasts.Count == 0)
                    {
                        return(new WeatherResponse {
                            StatusCode = HttpStatusCode.NotFound, Forecasts = null
                        });
                    }

                    return(new WeatherResponse
                    {
                        StatusCode = HttpStatusCode.OK,
                        Forecasts = apiResponse.Content.Forecasts
                    });
                }
            } catch (Exception ex) when((ex is ApiException) || (ex is WebException))
            {
                // Here we would Log exception
                return(new WeatherResponse {
                    StatusCode = HttpStatusCode.InternalServerError, Forecasts = null
                });
            }
        }
Example #10
0
 private Uri BuildCompleteRequest(IWeatherRequest request)
 {
     var builder = new UriBuilder(_config.BaseUri) {Query = request.Build()};
     return builder.Uri;
 }
 public HomeController(IWeatherRequest weatherRequest)
 {
     _weatherRequest = weatherRequest;
 }