From 18d6c80c2124f8578d1b7605ed75ec6142b1d5dd Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 16 Oct 2024 21:03:10 -0500 Subject: [PATCH] Updated Workflow builder to use DaprDefaults with IConfiguration Signed-off-by: Whit Waldo --- .../DaprWorkflowClientBuilderFactory.cs | 103 +----------------- 1 file changed, 6 insertions(+), 97 deletions(-) diff --git a/src/Dapr.Workflow/DaprWorkflowClientBuilderFactory.cs b/src/Dapr.Workflow/DaprWorkflowClientBuilderFactory.cs index 5c7215632..7a854cf05 100644 --- a/src/Dapr.Workflow/DaprWorkflowClientBuilderFactory.cs +++ b/src/Dapr.Workflow/DaprWorkflowClientBuilderFactory.cs @@ -50,14 +50,14 @@ public void CreateClientBuilder(Action configure) { _services.AddDurableTaskClient(builder => { - var apiToken = GetApiToken(_configuration); - var grpcEndpoint = GetGrpcEndpoint(_configuration); - + var apiToken = DaprDefaults.GetDefaultDaprApiToken(_configuration); + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(_configuration); + var httpClient = _httpClientFactory.CreateClient(); if (!string.IsNullOrWhiteSpace(apiToken)) { - httpClient.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken); + httpClient.DefaultRequestHeaders.Add( "Dapr-Api-Token", apiToken); } builder.UseGrpc(GrpcChannel.ForAddress(grpcEndpoint, new GrpcChannelOptions { HttpClient = httpClient })); @@ -69,8 +69,8 @@ public void CreateClientBuilder(Action configure) WorkflowRuntimeOptions options = new(); configure?.Invoke(options); - var apiToken = GetApiToken(_configuration); - var grpcEndpoint = GetGrpcEndpoint(_configuration); + var apiToken = DaprDefaults.GetDefaultDaprApiToken(_configuration); + var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(_configuration); if (!string.IsNullOrEmpty(grpcEndpoint)) { @@ -92,95 +92,4 @@ public void CreateClientBuilder(Action configure) builder.AddTasks(registry => options.AddWorkflowsAndActivitiesToRegistry(registry)); }); } - - /// - /// Builds the Dapr gRPC endpoint using the value from the IConfiguration, if available, then falling back - /// to the value in the environment variable(s) and finally otherwise using the default value (an empty string). - /// - /// - /// Marked as internal for testing purposes. - /// - /// An injected instance of the . - /// The built gRPC endpoint. - private string GetGrpcEndpoint(IConfiguration? configuration) - { - //Prioritize pulling from IConfiguration with a fallback from pulling from the environment variable directly - var grpcEndpoint = GetResourceValue(configuration, DaprDefaults.DaprGrpcEndpointName); - var grpcPort = GetResourceValue(configuration, DaprDefaults.DaprGrpcPortName); - int? parsedGrpcPort = string.IsNullOrWhiteSpace(grpcPort) ? null : int.Parse(grpcPort); - - var endpoint = BuildEndpoint(grpcEndpoint, parsedGrpcPort); - return string.IsNullOrWhiteSpace(endpoint) ? $"http://localhost:{DaprDefaults.DefaultGrpcPort}/" : endpoint; - } - - /// - /// Retrieves the Dapr API token first from the , if available, then falling back - /// to the value in the environment variable(s) directly, then finally otherwise using the default value (an - /// empty string). - /// - /// - /// Marked as internal for testing purposes. - /// - /// An injected instance of the . - /// The Dapr API token. - private string GetApiToken(IConfiguration? configuration) - { - //Prioritize pulling from IConfiguration with a fallback of pulling from the environment variable directly - return GetResourceValue(configuration, DaprDefaults.DaprApiTokenName); - } - - /// - /// Retrieves the specified value prioritizing pulling it from , falling back - /// to an environment variable, and using an empty string as a default. - /// - /// An instance of an . - /// The name of the value to retrieve. - /// The value of the resource. - private static string GetResourceValue(IConfiguration? configuration, string name) - { - //Attempt to retrieve first from the configuration - var configurationValue = configuration?.GetValue(name); - if (configurationValue is not null) - return configurationValue; - - //Fall back to the environment variable with the same name or default to an empty string - var envVar = Environment.GetEnvironmentVariable(name); - return envVar ?? string.Empty; - } - - /// - /// Builds the endpoint provided an optional endpoint and optional port. - /// - /// - /// Marked as internal for testing purposes. - /// - /// The endpoint. - /// The port - /// A constructed endpoint value. - internal static string BuildEndpoint(string? endpoint, int? endpointPort) - { - if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is null) - return string.Empty; - - var endpointBuilder = new UriBuilder(); - if (!string.IsNullOrWhiteSpace(endpoint)) - { - //Extract the scheme, host and port from the endpoint - var uri = new Uri(endpoint); - endpointBuilder.Scheme = uri.Scheme; - endpointBuilder.Host = uri.Host; - endpointBuilder.Port = uri.Port; - - //Update the port if provided separately - if (endpointPort is not null) - endpointBuilder.Port = (int)endpointPort; - } - else if (string.IsNullOrWhiteSpace(endpoint) && endpointPort is not null) - { - endpointBuilder.Host = "localhost"; - endpointBuilder.Port = (int)endpointPort; - } - - return endpointBuilder.ToString(); - } }