Skip to content

Commit

Permalink
Add apiKey to the constructor and remove rethrowExceptions from the c…
Browse files Browse the repository at this point in the history
…onstructor (this will be set by a setter method instead)
  • Loading branch information
colinnuk committed Aug 29, 2024
1 parent c1ac2f1 commit 4456015
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
47 changes: 38 additions & 9 deletions OpenMeteo/OpenMeteoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public class OpenMeteoClient
private readonly HttpController httpController;

private readonly IOpenMeteoLogger? _logger = default!;
private readonly bool _rethrowExceptions = false;
private readonly string _apiKey = string.Empty;

/// <summary>
/// If set to true, exceptions from the OpenMeteo API will be rethrown. Default is false.
/// </summary>
/// <param name="rethrowExceptions"></param>
public bool RethrowExceptions { get; set; } = false;

/// <summary>
/// Creates a new <seealso cref="OpenMeteoClient"/> object and sets the neccessary variables (httpController, CultureInfo)
Expand All @@ -29,13 +35,36 @@ public OpenMeteoClient()
}

/// <summary>
/// Creates a new <seealso cref="OpenMeteoClient"/> object and sets the neccessary variables (httpController, CultureInfo)
/// Creates a new <seealso cref="OpenMeteoClient"/> object with a logger
/// </summary>
public OpenMeteoClient(bool rethrowExceptions, IOpenMeteoLogger? logger = null)
/// <param name="logger">An object which implements an interface that can be used for logging from this class</param>
public OpenMeteoClient(IOpenMeteoLogger logger)
{
httpController = new HttpController();
_logger = logger;
_rethrowExceptions = rethrowExceptions;
}

/// <summary>
/// Creates a new <seealso cref="OpenMeteoClient"/> object with a logger and an API key
/// </summary>
/// <param name="apiKey">The API key to use the customer OpenMeteo URLs such as https://customer-api.open-meteo.com</param>
public OpenMeteoClient(string apiKey)
{
httpController = new HttpController();
_apiKey = apiKey;
}

/// <summary>
/// Creates a new <seealso cref="OpenMeteoClient"/> object with a logger and an API key
/// </summary>
/// <param name="logger">An object which implements an interface that can be used for logging from this class</param>
/// <param name="apiKey">The API key to use the customer OpenMeteo URLs such as https://customer-api.open-meteo.com</param>

public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey)
{
httpController = new HttpController();
_logger = logger;
_apiKey = apiKey;
}

/// <summary>
Expand All @@ -45,7 +74,7 @@ public OpenMeteoClient(bool rethrowExceptions, IOpenMeteoLogger? logger = null)
/// <returns>If successful returns an awaitable Task containing WeatherForecast or NULL if request failed</returns>
public async Task<WeatherForecast?> QueryAsync(string location)
{
GeocodingOptions geocodingOptions = new GeocodingOptions(location);
GeocodingOptions geocodingOptions = new(location);

// Get location Information
GeocodingApiResponse? response = await GetGeocodingDataAsync(geocodingOptions);
Expand Down Expand Up @@ -234,7 +263,7 @@ public OpenMeteoClient(bool rethrowExceptions, IOpenMeteoLogger? logger = null)
catch (HttpRequestException e)
{
_logger?.Warning($"{nameof(OpenMeteoClient)}.GetAirQualityAsync(). Message: {e.Message} StackTrace: {e.StackTrace}");
if (_rethrowExceptions)
if (RethrowExceptions)
throw;
return null;
}
Expand Down Expand Up @@ -341,7 +370,7 @@ public string WeathercodeToString(int weathercode)
catch (Exception e)
{
_logger?.Warning($"{nameof(OpenMeteoClient)}.GetWeatherForecastAsync(). Message: {e.Message} StackTrace: {e.StackTrace}");
if (_rethrowExceptions)
if (RethrowExceptions)
throw;
return null;
}
Expand All @@ -365,7 +394,7 @@ public string WeathercodeToString(int weathercode)
catch (HttpRequestException e)
{
_logger?.Warning($"{nameof(OpenMeteoClient)}.GetGeocodingDataAsync(). Message: {e.Message} StackTrace: {e.StackTrace}");
if (_rethrowExceptions)
if (RethrowExceptions)
throw;
return null;
}
Expand All @@ -388,7 +417,7 @@ public string WeathercodeToString(int weathercode)
{
_logger?.Warning($"Can't find elevation for latitude {options.Latitude} & longitude {options.Longitude}. Please make sure that they are valid.");
_logger?.Warning($"Error in {nameof(OpenMeteoClient)}.GetElevationAsync(). Message: {e.Message} StackTrace: {e.StackTrace}");
if (_rethrowExceptions)
if (RethrowExceptions)
throw;
return null;
}
Expand Down
8 changes: 5 additions & 3 deletions OpenMeteoTests/WeatherForecastTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -130,9 +129,12 @@ public void WeatherForecast_With_All_Options_Test()
[TestMethod]
public async Task Latitude_Longitude_No_Data_For_Selected_Forecast_Rethrows_Test()
{
OpenMeteoClient client = new(true);
OpenMeteoClient client = new()
{
RethrowExceptions = true
};

WeatherForecastOptions options = new WeatherForecastOptions
WeatherForecastOptions options = new()
{
Latitude = 1,
Longitude = 1,
Expand Down

0 comments on commit 4456015

Please sign in to comment.