diff --git a/Fauna.Test/Configuration.Tests.cs b/Fauna.Test/Configuration.Tests.cs index 7e4e1427..c0bc69fc 100644 --- a/Fauna.Test/Configuration.Tests.cs +++ b/Fauna.Test/Configuration.Tests.cs @@ -16,6 +16,30 @@ public void ConstructorWorksFine() Assert.IsTrue(b.DisposeHttpClient); } + [Test] + public void ConstructorUsesEnvVar() + { + Environment.SetEnvironmentVariable("FAUNA_SECRET", "secret"); + var b = new Configuration(); + + Assert.AreEqual("secret", b.Secret); + Assert.AreEqual(Endpoints.Default, b.Endpoint); + Assert.IsTrue(b.DisposeHttpClient); + } + + [Test] + public void ConstructorThrowsWithNullSecret() + { + string? currentVal = Environment.GetEnvironmentVariable("FAUNA_SECRET"); + Assert.Throws(() => + { + Environment.SetEnvironmentVariable("FAUNA_SECRET", null); + var b = new Configuration(); + + }); + Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal); + } + [Test] public void ConstructorWithHttpClient() { diff --git a/Fauna/Client.cs b/Fauna/Client.cs index 4de7a476..e7d48815 100644 --- a/Fauna/Client.cs +++ b/Fauna/Client.cs @@ -37,6 +37,12 @@ public class Client : BaseClient, IDisposable /// public long LastSeenTxn { get; private set; } + /// + /// Initializes a new instance of a Client with the default configuration. + /// Assumes the environment variable FAUNA_SECRET is set. + /// + public Client() : this(new Configuration()) { } + /// /// Initializes a new instance of a Client with a secret. /// diff --git a/Fauna/Configuration.cs b/Fauna/Configuration.cs index c4c3fe8c..36854cfe 100644 --- a/Fauna/Configuration.cs +++ b/Fauna/Configuration.cs @@ -11,17 +11,17 @@ public record class Configuration /// /// Whether the should dispose of the on Dispose. /// - public bool DisposeHttpClient { get; } + public bool DisposeHttpClient { get; } = true; /// /// The HTTP Client to use for requests. /// - public HttpClient HttpClient { get; } + public HttpClient HttpClient { get; } = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }; /// /// The secret key used for authentication. /// - public string Secret { get; init; } + public string Secret { get; init; } = Environment.GetEnvironmentVariable("FAUNA_SECRET") ?? string.Empty; /// /// The endpoint URL of the Fauna server. @@ -44,6 +44,14 @@ public record class Configuration /// public IStatsCollector? StatsCollector { get; init; } = new StatsCollector(); + public Configuration() + { + if (string.IsNullOrEmpty(Secret)) + { + throw new ArgumentNullException(nameof(Secret), "Need to set FAUNA_SECRET environment variable or pass a secret as a parameter when creating the Client."); + } + } + /// /// Initializes a new instance of the record with the specified secret key. /// @@ -51,19 +59,13 @@ public record class Configuration /// The to use. public Configuration(string secret, HttpClient? httpClient = null) { + Secret = secret; if (httpClient is null) { - HttpClient = new HttpClient - { - Timeout = TimeSpan.FromSeconds(5) - }; - DisposeHttpClient = true; - } - else - { - HttpClient = httpClient; + return; } - Secret = secret; + HttpClient = httpClient; + DisposeHttpClient = false; } }