Пример #1
0
        static async Task Main(string[] args)
        {
            try
            {
                ConfigHelper.InitializeConfig();
                var weatherService = new OpenWeatherService();

                var historyService = new HistoryLocationService();
                var history        = historyService.GetSavedLocations();

                Console.WriteLine("Please enter the name of a city and iso country code separated by a comma, example : Perth, AU");
                DisplayHistory(history);
                var    input = Console.ReadLine().Trim();
                string city;
                string country;
                ValidateInput(input, history, out city, out country);
                var currentWeather = await weatherService.GetCurrentWeather(city.Trim(' '), country?.Trim(' ').ToUpper());

                Console.WriteLine(currentWeather);
                while (currentWeather.Contains("We couldn't find"))
                {
                    Console.WriteLine("Please enter another city name or a number from the list.");
                    input = Console.ReadLine();
                    ValidateInput(input, history, out city, out country);
                    currentWeather = await weatherService.GetCurrentWeather(city.Trim(' '), country?.Trim(' ').ToUpper());
                }
                Console.WriteLine(currentWeather);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
Пример #2
0
        public async Task GetCurrentWeatherByCityName_Test()
        {
            // prepare SI
            WeatherConditions output = null;

            using (var stream = new BufferedStream(File.OpenRead("./Resources/OpenW_currentweather_SI.json"), 8192)) {
                var mockHttp = new MockHttpMessageHandler();
                mockHttp
                .When(OpenWeatherService.EndPointRoot + "*")
                .Respond("application/json", stream);
                IOpenWeatherService client = new OpenWeatherService("12345", mockHttp);

                output = await client.GetCurrentWeather(BolognaLatitude, BolognaLongitude, OWUnit.Metric, Language.Italian);

                stream.Close();
            }
            // assert
            output.ShouldNotBeNull();
            output.City.ShouldNotBeNull();
            output.City.Coordinates.Latitude.ShouldBe(44.4667);
            output.City.Coordinates.Longitude.ShouldBe(11.4333);
            output.WeatherInfo[0].Summary.ShouldBe("Clouds");
            output.WeatherInfo[0].Description.ShouldBe("nubi sparse");
            output.WeatherInfo[0].Icon.ShouldBe("03d");
            output.City.Name.ShouldNotBeNullOrWhiteSpace();
            output.City.Name.ShouldBe(BolognaCityName, StringCompareShould.IgnoreCase);
            output.City.CountryCode.ShouldBe("IT");
            output.City.SunriseTime.ToUnixTimeSeconds().ShouldBe(1610174975);

            // prepare imperial
            WeatherConditions outputImperial = null;

            using (var stream = new BufferedStream(File.OpenRead("./Resources/OpenW_currentweather_Imperial.json"), 8192)) {
                var mockHttp = new MockHttpMessageHandler();
                mockHttp
                .When(OpenWeatherService.EndPointRoot + "*")
                .Respond("application/json", stream);
                IOpenWeatherService client = new OpenWeatherService("12345", mockHttp);

                outputImperial = await client.GetCurrentWeather(BolognaCityName, OWUnit.Imperial, Language.English);

                stream.Close();
            }
            // assert
            output.ShouldNotBeNull();
            output.City.ShouldNotBeNull();
            outputImperial.City.Name.ShouldBe(output.City.Name);
            outputImperial.City.CountryCode.ShouldBe(output.City.CountryCode);
            outputImperial.City.Coordinates.Latitude.ShouldBe(output.City.Coordinates.Latitude);
            outputImperial.City.Coordinates.Longitude.ShouldBe(output.City.Coordinates.Longitude);
            outputImperial.Wind.Speed.ShouldNotBe(output.Wind.Speed);
            outputImperial.Temperature.Daily.ShouldNotBeNull();
            outputImperial.Temperature.Daily.Value.ShouldBeGreaterThan(output.Temperature.Daily.Value);
            outputImperial.ApparentTemperature.Daily.ShouldNotBeNull();
            outputImperial.ApparentTemperature.Daily.Value.ShouldBeGreaterThan(output.ApparentTemperature.Daily.Value);
        }
Пример #3
0
        public void InvalidKeyThrowsException()
        {
            // prepare
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .When(OpenWeatherService.EndPointRoot + "*")
            .Respond(HttpStatusCode.Unauthorized);
            // assert
            IOpenWeatherService client = new OpenWeatherService(null, mockHttp);

            Assert.ThrowsAsync <WeatherException>(async() => await client.GetCurrentWeather(AlcatrazLatitude, AlcatrazLongitude));
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecast(AlcatrazLatitude, AlcatrazLongitude));

            client = new OpenWeatherService("", mockHttp);
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecastDSL(AlcatrazLatitude, AlcatrazLongitude, OWUnit.Imperial));
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecast(AlcatrazLatitude, AlcatrazLongitude));

            client = new OpenWeatherService("fake_key", mockHttp);
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetCurrentWeather(BolognaCityName, OWUnit.Standard, Language.Catalan));
        }
Пример #4
0
        public async void GetCurrentWeatherByCoordinates_Test()
        {
            // prepare
            WeatherConditions output = null;

            using (var stream = new BufferedStream(File.OpenRead("./Resources/OpenW_currentweather_SI.json"), 8192)) {
                var mockHttp = new MockHttpMessageHandler();
                mockHttp
                .When(OpenWeatherService.EndPointRoot + "*")
                .Respond("application/json", stream);
                IOpenWeatherService client = new OpenWeatherService("a_valid_key", mockHttp);

                output = await client.GetCurrentWeather(BolognaLatitude, BolognaLongitude, OWUnit.Metric, Language.Italian);

                stream.Close();
            }

            // assert
            output.ShouldNotBeNull();
            output.City.Name.ShouldNotBeNullOrWhiteSpace();
            output.City.Name.ShouldBe(BolognaCityName, StringCompareShould.IgnoreCase);
        }