From b52d203fe0f9c69bcf4466fa20ea143a001344f2 Mon Sep 17 00:00:00 2001 From: dvonthenen Date: Fri, 1 Nov 2024 08:41:41 -0700 Subject: [PATCH] Reorganize Factory, Add CancelToken Option to Send Functions --- .../Abstractions/v2/AbstractRestClient.cs | 2 +- .../v2/AbstractWebSocketClient.cs | 24 +- Deepgram/Abstractions/v2/Utilities.cs | 2 +- Deepgram/Abstractions/v2/WebSocketMessage.cs | 2 +- Deepgram/ClientFactory.cs | 255 ++++++++++-------- .../Interfaces/v2/IListenWebSocketClient.cs | 8 +- Deepgram/Clients/Interfaces/v2/ILiveClient.cs | 73 ----- .../Clients/Interfaces/v2/IOnPremClient.cs | 20 -- .../Interfaces/v2/IPreRecordedClient.cs | 20 -- .../Clients/Interfaces/v2/ISpeakClient.cs | 20 -- .../Interfaces/v2/ISpeakWebSocketClient.cs | 5 +- .../Clients/Listen/v2/WebSocket/Client.cs | 9 +- .../Listen/v2/WebSocket/ResponseEvent.cs | 2 +- .../Listen/v2/WebSocket/UriSegments.cs | 2 +- Deepgram/Clients/Speak/v2/WebSocket/Client.cs | 9 +- .../Common/v2/WebSocket/WebSocketType.cs | 2 +- .../Models/Listen/v2/WebSocket/Alternative.cs | 2 +- .../Models/Listen/v2/WebSocket/Average.cs | 2 +- .../Models/Listen/v2/WebSocket/Channel.cs | 2 +- Deepgram/Models/Listen/v2/WebSocket/Hit.cs | 2 +- .../Models/Listen/v2/WebSocket/ListenType.cs | 2 +- .../Models/Listen/v2/WebSocket/LiveSchema.cs | 2 +- .../Models/Listen/v2/WebSocket/Metadata.cs | 2 +- .../Listen/v2/WebSocket/MetadataResponse.cs | 2 +- .../Models/Listen/v2/WebSocket/ModelInfo.cs | 2 +- .../Listen/v2/WebSocket/ResultResponse.cs | 2 +- Deepgram/Models/Listen/v2/WebSocket/Search.cs | 2 +- .../v2/WebSocket/SpeechStartedResponse.cs | 2 +- .../v2/WebSocket/UtteranceEndResponse.cs | 2 +- Deepgram/Models/Listen/v2/WebSocket/Word.cs | 2 +- .../websocket/microphone/Program.cs | 4 +- .../websocket/simple/Program.cs | 4 +- 32 files changed, 198 insertions(+), 293 deletions(-) delete mode 100644 Deepgram/Clients/Interfaces/v2/ILiveClient.cs delete mode 100644 Deepgram/Clients/Interfaces/v2/IOnPremClient.cs delete mode 100644 Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs delete mode 100644 Deepgram/Clients/Interfaces/v2/ISpeakClient.cs diff --git a/Deepgram/Abstractions/v2/AbstractRestClient.cs b/Deepgram/Abstractions/v2/AbstractRestClient.cs index 0e165d18..e750f9b1 100644 --- a/Deepgram/Abstractions/v2/AbstractRestClient.cs +++ b/Deepgram/Abstractions/v2/AbstractRestClient.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs b/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs index 16bdbec1..1498c7dc 100644 --- a/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs +++ b/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs @@ -245,7 +245,7 @@ public async Task Subscribe(EventHandler eventHandler) /// /// Sends a Close message to Deepgram /// - public virtual Task SendClose(bool nullByte = false) + public virtual Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null) { throw new DeepgramException("Unimplemented"); } @@ -271,8 +271,8 @@ public virtual void SendMessage(byte[] data, int length = Constants.UseArrayLeng /// /// This method sends a binary message over the WebSocket connection immediately without queueing. - /// - public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend) + /// , + public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null) { if (!IsConnected()) { @@ -280,7 +280,10 @@ public virtual async Task SendBinaryImmediately(byte[] data, int length = Consta return; } - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + + await _mutexSend.WaitAsync(_cancelToken.Token); try { Log.Verbose("SendBinaryImmediately", "Sending binary message immediately..."); @@ -288,7 +291,7 @@ public virtual async Task SendBinaryImmediately(byte[] data, int length = Consta { length = data.Length; } - await _clientWebSocket.SendAsync(new ArraySegment(data, 0, length), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token) + await _clientWebSocket.SendAsync(new ArraySegment(data, 0, length), WebSocketMessageType.Binary, true, _cancelToken.Token) .ConfigureAwait(false); } finally @@ -300,7 +303,7 @@ await _clientWebSocket.SendAsync(new ArraySegment(data, 0, length), WebSoc /// /// This method sends a text message over the WebSocket connection immediately without queueing. /// - public virtual async Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend) + public virtual async Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null) { if (!IsConnected()) { @@ -308,7 +311,10 @@ public virtual async Task SendMessageImmediately(byte[] data, int length = Const return; } - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + + await _mutexSend.WaitAsync(_cancelToken.Token); try { Log.Verbose("SendMessageImmediately", "Sending text message immediately..."); @@ -316,7 +322,7 @@ public virtual async Task SendMessageImmediately(byte[] data, int length = Const { length = data.Length; } - await _clientWebSocket.SendAsync(new ArraySegment(data, 0, length), WebSocketMessageType.Text, true, _cancellationTokenSource.Token) + await _clientWebSocket.SendAsync(new ArraySegment(data, 0, length), WebSocketMessageType.Text, true, _cancelToken.Token) .ConfigureAwait(false); } finally @@ -613,7 +619,7 @@ public async Task Stop(CancellationTokenSource? cancelToken = null, bool n if (_clientWebSocket!.State == WebSocketState.Open) { Log.Debug("Stop", "Sending Close message..."); - await SendClose(nullByte); + await SendClose(nullByte, cancelToken); } // small delay to wait for any final transcription diff --git a/Deepgram/Abstractions/v2/Utilities.cs b/Deepgram/Abstractions/v2/Utilities.cs index 65f134d1..6f130f8d 100644 --- a/Deepgram/Abstractions/v2/Utilities.cs +++ b/Deepgram/Abstractions/v2/Utilities.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Abstractions/v2/WebSocketMessage.cs b/Deepgram/Abstractions/v2/WebSocketMessage.cs index 31a5182b..410afe2d 100644 --- a/Deepgram/Abstractions/v2/WebSocketMessage.cs +++ b/Deepgram/Abstractions/v2/WebSocketMessage.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/ClientFactory.cs b/Deepgram/ClientFactory.cs index 17e2f7d6..9eb46e2d 100644 --- a/Deepgram/ClientFactory.cs +++ b/Deepgram/ClientFactory.cs @@ -27,31 +27,6 @@ public static V1.IAnalyzeClient CreateAnalyzeClient(string apiKey = "", Deepgram return new AnalyzeClient(apiKey, options, httpId); } - /// - /// This method allows you to create an AnalyzeClient with a specific version of the client. - /// - public static object CreateAnalyzeClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) - { - // Currently only a single version of the AnalyzeClient exists - return new AnalyzeClient(apiKey, options, httpId); - } - - /// - // *********** WARNING *********** - // This function creates a LiveClient for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateListenWebSocketClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** - /// - [Obsolete("Please use CreateListenWebSocketClient instead", false)] - public static V1.ILiveClient CreateLiveClient(string apiKey = "", DeepgramWsClientOptions? options = null) - { - return new LiveClient(apiKey, options); - } - /// /// Create a new ListenWebSocketClient using the latest version /// @@ -63,25 +38,6 @@ public static V2.IListenWebSocketClient CreateListenWebSocketClient(string apiKe return new ListenWebSocketClient(apiKey, options); } - /// - /// This method allows you to create an AnalyzeClient with a specific version of the client. - /// - public static object CreateListenWebSocketClient(int version, string apiKey = "", DeepgramWsClientOptions? options = null) - { - // at some point this needs to be changed to use reflection to get the type of the client - switch(version) - { - case 1: - Log.Information("ClientFactory", $"Version 1 of the ListenWebSocketClient is being deprecated in the next major version."); - Log.Information("ClientFactory", $"Transition to the latest version at your earliest convenience."); - return new ListenV1.Client(apiKey, options); - case 2: - return new ListenV2.Client(apiKey, options); - default: - throw new ArgumentException("Invalid version", nameof(version)); - } - } - /// /// Create a new ManageClient /// @@ -94,31 +50,6 @@ public static V1.IManageClient CreateManageClient(string apiKey = "", DeepgramHt return new ManageClient(apiKey, options, httpId); } - /// - /// This method allows you to create an ManageClient with a specific version of the client. - /// - public static object CreateManageClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) - { - // Currently only a single version of the ManageClient exists - return new ManageClient(apiKey, options, httpId); - } - - /// - // *********** WARNING *********** - // This function creates a OnPrem Client for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateSelfHostedClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** - /// - [Obsolete("Please use CreateSelfHostedClient instead", false)] - public static V1.IOnPremClient CreateOnPremClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) - { - return new OnPremClient(apiKey, options, httpId); - } - /// /// Create a new SelfHostedClient /// @@ -132,44 +63,64 @@ public static V1.ISelfHostedClient CreateSelfHostedClient(string apiKey = "", De } /// - /// This method allows you to create an SelfHostedClient with a specific version of the client. + /// Create a new ListenRESTClient /// - public static object CreateSelfHostedClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + /// + /// + /// + /// + public static V1.IListenRESTClient CreateListenRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - // Currently only a single version of the SelfHostedClient exists - return new SelfHostedClient(apiKey, options, httpId); + return new ListenRESTClient(apiKey, options, httpId); } /// - // *********** WARNING *********** - // This function creates a PreRecordedClient for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateListenRESTClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** + /// Create a new SpeakRESTClient /// - [Obsolete("Please use CreateListenRESTClient instead", false)] - public static V1.IPreRecordedClient CreatePreRecordedClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + /// + /// + /// + /// + public static V1.ISpeakRESTClient CreateSpeakRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - return new PreRecordedClient(apiKey, options, httpId); + return new SpeakRESTClient(apiKey, options, httpId); } /// - /// Create a new ListenRESTClient + /// Create a new AnalyzeClient /// /// /// - /// /// - public static V1.IListenRESTClient CreateListenRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static V2.ISpeakWebSocketClient CreateSpeakWebSocketClient(string apiKey = "", DeepgramWsClientOptions? options = null) { - return new ListenRESTClient(apiKey, options, httpId); + return new SpeakWebSocketClient(apiKey, options); + } + + + + /// + /// These functions are only used to create specific and typically older verions of Listen, Speak, etc clients. + /// The functions above always create the latest version of the client. + /// + /// The functions below should only be used to reference an older client with the intnetion of at some point to move away from + /// that version to the latest version (ie transition to the function above). Older clients will be removed at the next major version + /// and the latest version will be renamed to v1. + /// + + /// + /// This method allows you to create an AnalyzeClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client + /// + public static object CreateAnalyzeClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + // Currently only a single version of the AnalyzeClient exists + return new AnalyzeClient(apiKey, options, httpId); } /// /// This method allows you to create an ListenRESTClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client /// public static object CreateListenRESTClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { @@ -178,55 +129,58 @@ public static object CreateListenRESTClient(int version, string apiKey = "", Dee } /// - // *********** WARNING *********** - // This function creates a Speak Client for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateSpeakRESTClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** + /// This method allows you to create an AnalyzeClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client /// - [Obsolete("Please use CreateSpeakRESTClient instead", false)] - public static V1.ISpeakClient CreateSpeakClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static object CreateListenWebSocketClient(int version, string apiKey = "", DeepgramWsClientOptions? options = null) { - return new SpeakClient(apiKey, options, httpId); + // at some point this needs to be changed to use reflection to get the type of the client + switch (version) + { + case 1: + Log.Information("ClientFactory", $"Version 1 of the ListenWebSocketClient is being deprecated in the next major version."); + Log.Information("ClientFactory", $"Transition to the latest version at your earliest convenience."); + return new ListenV1.Client(apiKey, options); + case 2: + return new ListenV2.Client(apiKey, options); + default: + throw new ArgumentException("Invalid version", nameof(version)); + } } /// - /// Create a new SpeakRESTClient + /// This method allows you to create an ManageClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client /// - /// - /// - /// - /// - public static V1.ISpeakRESTClient CreateSpeakRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static object CreateManageClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - return new SpeakRESTClient(apiKey, options, httpId); + // Currently only a single version of the ManageClient exists + return new ManageClient(apiKey, options, httpId); } /// - /// This method allows you to create an SpeakRESTClient with a specific version of the client. + /// This method allows you to create an SelfHostedClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client /// - public static object CreateSpeakRESTClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static object CreateSelfHostedClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - // Currently only a single version of the SpeakRESTClient exists - return new SpeakRESTClient(apiKey, options, httpId); + // Currently only a single version of the SelfHostedClient exists + return new SelfHostedClient(apiKey, options, httpId); } /// - /// Create a new AnalyzeClient + /// This method allows you to create an SpeakRESTClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client /// - /// - /// - /// - public static V2.ISpeakWebSocketClient CreateSpeakWebSocketClient(string apiKey = "", DeepgramWsClientOptions? options = null) + public static object CreateSpeakRESTClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - return new SpeakWebSocketClient(apiKey, options); + // Currently only a single version of the SpeakRESTClient exists + return new SpeakRESTClient(apiKey, options, httpId); } /// /// This method allows you to create an SpeakWebSocketClient with a specific version of the client. + /// TODO: this should be revisited at a later time. Opportunity to use reflection to get the type of the client /// public static object CreateSpeakWebSocketClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null) { @@ -243,4 +197,73 @@ public static object CreateSpeakWebSocketClient(int version, string apiKey = "", throw new ArgumentException("Invalid version", nameof(version)); } } + + /// + /// The functions below are deprecated and will be removed in the next major version. You should not use + /// these functions for new projects. They are only here to support older projects that are using these functions. + /// + + /// + // *********** WARNING *********** + // This function creates a LiveClient for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateListenWebSocketClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateListenWebSocketClient instead", false)] + public static V1.ILiveClient CreateLiveClient(string apiKey = "", DeepgramWsClientOptions? options = null) + { + return new LiveClient(apiKey, options); + } + + /// + // *********** WARNING *********** + // This function creates a Speak Client for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateSpeakRESTClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateSpeakRESTClient instead", false)] + public static V1.ISpeakClient CreateSpeakClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new SpeakClient(apiKey, options, httpId); + } + + /// + // *********** WARNING *********** + // This function creates a PreRecordedClient for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateListenRESTClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateListenRESTClient instead", false)] + public static V1.IPreRecordedClient CreatePreRecordedClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new PreRecordedClient(apiKey, options, httpId); + } + + /// + // *********** WARNING *********** + // This function creates a OnPrem Client for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateSelfHostedClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateSelfHostedClient instead", false)] + public static V1.IOnPremClient CreateOnPremClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new OnPremClient(apiKey, options, httpId); + } } diff --git a/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs b/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs index d59cce6d..6d26ff20 100644 --- a/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs +++ b/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs @@ -84,7 +84,7 @@ public Task Connect(LiveSchema options, CancellationTokenSource? cancelTok /// /// Sends a Close message to Deepgram /// - public Task SendClose(bool nullByte = false); + public Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null); /// /// Sends a binary message over the WebSocket connection. @@ -111,14 +111,16 @@ public Task Connect(LiveSchema options, CancellationTokenSource? cancelTok /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. - public Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend); + /// /// Provide a cancel token to be used for the send function or use the internal one + public Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null); /// /// This method sends a text message over the WebSocket connection immediately without queueing. /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. - public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend); + /// /// Provide a cancel token to be used for the send function or use the internal one + public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null); #endregion #region Helpers diff --git a/Deepgram/Clients/Interfaces/v2/ILiveClient.cs b/Deepgram/Clients/Interfaces/v2/ILiveClient.cs deleted file mode 100644 index d9d574c1..00000000 --- a/Deepgram/Clients/Interfaces/v2/ILiveClient.cs +++ /dev/null @@ -1,73 +0,0 @@ -// 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 - -using Deepgram.Models.Live.v1; - -namespace Deepgram.Clients.Interfaces.v2; - -/// -// *********** WARNING *********** -// This is the ILiveClient interface -// -// Deprecated: This class is deprecated. Use the `IListenWebSocketClient` function instead. -// This will be removed in a future release. -// -// This class is frozen and no new functionality will be added. -// *********** WARNING *********** -/// -[Obsolete("Please use IListenWebSocketClient instead", false)] -public interface ILiveClient : IListenWebSocketClient -{ - #region Subscribe Event - /// - /// Subscribe to an Open event from the Deepgram API - /// - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a Metadata event from the Deepgram API - /// - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a Results event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to an UtteranceEnd event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a SpeechStarted event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a Close event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to an Unhandled event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to an Error event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - #endregion -} diff --git a/Deepgram/Clients/Interfaces/v2/IOnPremClient.cs b/Deepgram/Clients/Interfaces/v2/IOnPremClient.cs deleted file mode 100644 index a62f2f27..00000000 --- a/Deepgram/Clients/Interfaces/v2/IOnPremClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -// 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.Clients.Interfaces.v2; - -/// -// *********** WARNING *********** -// This class provides the IOnPremClient implementation -// -// Deprecated: This class is deprecated. Use ISelfHostedClient instead. -// This will be removed in a future release. -// -// This package is frozen and no new functionality will be added. -// *********** WARNING *********** -/// -[Obsolete("Please use ISelfHostedClient instead", false)] -public interface IOnPremClient : ISelfHostedClient -{ -} diff --git a/Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs b/Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs deleted file mode 100644 index 7fcf8749..00000000 --- a/Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -// 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.Clients.Interfaces.v2; - -/// -// *********** WARNING *********** -// This is the IPreRecordedClient interface -// -// Deprecated: This class is deprecated. Use the `IListenRESTClient` function instead. -// This will be removed in a future release. -// -// This class is frozen and no new functionality will be added. -// *********** WARNING *********** -/// -[Obsolete("Please use IListenRESTClient instead", false)] -public interface IPreRecordedClient : IListenRESTClient -{ -} diff --git a/Deepgram/Clients/Interfaces/v2/ISpeakClient.cs b/Deepgram/Clients/Interfaces/v2/ISpeakClient.cs deleted file mode 100644 index d5ad0807..00000000 --- a/Deepgram/Clients/Interfaces/v2/ISpeakClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -// 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.Clients.Interfaces.v2; - -/// -/// *********** WARNING *********** -/// This class provides the ISpeakClient implementation for the Deepgram API -/// -/// Deprecated: This class is deprecated. Use the ISpeakRESTClient interface instead. -/// This will be removed in a future release. -/// -/// This package is frozen and no new functionality will be added. -/// *********** WARNING *********** -/// -[Obsolete("Please use ISpeakRESTClient instead", false)] -public interface ISpeakClient : ISpeakRESTClient -{ -} diff --git a/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs b/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs index 48f01290..1a013049 100644 --- a/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs +++ b/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs @@ -108,7 +108,7 @@ public Task Connect(SpeakSchema options, CancellationTokenSource? cancelTo /// /// Sends a Close message to Deepgram /// - public Task SendClose(bool nullByte = false); + public Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null); /// /// Sends a binary message over the WebSocket connection. @@ -140,7 +140,8 @@ public Task Connect(SpeakSchema options, CancellationTokenSource? cancelTo /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. - public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend); + /// /// Provide a cancel token to be used for the send function or use the internal one + public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null); #endregion #region Helpers diff --git a/Deepgram/Clients/Listen/v2/WebSocket/Client.cs b/Deepgram/Clients/Listen/v2/WebSocket/Client.cs index 096cea4b..05b74948 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/Client.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/Client.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 @@ -294,7 +294,7 @@ public async Task SendFinalize() /// /// Sends a Close message to Deepgram /// - public override async Task SendClose(bool nullByte = false) + public override async Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null) { if (_clientWebSocket == null || !IsConnected()) { @@ -302,11 +302,14 @@ public override async Task SendClose(bool nullByte = false) return; } + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + Log.Debug("SendClose", "Sending Close Message Immediately..."); if (nullByte) { // send a close to Deepgram - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + await _mutexSend.WaitAsync(_cancelToken.Token); try { await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token) diff --git a/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs b/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs index 2cd1a118..2a920e55 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs b/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs index a51f2f8a..d05e90b3 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Clients/Speak/v2/WebSocket/Client.cs b/Deepgram/Clients/Speak/v2/WebSocket/Client.cs index 0677917a..9569bd6d 100644 --- a/Deepgram/Clients/Speak/v2/WebSocket/Client.cs +++ b/Deepgram/Clients/Speak/v2/WebSocket/Client.cs @@ -327,7 +327,7 @@ public void Close(bool nullByte = false) /// This method sends a close message over the WebSocket connection. /// NOTE: This is fine to use the SendImmediately methods because you want to shutdown the websocket ASAP. /// - public override async Task SendClose(bool nullByte = false) + public override async Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null) { if (_clientWebSocket == null || !IsConnected()) { @@ -335,11 +335,14 @@ public override async Task SendClose(bool nullByte = false) return; } + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + Log.Debug("SendClose", "Sending Close Message Immediately..."); if (nullByte) { // send a close to Deepgram - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + await _mutexSend.WaitAsync(_cancelToken.Token); try { await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token) @@ -354,7 +357,7 @@ await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebS ControlMessage controlMessage = new ControlMessage(Constants.Close); byte[] data = Encoding.UTF8.GetBytes(controlMessage.ToString()); - await SendMessageImmediately(data); + await SendMessageImmediately(data, Constants.UseArrayLengthForSend, _cancelToken); } /// diff --git a/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs b/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs index 1825b190..c1baf529 100644 --- a/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs +++ b/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs b/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs index 0a59fb61..39f9ae51 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Average.cs b/Deepgram/Models/Listen/v2/WebSocket/Average.cs index 3809bb74..03b0e54d 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Average.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Average.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Channel.cs b/Deepgram/Models/Listen/v2/WebSocket/Channel.cs index 51fab15d..5523d91f 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Channel.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Channel.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Hit.cs b/Deepgram/Models/Listen/v2/WebSocket/Hit.cs index 4f6dedc7..a3a9410a 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Hit.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Hit.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs b/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs index 5f2744d2..8d3e0d3d 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs b/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs index 67fe0e82..a36dcea7 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs b/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs index 9ace5968..8583a79f 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs index 170aea34..b31a7a8d 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs b/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs index 4bd44eb6..8e05e62d 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs index e55ed77d..bc75f64a 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Search.cs b/Deepgram/Models/Listen/v2/WebSocket/Search.cs index d003e816..f7549c31 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Search.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Search.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs index 568410c2..046faf4f 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs index 947c5d6d..ab5a3ac2 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/Deepgram/Models/Listen/v2/WebSocket/Word.cs b/Deepgram/Models/Listen/v2/WebSocket/Word.cs index 55ebe3ca..8ba0c3e9 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Word.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Word.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// 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 diff --git a/examples/speech-to-text/websocket/microphone/Program.cs b/examples/speech-to-text/websocket/microphone/Program.cs index f0a4c1c5..8e4630f1 100644 --- a/examples/speech-to-text/websocket/microphone/Program.cs +++ b/examples/speech-to-text/websocket/microphone/Program.cs @@ -17,9 +17,9 @@ static async Task Main(string[] args) { // Initialize Library with default logging // Normal logging is "Info" level - //Deepgram.Library.Initialize(); + Deepgram.Library.Initialize(); // OR very chatty logging - Deepgram.Library.Initialize(LogLevel.Verbose); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose + //Deepgram.Library.Initialize(LogLevel.Debug); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose // Initialize the microphone library Deepgram.Microphone.Library.Initialize(); diff --git a/examples/text-to-speech/websocket/simple/Program.cs b/examples/text-to-speech/websocket/simple/Program.cs index 712bca87..70161fc5 100644 --- a/examples/text-to-speech/websocket/simple/Program.cs +++ b/examples/text-to-speech/websocket/simple/Program.cs @@ -17,9 +17,9 @@ static async Task Main(string[] args) { // Initialize Library with default logging // Normal logging is "Info" level - //Library.Initialize(); + Library.Initialize(); // OR very chatty logging - Library.Initialize(LogLevel.Verbose); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose + //Library.Initialize(LogLevel.Verbose); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose //// use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable //DeepgramWsClientOptions options = new DeepgramWsClientOptions();