diff --git a/Deepgram/Abstractions/AbstractRestClient.cs b/Deepgram/Abstractions/AbstractRestClient.cs index 343cc469..bc3cae3b 100644 --- a/Deepgram/Abstractions/AbstractRestClient.cs +++ b/Deepgram/Abstractions/AbstractRestClient.cs @@ -16,18 +16,21 @@ public abstract class AbstractRestClient /// /// Copy of the options for the client /// - internal DeepgramHttpClientOptions _options; + internal IDeepgramClientOptions _options; /// /// Constructor that take the options and a httpClient /// /// Options for the Deepgram client - internal AbstractRestClient(string? apiKey = null, DeepgramHttpClientOptions? options = null, string? httpId = null) + internal AbstractRestClient(string? apiKey = null, IDeepgramClientOptions? options = null, string? httpId = null) { Log.Verbose("AbstractRestClient", "ENTER"); - options ??= new DeepgramHttpClientOptions(apiKey); + if (options == null) + { + options = (IDeepgramClientOptions) new DeepgramHttpClientOptions(apiKey); + } _httpClient = HttpClientFactory.Create(httpId); _httpClient = HttpClientFactory.ConfigureDeepgram(_httpClient, options); _options = options; @@ -681,7 +684,7 @@ public virtual async Task DeleteAsync(string uriSegment, S? parameter, } } - internal static string GetUri(DeepgramHttpClientOptions options, string path) + internal static string GetUri(IDeepgramClientOptions options, string path) { return $"{options.BaseAddress}/{path}"; } diff --git a/Deepgram/Clients/Analyze/v1/Client.cs b/Deepgram/Clients/Analyze/v1/Client.cs index 36da3432..a6a2129e 100644 --- a/Deepgram/Clients/Analyze/v1/Client.cs +++ b/Deepgram/Clients/Analyze/v1/Client.cs @@ -13,7 +13,7 @@ namespace Deepgram.Clients.Analyze.v1; /// /// Required DeepgramApiKey /// for HttpClient Configuration -public class Client(string? apiKey = null, DeepgramHttpClientOptions? deepgramClientOptions = null, string? httpId = null) +public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) : AbstractRestClient(apiKey, deepgramClientOptions, httpId), IAnalyzeClient { #region NoneCallBacks diff --git a/Deepgram/Clients/Live/v1/Client.cs b/Deepgram/Clients/Live/v1/Client.cs index db1ba832..da21b013 100644 --- a/Deepgram/Clients/Live/v1/Client.cs +++ b/Deepgram/Clients/Live/v1/Client.cs @@ -15,7 +15,7 @@ namespace Deepgram.Clients.Live.v1; public class Client : IDisposable, ILiveClient { #region Fields - private readonly DeepgramWsClientOptions _deepgramClientOptions; + private readonly IDeepgramClientOptions _deepgramClientOptions; private ClientWebSocket? _clientWebSocket; private CancellationTokenSource? _cancellationTokenSource; @@ -25,8 +25,8 @@ public class Client : IDisposable, ILiveClient #endregion /// Required DeepgramApiKey - /// for HttpClient Configuration - public Client(string? apiKey = null, DeepgramWsClientOptions? options = null) + /// for HttpClient Configuration + public Client(string? apiKey = null, IDeepgramClientOptions? options = null) { Log.Verbose("LiveClient", "ENTER"); @@ -800,7 +800,7 @@ await _clientWebSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, stri /// /// Get the URI for the WebSocket connection /// - internal static Uri GetUri(DeepgramWsClientOptions options, LiveSchema parameter, Dictionary? addons = null) + internal static Uri GetUri(IDeepgramClientOptions options, LiveSchema parameter, Dictionary? addons = null) { var propertyInfoList = parameter.GetType() .GetProperties() diff --git a/Deepgram/Clients/Manage/v1/Client.cs b/Deepgram/Clients/Manage/v1/Client.cs index 17e6d519..f7360911 100644 --- a/Deepgram/Clients/Manage/v1/Client.cs +++ b/Deepgram/Clients/Manage/v1/Client.cs @@ -13,7 +13,7 @@ namespace Deepgram.Clients.Manage.v1; /// /// Required DeepgramApiKey /// for HttpClient Configuration -public class Client(string? apiKey = null, DeepgramHttpClientOptions? deepgramClientOptions = null, string? httpId = null) +public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) : AbstractRestClient(apiKey, deepgramClientOptions, httpId), IManageClient { #region Projects diff --git a/Deepgram/Clients/OnPrem/v1/Client.cs b/Deepgram/Clients/OnPrem/v1/Client.cs index d8c4b2d6..ac4b1f5d 100644 --- a/Deepgram/Clients/OnPrem/v1/Client.cs +++ b/Deepgram/Clients/OnPrem/v1/Client.cs @@ -13,7 +13,7 @@ namespace Deepgram.Clients.OnPrem.v1; /// /// Required DeepgramApiKey /// for HttpClient Configuration -public class Client(string? apiKey = null, DeepgramHttpClientOptions? deepgramClientOptions = null, string? httpId = null) +public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) : AbstractRestClient(apiKey, deepgramClientOptions, httpId), IOnPremClient { /// diff --git a/Deepgram/Clients/PreRecorded/v1/Client.cs b/Deepgram/Clients/PreRecorded/v1/Client.cs index 596d0088..b332f171 100644 --- a/Deepgram/Clients/PreRecorded/v1/Client.cs +++ b/Deepgram/Clients/PreRecorded/v1/Client.cs @@ -13,7 +13,7 @@ namespace Deepgram.Clients.PreRecorded.v1; /// /// Required DeepgramApiKey /// for HttpClient Configuration -public class Client(string? apiKey = null, DeepgramHttpClientOptions? deepgramClientOptions = null, string? httpId = null) +public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) : AbstractRestClient(apiKey, deepgramClientOptions, httpId), IPreRecordedClient { diff --git a/Deepgram/Clients/Speak/v1/Client.cs b/Deepgram/Clients/Speak/v1/Client.cs index ca58c4a1..fe148c96 100644 --- a/Deepgram/Clients/Speak/v1/Client.cs +++ b/Deepgram/Clients/Speak/v1/Client.cs @@ -13,7 +13,7 @@ namespace Deepgram.Clients.Speak.v1; /// /// Required DeepgramApiKey /// for HttpClient Configuration -public class Client(string? apiKey = null, DeepgramHttpClientOptions? deepgramClientOptions = null, string? httpId = null) +public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) : AbstractRestClient(apiKey, deepgramClientOptions, httpId), ISpeakClient { #region NoneCallBacks diff --git a/Deepgram/Factory/HttpClientFactory.cs b/Deepgram/Factory/HttpClientFactory.cs index f6fbaaa9..6422682e 100644 --- a/Deepgram/Factory/HttpClientFactory.cs +++ b/Deepgram/Factory/HttpClientFactory.cs @@ -32,7 +32,7 @@ public static HttpClient Create(string? httpId = null) return client; } - internal static HttpClient ConfigureDeepgram(HttpClient client, DeepgramHttpClientOptions? options = null) + internal static HttpClient ConfigureDeepgram(HttpClient client, IDeepgramClientOptions? options = null) { options ??= new DeepgramHttpClientOptions(); diff --git a/Deepgram/Models/Authenticate/v1/DeepgramHttpClientOptions.cs b/Deepgram/Models/Authenticate/v1/DeepgramHttpClientOptions.cs index 0b76bc88..c685e6d7 100644 --- a/Deepgram/Models/Authenticate/v1/DeepgramHttpClientOptions.cs +++ b/Deepgram/Models/Authenticate/v1/DeepgramHttpClientOptions.cs @@ -7,7 +7,7 @@ namespace Deepgram.Models.Authenticate.v1; /// /// Configuration for the Deepgram client /// -public class DeepgramHttpClientOptions +public class DeepgramHttpClientOptions : IDeepgramClientOptions { /*****************************/ // General Options @@ -37,6 +37,11 @@ public class DeepgramHttpClientOptions // Prerecorded /*****************************/ + /*****************************/ + // Live + /*****************************/ + public bool KeepAlive { get; } + /*****************************/ // OnPrem /*****************************/ @@ -68,6 +73,7 @@ public DeepgramHttpClientOptions(string? apiKey = null, string? baseAddress = nu Log.Debug("DeepgramHttpClientOptions", onPrem == null ? "OnPrem is null" : "OnPrem provided"); Log.Debug("DeepgramHttpClientOptions", headers == null ? "Headers is null" : "Headers provided"); + KeepAlive = false; ApiKey = apiKey ?? ""; BaseAddress = baseAddress ?? Defaults.DEFAULT_URI; OnPrem = onPrem ?? false; diff --git a/Deepgram/Models/Authenticate/v1/DeepgramOptionsFromEnv.cs b/Deepgram/Models/Authenticate/v1/DeepgramOptionsFromEnv.cs new file mode 100644 index 00000000..1645b1da --- /dev/null +++ b/Deepgram/Models/Authenticate/v1/DeepgramOptionsFromEnv.cs @@ -0,0 +1,96 @@ +// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Use of this source code is governed by a MIT license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT + +namespace Deepgram.Models.Authenticate.v1; + +public class DeepgramOptionsFromEnv : IDeepgramClientOptions +{ + /*****************************/ + // General Options + /*****************************/ + /// + /// Deepgram API KEY + /// + public string ApiKey { get; set; } + + /// + /// BaseAddress of the server :defaults to api.deepgram.com + /// no need to attach the protocol it will be added internally + /// + public string BaseAddress { get; set; } = Defaults.DEFAULT_URI; + + /// + /// Api endpoint version + /// + public string APIVersion { get; set; } = Defaults.DEFAULT_API_VERSION; + + /// + /// Global headers to always be added to the request + /// + public Dictionary Headers { get; set; } + + /*****************************/ + // Prerecorded + /*****************************/ + + /*****************************/ + // Live + /*****************************/ + /// + /// Enable sending KeepAlives for Streaming + /// + public bool KeepAlive { get; set; } = false; + + /*****************************/ + // OnPrem + /*****************************/ + /// + /// Enable when using OnPrem mode + /// + public bool OnPrem { get; set; } = false; + + /*****************************/ + // Manage + /*****************************/ + + /*****************************/ + // Analyze + /*****************************/ + + /*****************************/ + // Speak + /*****************************/ + + /*****************************/ + // Constructor + /*****************************/ + + public DeepgramOptionsFromEnv() + { + ApiKey = Environment.GetEnvironmentVariable("DEEPGRAM_API_KEY") ?? ""; + BaseAddress = Environment.GetEnvironmentVariable("DEEPGRAM_HOST") ?? Defaults.DEFAULT_URI; + APIVersion = Environment.GetEnvironmentVariable("DEEPGRAM_API_VERSION") ?? Defaults.DEFAULT_API_VERSION; + var onPrem = Environment.GetEnvironmentVariable("DEEPGRAM_ON_PREM") ?? ""; + var keepAlive = Environment.GetEnvironmentVariable("DEEPGRAM_KEEP_ALIVE") ?? ""; + + Headers = new Dictionary(); + for (int x = 0; x < 20; x++) + { + var param = Environment.GetEnvironmentVariable($"DEEPGRAM_PARAM_{x}"); + if (param != null) + { + var value = Environment.GetEnvironmentVariable($"DEEPGRAM_PARAM_VALUE_{x}") ?? ""; + Headers[param] = value; + } + else + { + break; + } + } + + OnPrem = onPrem.ToLower() == "true"; + KeepAlive = keepAlive.ToLower() == "true"; + } + +} diff --git a/Deepgram/Models/Authenticate/v1/DeepgramWsClientOptions.cs b/Deepgram/Models/Authenticate/v1/DeepgramWsClientOptions.cs index 88e6bddd..b7fb5f50 100644 --- a/Deepgram/Models/Authenticate/v1/DeepgramWsClientOptions.cs +++ b/Deepgram/Models/Authenticate/v1/DeepgramWsClientOptions.cs @@ -7,7 +7,7 @@ namespace Deepgram.Models.Authenticate.v1; /// /// Configuration for the Deepgram client /// -public class DeepgramWsClientOptions +public class DeepgramWsClientOptions : IDeepgramClientOptions { /*****************************/ // General Options diff --git a/Deepgram/Models/Authenticate/v1/IDeepgramOptions.cs b/Deepgram/Models/Authenticate/v1/IDeepgramOptions.cs new file mode 100644 index 00000000..d4a60b16 --- /dev/null +++ b/Deepgram/Models/Authenticate/v1/IDeepgramOptions.cs @@ -0,0 +1,64 @@ +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Use of this source code is governed by a MIT license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT + +namespace Deepgram.Models.Authenticate.v1; + +public interface IDeepgramClientOptions +{ + /*****************************/ + // General Options + /*****************************/ + /// + /// Deepgram API KEY + /// + public string ApiKey { get; } + + /// + /// BaseAddress of the server :defaults to api.deepgram.com + /// no need to attach the protocol it will be added internally + /// + public string BaseAddress { get; } + + /// + /// Api endpoint version + /// + public string APIVersion { get; } + + /// + /// Global headers to always be added to the request + /// + public Dictionary Headers { get; } + + /*****************************/ + // Prerecorded + /*****************************/ + + /*****************************/ + // Live + /*****************************/ + /// + /// Enable sending KeepAlives for Streaming + /// + public bool KeepAlive { get; } + + /*****************************/ + // OnPrem + /*****************************/ + /// + /// Enable when using OnPrem mode + /// + public bool OnPrem { get; } + + /*****************************/ + // Manage + /*****************************/ + + /*****************************/ + // Analyze + /*****************************/ + + /*****************************/ + // Speak + /*****************************/ +}