From 27201d375025a824bbaf9de4b0890ef7f68d71c0 Mon Sep 17 00:00:00 2001 From: Miguel Prieto Date: Wed, 4 Dec 2024 20:35:48 -0300 Subject: [PATCH] - Allow a custom configuration in ApiExtensions - Lazily initialize from Environment variables if no custom config - Improve unset env variable error message --- Conductor/Client/Extensions/ApiExtensions.cs | 28 +++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Conductor/Client/Extensions/ApiExtensions.cs b/Conductor/Client/Extensions/ApiExtensions.cs index 7a2846f..c0878a5 100644 --- a/Conductor/Client/Extensions/ApiExtensions.cs +++ b/Conductor/Client/Extensions/ApiExtensions.cs @@ -14,7 +14,6 @@ using Conductor.Executor; using Conductor.Client.Authentication; using System; -using System.Diagnostics; namespace Conductor.Client.Extensions { @@ -25,20 +24,25 @@ public class ApiExtensions private const string ENV_SECRET = "SECRET"; private const int REST_CLIENT_REQUEST_TIME_OUT = 30 * 1000; - public static Configuration Configuration { get; set; } - - static ApiExtensions() - { - Configuration = new Configuration(REST_CLIENT_REQUEST_TIME_OUT) + private static readonly Lazy _lazyConfiguration = + new Lazy(() => new Configuration(REST_CLIENT_REQUEST_TIME_OUT) { BasePath = GetEnvironmentVariable(ENV_ROOT_URI), AuthenticationSettings = new OrkesAuthenticationSettings( - GetEnvironmentVariable(ENV_KEY_ID), - GetEnvironmentVariable(ENV_SECRET) - ) - }; + GetEnvironmentVariable(ENV_KEY_ID), + GetEnvironmentVariable(ENV_SECRET) + ) + }); + + private static Configuration _customConfiguration; + + public static Configuration Configuration + { + get => _customConfiguration ?? _lazyConfiguration.Value; + set => _customConfiguration = value; } + public static WorkflowExecutor GetWorkflowExecutor() { return new WorkflowExecutor( @@ -66,9 +70,7 @@ public static string GetWorkflowExecutionURL(string workflowId) private static string GetEnvironmentVariable(string variable) { - string value = Environment.GetEnvironmentVariable(variable); - Debug.Assert(value != null); - return value; + return Environment.GetEnvironmentVariable(variable) ?? throw new InvalidOperationException($"Environment variable '{variable}' is not set."); } } }