diff --git a/OpenMeteo/OpenMeteoClient.cs b/OpenMeteo/OpenMeteoClient.cs index 835f621..7471d3c 100644 --- a/OpenMeteo/OpenMeteoClient.cs +++ b/OpenMeteo/OpenMeteoClient.cs @@ -127,7 +127,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey) /// Awaitable Task containing WeatherForecast or NULL public async Task QueryAsync(float latitude, float longitude) { - WeatherForecastOptions options = new WeatherForecastOptions + WeatherForecastOptions options = new() { Latitude = latitude, Longitude = longitude, @@ -171,7 +171,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey) /// public async Task GetLocationDataAsync(string location) { - GeocodingOptions geocodingOptions = new GeocodingOptions(location); + GeocodingOptions geocodingOptions = new(location); return await GetLocationDataAsync(geocodingOptions); } @@ -247,7 +247,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey) try { var url = _urlFactory.GetUrlWithOptions(options); - _logger?.Debug($"{nameof(OpenMeteoClient)}.GetAirQualityAsync(). URL: {url}"); + _logger?.Debug($"{nameof(OpenMeteoClient)}.GetAirQualityAsync(). URL: {_urlFactory.SanitiseUrl(url)}"); HttpResponseMessage response = await httpController.Client.GetAsync(url); response.EnsureSuccessStatusCode(); @@ -267,7 +267,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey) try { var url = _urlFactory.GetUrlWithOptions(options); - _logger?.Debug($"{nameof(OpenMeteoClient)}.GetElevationAsync(). URL: {url}"); + _logger?.Debug($"{nameof(OpenMeteoClient)}.GetElevationAsync(). URL: {_urlFactory.SanitiseUrl(url)}"); HttpResponseMessage response = await httpController.Client.GetAsync(url); if(response.IsSuccessStatusCode) { @@ -306,7 +306,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey) { var url = _urlFactory.GetUrlWithOptions(options); - _logger?.Debug($"{nameof(OpenMeteoClient)}.GetGeocodingDataAsync(). URL: {url}"); + _logger?.Debug($"{nameof(OpenMeteoClient)}.GetGeocodingDataAsync(). URL: {_urlFactory.SanitiseUrl(url)}"); HttpResponseMessage response = await httpController.Client.GetAsync(url); response.EnsureSuccessStatusCode(); @@ -328,7 +328,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey) try { var url = _urlFactory.GetUrlWithOptions(options); - _logger?.Debug($"{nameof(OpenMeteoClient)}.GetElevationAsync(). URL: {url}"); + _logger?.Debug($"{nameof(OpenMeteoClient)}.GetElevationAsync(). URL: {_urlFactory.SanitiseUrl(url)}"); HttpResponseMessage response = await httpController.Client.GetAsync(url); response.EnsureSuccessStatusCode(); diff --git a/OpenMeteo/UrlFactory.cs b/OpenMeteo/UrlFactory.cs index a5cd343..8dc5b28 100644 --- a/OpenMeteo/UrlFactory.cs +++ b/OpenMeteo/UrlFactory.cs @@ -21,6 +21,11 @@ public UrlFactory(string apiKey) _apiKey = apiKey; } + public string SanitiseUrl(string url) + { + return string.IsNullOrEmpty(_apiKey) ? url : url.Replace(_apiKey, "APIKEY"); + } + public string GetUrlWithOptions(WeatherForecastOptions options) { UriBuilder uri = new(GetBaseUrl(_weatherApiUrl)); diff --git a/OpenMeteoTests/UrlFactoryTests.cs b/OpenMeteoTests/UrlFactoryTests.cs index 9fef65f..5730e4d 100644 --- a/OpenMeteoTests/UrlFactoryTests.cs +++ b/OpenMeteoTests/UrlFactoryTests.cs @@ -86,6 +86,30 @@ public void GetUrlWithOptions_ElevationOptions_WithApiKey_Test() Assert.AreEqual(expectedUrl, url); } + [TestMethod] + public void SanitiseUrl_WithApiKey_Test() + { + var factory = new UrlFactory("testApiKey"); + var url = "https://api.open-meteo.com/v1/forecast?apikey=testApiKey"; + var sanitisedUrl = factory.SanitiseUrl(url); + + var expectedUrl = "https://api.open-meteo.com/v1/forecast?apikey=APIKEY"; + Assert.AreEqual(expectedUrl, sanitisedUrl); + } + + [TestMethod] + public void SanitiseUrl_WithNoApiKey_Test() + { + var factory = new UrlFactory(); + var url = "https://api.open-meteo.com/v1/forecast"; + var sanitisedUrl = factory.SanitiseUrl(url); + + var expectedUrl = "https://api.open-meteo.com/v1/forecast"; + Assert.AreEqual(expectedUrl, sanitisedUrl); + } + + + private static WeatherForecastOptions GetWeatherForecastOptions() => new() { Latitude = 40.7128f,