Skip to content

Commit

Permalink
Remove APIKEY from logs
Browse files Browse the repository at this point in the history
  • Loading branch information
colinnuk committed Aug 30, 2024
1 parent db1eb80 commit 1ac61f7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
12 changes: 6 additions & 6 deletions OpenMeteo/OpenMeteoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey)
/// <returns>Awaitable Task containing WeatherForecast or NULL</returns>
public async Task<WeatherForecast?> QueryAsync(float latitude, float longitude)
{
WeatherForecastOptions options = new WeatherForecastOptions
WeatherForecastOptions options = new()
{
Latitude = latitude,
Longitude = longitude,
Expand Down Expand Up @@ -171,7 +171,7 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey)
/// <returns></returns>
public async Task<GeocodingApiResponse?> GetLocationDataAsync(string location)
{
GeocodingOptions geocodingOptions = new GeocodingOptions(location);
GeocodingOptions geocodingOptions = new(location);

return await GetLocationDataAsync(geocodingOptions);
}
Expand Down Expand Up @@ -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();

Expand All @@ -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)
{
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions OpenMeteo/UrlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
24 changes: 24 additions & 0 deletions OpenMeteoTests/UrlFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1ac61f7

Please sign in to comment.