// GET api/values/5 public WeatherDataVIewModel Get([FromUri] WeatherFilter weatherFilter) { if (ModelState.IsValid) { return(_weatherService.GetWeatherByCityAndCountry(weatherFilter)); } else { return(new WeatherDataVIewModel()); } }
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); }
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); }