示例#1
0
 // GET api/values/5
 public WeatherDataVIewModel Get([FromUri] WeatherFilter weatherFilter)
 {
     if (ModelState.IsValid)
     {
         return(_weatherService.GetWeatherByCityAndCountry(weatherFilter));
     }
     else
     {
         return(new WeatherDataVIewModel());
     }
 }
示例#2
0
        public void Get()
        {
            // Arrange
            WeatherController controller    = new WeatherController(new WeatherService());
            WeatherFilter     weatherFilter = new WeatherFilter()
            {
                Country = "US", City = "Abbeville"
            };
            // Act
            WeatherDataVIewModel weatherDataVIewModel = controller.Get(weatherFilter);

            // Assert
            Assert.IsNotNull(weatherDataVIewModel);
            Assert.AreEqual(weatherFilter.Country, weatherDataVIewModel.Country);
            Assert.AreEqual(weatherFilter.City, weatherDataVIewModel.City);
        }
示例#3
0
        public WeatherDataVIewModel GetWeatherByCityAndCountry(WeatherFilter weatherOptions)
        {
            Uri         uri         = new Uri("https://api.openweathermap.org/data/2.5/weather?q=" + weatherOptions.City + "," + weatherOptions.Country + "&appid=" + "6b3b6d4ebd445dcfc5f4336c96abe06c");
            WeatherData weatherData = null;

            using (var client = new HttpClient())
            {
                var responseTask = client.GetAsync(uri);
                responseTask.Wait();

                var result = responseTask.Result;

                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <WeatherData>();
                    readTask.Wait();

                    weatherData = readTask.Result;
                }
                else
                {
                    return(null);
                    // ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
                }
            }

            WeatherDataVIewModel weatherDataVIewModel = new WeatherDataVIewModel()
            {
                Country    = weatherData.sys.country,
                City       = weatherData.name,
                Clouds     = weatherData.clouds,
                Humidity   = weatherData.main.humidity,
                Pressure   = weatherData.main.pressure,
                Temprature = weatherData.main.temp,
                Visibility = weatherData.visibility,
                Wind       = weatherData.wind
            };

            return(weatherDataVIewModel);
        }