diff --git a/Conductor/Client/Extensions/DependencyInjectionExtensions.cs b/Conductor/Client/Extensions/DependencyInjectionExtensions.cs index a7ff27b8..004746a9 100644 --- a/Conductor/Client/Extensions/DependencyInjectionExtensions.cs +++ b/Conductor/Client/Extensions/DependencyInjectionExtensions.cs @@ -22,7 +22,7 @@ public static IServiceCollection AddConductorWorker(this IServiceCollection serv services.AddOptions(); if (configuration == null) { - configuration = Configuration.Default; + configuration = new Configuration(); } services.AddSingleton(configuration); services.AddSingleton(); diff --git a/Conductor/Client/OrkesApiClient.cs b/Conductor/Client/OrkesApiClient.cs new file mode 100644 index 00000000..284af9e9 --- /dev/null +++ b/Conductor/Client/OrkesApiClient.cs @@ -0,0 +1,23 @@ +using Conductor.Client.Authentication; + +namespace Conductor.Client +{ + public class OrkesApiClient + { + private Configuration _configuration = null; + + public OrkesApiClient(Configuration configuration, OrkesAuthenticationSettings orkesAuthenticationSettings) + { + _configuration = configuration; + if (orkesAuthenticationSettings != null) + { + configuration.AuthenticationSettings = orkesAuthenticationSettings; + } + } + + public T GetClient() where T : IApiAccessor, new() + { + return _configuration.GetClient(); + } + } +} diff --git a/README.md b/README.md index 21cb9e5d..8d9ce64c 100644 --- a/README.md +++ b/README.md @@ -35,25 +35,16 @@ See [Access Control](https://orkes.io/content/docs/getting-started/concepts/acce ### Configure API Client ```csharp -OrkesApiClient GetApiClient(string basePath, string keyId, string keySecret) -{ - return new OrkesApiClient( - configuration: new Configuration() - { - BasePath = basePath - }, - authenticationSettings: new OrkesAuthenticationSettings( - keyId, keySecret - ) - ); -} +using Conductor.Api; +using Conductor.Client; +using Conductor.Client.Authentication; -OrkesApiClient apiClient = GetApiClient( - basePath: "https://play.orkes.io/api", - keyId: "key", - keySecret: "secret" -); -WorkflowResourceApi workflowClient = apiClient.GetClient(); +var configuration = new Configuration() { + BasePath = basePath, + AuthenticationSettings = new OrkesAuthenticationSettings(keyId, keySecret) +}; + +var workflowClient = configuration.GetClient(); workflowClient.StartWorkflow( name: "test-sdk-csharp-workflow", diff --git a/Tests/Client/OrkesApiClientTest.cs b/Tests/Client/OrkesApiClientTest.cs new file mode 100644 index 00000000..abcd3981 --- /dev/null +++ b/Tests/Client/OrkesApiClientTest.cs @@ -0,0 +1,22 @@ +using Conductor.Api; +using Conductor.Client; +using Tests.Util; +using Xunit; + +namespace Tests.Client +{ + public class OrkesApiClientTest + { + [Fact] + public void TestOrkesApiClient() + { + var configuration = ApiUtil.GetConfiguration(); + var orkesApiClient = new OrkesApiClient(configuration, null); + var workflowClient = orkesApiClient.GetClient(); + var expectedWorkflowClient = configuration.GetClient(); + var token = workflowClient.Configuration.AccessToken; + var expectedToken = expectedWorkflowClient.Configuration.AccessToken; + Assert.Equal(expectedToken, token); + } + } +} \ No newline at end of file diff --git a/Tests/Util/ApiUtil.cs b/Tests/Util/ApiUtil.cs index 5f517242..5704a80b 100644 --- a/Tests/Util/ApiUtil.cs +++ b/Tests/Util/ApiUtil.cs @@ -13,15 +13,18 @@ public class ApiUtil private const string ENV_KEY_ID = "KEY"; private const string ENV_SECRET = "SECRET"; + private static Configuration _configuration = null; + static ApiUtil() { - var configuration = new Configuration(); - configuration.BasePath = GetEnvironmentVariable(ENV_ROOT_URI); - configuration.Timeout = 5000; - configuration.AuthenticationSettings = new OrkesAuthenticationSettings( - GetEnvironmentVariable(ENV_KEY_ID), - GetEnvironmentVariable(ENV_SECRET)); - Configuration.Default = configuration; + _configuration = new Configuration() + { + Timeout = 30000, + BasePath = GetEnvironmentVariable(ENV_ROOT_URI), + AuthenticationSettings = new OrkesAuthenticationSettings( + GetEnvironmentVariable(ENV_KEY_ID), + GetEnvironmentVariable(ENV_SECRET)) + }; } public static WorkflowExecutor GetWorkflowExecutor() @@ -34,7 +37,12 @@ public static WorkflowExecutor GetWorkflowExecutor() public static T GetClient() where T : IApiAccessor, new() { - return Configuration.Default.GetClient(); + return _configuration.GetClient(); + } + + public static Configuration GetConfiguration() + { + return _configuration; } private static string GetEnvironmentVariable(string variable) diff --git a/Tests/Util/WorkerUtil.cs b/Tests/Util/WorkerUtil.cs index d36b7a37..3c0a18fe 100644 --- a/Tests/Util/WorkerUtil.cs +++ b/Tests/Util/WorkerUtil.cs @@ -13,7 +13,7 @@ public static IHost GetWorkerHost() .ConfigureServices( (ctx, services) => { - services.AddConductorWorker(); + services.AddConductorWorker(ApiUtil.GetConfiguration()); services.AddConductorWorkflowTask(); services.WithHostedService(); }