Exemplo n.º 1
0
        public async Task OnSuccessShouldReturnValidObject()
        {
            // Arrange
            //
            var faceSettings = TestHelper.GetFaceSettings();
            var lat          = (decimal)38.855652;
            var lon          = (decimal) - 94.799712;

            var handler             = new Mock <HttpMessageHandler>();
            var openWeatherResponse =
                "{\"coord\":{\"lon\":-94.8,\"lat\":38.88},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"}],\"base\":\"stations\",\"main\":{\"temp\":4.28,\"feels_like\":0.13,\"temp_min\":3,\"temp_max\":5.56,\"pressure\":1034,\"humidity\":51},\"visibility\":16093,\"wind\":{\"speed\":2.21,\"deg\":169},\"clouds\":{\"all\":1},\"dt\":1584811457,\"sys\":{\"type\":1,\"id\":5188,\"country\":\"US\",\"sunrise\":1584793213,\"sunset\":1584837126},\"timezone\":-18000,\"id\":4276614,\"name\":\"Olathe\",\"cod\":200}";

            handler.SetupRequest(HttpMethod.Get, faceSettings.BuildOpenWeatherUrl(lat, lon))
            .ReturnsResponse(openWeatherResponse, "application/json");

            var client = new OpenWeatherClient(
                TestHelper.GetLoggerMock <OpenWeatherClient>().Object,
                handler.CreateClient(),
                faceSettings,
                TestHelper.GetMetricsMock().Object,
                MapperConfig.CreateMapper());

            // Act
            //
            var result = await client.RequestOpenWeather(lat, lon);

            // Assert
            //
            Assert.Equal(RequestStatusCode.Ok, result.RequestStatus.StatusCode);
            Assert.Equal("clear-day", result.Icon);
            Assert.Equal((decimal)0.51, result.Humidity);
            Assert.Equal((decimal)4.28, result.Temperature);
        }
Exemplo n.º 2
0
        public WeatherModel GetWeather()
        {
            //var client = new MetaWeatherClient();
            var client = new OpenWeatherClient();

            return(client.GetWeather());
        }
Exemplo n.º 3
0
        public async Task OnErrorShouldReturnErrorObject()
        {
            // Arrange
            //
            var faceSettings = TestHelper.GetFaceSettings();
            var lat          = (decimal)38.855652;
            var lon          = (decimal) - 94.799712;

            var handler = new Mock <HttpMessageHandler>();

            handler.SetupAnyRequest()
            .ReturnsResponse(HttpStatusCode.BadRequest);

            var client = new OpenWeatherClient(
                TestHelper.GetLoggerMock <OpenWeatherClient>().Object,
                handler.CreateClient(),
                faceSettings,
                TestHelper.GetMetricsMock().Object,
                MapperConfig.CreateMapper());

            // Act
            //
            var result = await client.RequestOpenWeather(lat, lon);

            // Assert
            //
            Assert.Equal(RequestStatusCode.Error, result.RequestStatus.StatusCode);
            Assert.Equal(400, result.RequestStatus.ErrorCode);
        }
Exemplo n.º 4
0
 public HomeController(OpenWeatherClient openWeatherClient, ILogger <HomeController> logger, SeatGeekClient seatGeekClient, UserManager <IdentityUser> userManager, PlanFortDBContext planFortDBContext)
 {
     _logger            = logger;
     _seatGeekClient    = seatGeekClient;
     _userManager       = userManager;
     _planFortDBContext = planFortDBContext;
     _openWeatherClient = openWeatherClient;
 }
        public OpenWeatherServiceTestsVersion2()
        {
            _server = WireMockServer.Start();

            var client = new OpenWeatherClient(_server.Urls.First(), "any string");

            _sut = new OpenWeatherService(client);
        }
Exemplo n.º 6
0
        static async Task Main(string[] args)
        {
            var client = new OpenWeatherClient("https://api.openweathermap.org/data/2.5", "3141d5a3756312d1295eefeadbccc24d");

            var service = new OpenWeatherService(client);

            var result = await service.GetInfoAsync("Sittard, NL");

            Console.WriteLine($"{result.DegreesCelsius:##.##} celsius ({result.Description})");
        }
Exemplo n.º 7
0
        static async Task Main(string[] args)
        {
            var client = new OpenWeatherClient("https://wiremock.azurewebsites.net", "xxx");

            var service = new OpenWeatherService(client);

            var result = await service.GetInfoAsync("Sittard, NL");

            Console.WriteLine($"{result.DegreesCelsius:##.##} celsius ({result.Description})");
        }
        public async Task ByCityId()
        {
            using (IOpenWeatherClient client = new OpenWeatherClient())
            {
                var weatherForecast = await client.GetWeatherForecast5d3hAsync(
                    cityId : Const.SampleCityId,
                    apiKey : Const.OpenWeatherApiKey);

                Assert.NotNull(weatherForecast);
            }
        }
        public async Task ByCityName()
        {
            using (IOpenWeatherClient client = new OpenWeatherClient())
            {
                WeatherForecast weatherForecast = await client.GetWeatherForecast5d3hAsync(
                    cityNameCountryCode : Const.SampleCityNameCountryCode,
                    apiKey : Const.OpenWeatherApiKey);

                Assert.NotNull(weatherForecast);
            }
        }
        public async Task ByLonLat_UseConstructorAppId()
        {
            using (IOpenWeatherClient client = new OpenWeatherClient(apiKey: Const.OpenWeatherApiKey))
            {
                WeatherForecast weatherForecast = await client.GetWeatherForecast5d3hAsync(
                    longitude : Const.SampleLongitude,
                    latitude : Const.SampleLatitude);

                Assert.NotNull(weatherForecast);
            }
        }
        public async Task ByCityId()
        {
            using (IOpenWeatherClient client = new OpenWeatherClient())
            {
                var currentWeather = await client.GetCurrentWeatherAsync(
                    cityId : Const.SampleCityId,
                    apiKey : Const.OpenWeatherApiKey);

                Assert.NotNull(currentWeather);
            }
        }
        public async Task ByCityName()
        {
            using (IOpenWeatherClient client = new OpenWeatherClient())
            {
                CurrentWeather currentWeather = await client.GetCurrentWeatherAsync(
                    cityNameCountryCode : Const.SampleCityNameCountryCode,
                    apiKey : Const.OpenWeatherApiKey);

                Assert.NotNull(currentWeather);
            }
        }
        public async Task ByLonLat_UseConstructorAppId()
        {
            using (IOpenWeatherClient client = new OpenWeatherClient(apiKey: Const.OpenWeatherApiKey))
            {
                CurrentWeather currentWeather = await client.GetCurrentWeatherAsync(
                    longitude : Const.SampleLongitude,
                    latitude : Const.SampleLatitude);

                Assert.NotNull(currentWeather);
            }
        }
 public async Task Err_MissingLon()
 {
     await Assert.ThrowsAsync <ArgumentException>(async() =>
     {
         using (IOpenWeatherClient client = new OpenWeatherClient())
         {
             var weatherForecast = await client.GetWeatherForecast5d3hAsync(
                 latitude: Const.SampleLatitude,
                 apiKey: Const.OpenWeatherApiKey);
         }
     });
 }
Exemplo n.º 15
0
        public OpenWeatherClientTests()
        {
            _config = new MockConfig
            {
                BaseAddress    = "http://api.openweathermap.org/data/2.5/weather",
                ApiKey         = "62ffa8c4c1dfbc438b162198e174c4cb",
                State          = "wa",
                IsoCountryCode = "au"
            };

            _sut = new OpenWeatherClient(new HttpClient(), _config, new MetricUrlBuilder(_config));
        }
 public async Task Err_MissingLat()
 {
     await Assert.ThrowsAsync <ArgumentException>(async() =>
     {
         using (IOpenWeatherClient client = new OpenWeatherClient())
         {
             var currentWeather = await client.GetCurrentWeatherAsync(
                 longitude: Const.SampleLongitude,
                 apiKey: Const.OpenWeatherApiKey);
         }
     });
 }
 public async Task Err_CityNameCityId()
 {
     await Assert.ThrowsAsync <ArgumentException>(async() =>
     {
         using (IOpenWeatherClient client = new OpenWeatherClient())
         {
             var currentWeather = await client.GetCurrentWeatherAsync(
                 cityNameCountryCode: Const.SampleCityNameCountryCode,
                 cityId: Const.SampleCityId,
                 apiKey: Const.OpenWeatherApiKey);
         }
     });
 }
 public async Task Err_LonLatCityName()
 {
     await Assert.ThrowsAsync <ArgumentException>(async() =>
     {
         using (IOpenWeatherClient client = new OpenWeatherClient())
         {
             var weatherForecast = await client.GetWeatherForecast5d3hAsync(
                 longitude: Const.SampleLongitude,
                 latitude: Const.SampleLatitude,
                 cityNameCountryCode: Const.SampleCityNameCountryCode,
                 apiKey: Const.OpenWeatherApiKey);
         }
     });
 }
        public async Task ByLonLat_UseIConfiguration()
        {
            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("OpenWeather:ApiKey", Const.OpenWeatherApiKey) })
                         .Build();

            using (IOpenWeatherClient client = new OpenWeatherClient(config))
            {
                CurrentWeather currentWeather = await client.GetCurrentWeatherAsync(
                    longitude : Const.SampleLongitude,
                    latitude : Const.SampleLatitude);

                Assert.NotNull(currentWeather);
            }
        }
        public async Task Err_ConstructorAppIdOverridesIConfiguration()
        {
            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("OpenWeather:ApiKey", Const.OpenWeatherApiKey) })
                         .Build();

            await Assert.ThrowsAsync <System.Net.WebException>(async() =>
            {
                using (IOpenWeatherClient client = new OpenWeatherClient(config, apiKey: "this-key-fails-for-sure"))
                {
                    var currentWeather = await client.GetCurrentWeatherAsync(
                        longitude: Const.SampleLongitude,
                        latitude: Const.SampleLatitude);
                }
            });
        }
Exemplo n.º 21
0
 public YAFaceController(
     ILogger <YAFaceController> logger, PostgresDataProvider postgresDataProvider, KafkaProvider kafkaProvider,
     ExchangeRateCacheStrategy exchangeRateCacheStrategy,
     VirtualearthClient virtualearthClient,
     DarkSkyClient darkSkyClient,
     OpenWeatherClient openWeatherClient,
     FaceSettings faceSettings)
 {
     _logger = logger;
     _postgresDataProvider      = postgresDataProvider;
     _kafkaProvider             = kafkaProvider;
     _exchangeRateCacheStrategy = exchangeRateCacheStrategy;
     _virtualearthClient        = virtualearthClient;
     _darkSkyClient             = darkSkyClient;
     _openWeatherClient         = openWeatherClient;
     _faceSettings = faceSettings;
 }
Exemplo n.º 22
0
        public async Task OnAuthErrorShouldLogAuthIssue()
        {
            // Arrange
            //
            var faceSettings = TestHelper.GetFaceSettings();
            var lat          = (decimal)38.855652;
            var lon          = (decimal) - 94.799712;

            var loggerMock = TestHelper.GetLoggerMock <OpenWeatherClient>();

            var handler = new Mock <HttpMessageHandler>();

            handler.SetupAnyRequest()
            .ReturnsResponse(HttpStatusCode.Unauthorized);

            var client = new OpenWeatherClient(
                loggerMock.Object,
                handler.CreateClient(),
                faceSettings,
                TestHelper.GetMetricsMock().Object,
                MapperConfig.CreateMapper());

            // Act
            //
            var result = await client.RequestOpenWeather(lat, lon);

            // Assert
            //
            Assert.Equal(RequestStatusCode.Error, result.RequestStatus.StatusCode);
            Assert.Equal(401, result.RequestStatus.ErrorCode);

            loggerMock.Verify(
                x => x.Log(
                    It.IsAny <LogLevel>(),
                    It.IsAny <EventId>(),
                    It.Is <It.IsAnyType>((o, t) => string.Equals("Unauthorized access to OpenWeather", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
                    It.IsAny <Exception>(),
                    (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()),
                Times.Once);
        }
Exemplo n.º 23
0
        private async Task <IEnumerable <WeatherToApiDto> > GetWeathersFromThirdPartiesApi(string cityNameCountryCode)
        {
            using (IOpenWeatherClient openWeatherClient = new OpenWeatherClient(apiKey: configuration.GetSection("ApiWeatherThirdPartieKey").Value))
            {
                var currentWeather = await openWeatherClient.GetWeatherForecast5d3hAsync(cityNameCountryCode : cityNameCountryCode);

                var listOfWeatherToApiDto = new List <WeatherToApiDto>();

                foreach (var item in currentWeather.List)
                {
                    listOfWeatherToApiDto.Add(new WeatherToApiDto
                    {
                        CityName    = currentWeather.City.Name,
                        CountryName = currentWeather.City.Country,
                        MaxTemp     = item.Main.TempMaxC,
                        MinTemp     = item.Main.TempMinC,
                        Date        = item.DateTimeUtc
                    });
                }
                return(listOfWeatherToApiDto);
            }
        }
        public async Task StartAsync(IDialogContext context)
        {
            var weatherLocation = context.UserData.GetValueOrDefault <WeatherLocation>(WeatherBot.Storage.Location.UserDataLocationKey);

            if (weatherLocation?.GeoLocation != null)
            {
                _latitude  = weatherLocation.GeoLocation.Latitude;
                _longitude = weatherLocation.GeoLocation.Longitude;
            }

            var weatherClient       = new OpenWeatherClient("metric");
            var openWeatherResponse = await weatherClient.GetWeatherAsync(_latitude, _longitude);

            if (weatherLocation != null)
            {
                weatherLocation.Name = openWeatherResponse.Name;
                context.UserData.SetValue(WeatherBot.Storage.Location.UserDataLocationKey, weatherLocation);
            }

            var weatherString = $"The temperature in {openWeatherResponse.Name} is {openWeatherResponse.Main.Temp}°C with lows of {openWeatherResponse.Main.TempMin}°C & highs of {openWeatherResponse.Main.TempMax}°C";

            context.Done(weatherString);
        }
Exemplo n.º 25
0
        public OpenWeatherServiceTestsVersion1()
        {
            var client = new OpenWeatherClient("https://api.openweathermap.org/data/2.5/", "3141d5a3756312d1295eefeadbccc24d");

            _sut = new OpenWeatherService(client);
        }
 public WeatherModule(Settings settings, OpenWeatherClient openweatherClient)
 {
     _settings = settings;
     _client   = openweatherClient;
 }