Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Client endpoint from env var #181

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Fauna.Test/Configuration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ public void ConstructorWorksFine()
Assert.IsTrue(b.DisposeHttpClient);
}

[Test]
public void ConstructorWithEndpointEnvVar()
{
string? currentVal = Environment.GetEnvironmentVariable("FAUNA_ENDPOINT");
Environment.SetEnvironmentVariable("FAUNA_ENDPOINT", "http://localhost:8443/");
adambollen marked this conversation as resolved.
Show resolved Hide resolved

Configuration config = new Configuration("secret");

Assert.AreEqual("http://localhost:8443/", config.Endpoint.ToString());

Environment.SetEnvironmentVariable("FAUNA_ENDPOINT", currentVal);
}

[Test]
public void ConstructorThrowsWithBadEndpointEnvVar()
{
string? currentVal = Environment.GetEnvironmentVariable("FAUNA_ENDPOINT");
Assert.Throws<UriFormatException>((() =>
{
Environment.SetEnvironmentVariable("FAUNA_ENDPOINT", "bad.endpoint");

Configuration unused = new Configuration();
}));

Environment.SetEnvironmentVariable("FAUNA_ENDPOINT", currentVal);
}

[Test]
public void ConstructorUsesEnvVar()
{
Expand Down
2 changes: 1 addition & 1 deletion Fauna/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public record class Configuration
/// <summary>
/// The endpoint URL of the Fauna server.
/// </summary>
public Uri Endpoint { get; init; } = Endpoints.Default;
public Uri Endpoint { get; init; } = Endpoints.GetFaunaEndpoint();

/// <summary>
/// Default options for queries sent to Fauna.
Expand Down
20 changes: 20 additions & 0 deletions Fauna/Core/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,24 @@ public static class Endpoints
/// The default URI for Fauna, used for production.
/// </summary>
public static Uri Default { get; } = new("https://db.fauna.com");

/// <summary>
/// Gets the configured endpoint URI, falling back to the default if not set.
/// </summary>
/// <returns>The URI for the Fauna endpoint.</returns>
public static Uri GetFaunaEndpoint()
{
string? endpoint = Environment.GetEnvironmentVariable("FAUNA_ENDPOINT");
if (string.IsNullOrWhiteSpace(endpoint))
{
return Default;
}

if (Uri.IsWellFormedUriString(endpoint, UriKind.Absolute))
{
return new Uri(endpoint);
}

throw new UriFormatException("Invalid FAUNA_ENDPOINT environment variable. Must be a valid URI.");
}
}
Loading