From efc15b5a39c5bfc0a3dd2948e0576ff7e48ac398 Mon Sep 17 00:00:00 2001 From: dvonthenen Date: Fri, 1 Nov 2024 16:35:31 -0700 Subject: [PATCH] Convenience Changes, Removal of Plain `Send()` to Avoid confusion --- .../v2/AbstractWebSocketClient.cs | 6 ----- .../Interfaces/v2/IListenWebSocketClient.cs | 6 +++++ .../Interfaces/v2/ISpeakWebSocketClient.cs | 6 +++++ .../Clients/Listen/v2/WebSocket/Client.cs | 16 ++++++++++--- .../Clients/Listen/v2/WebSocket/Constants.cs | 5 ++++ Deepgram/Clients/Speak/v2/WebSocket/Client.cs | 6 ++--- .../Listen/v2/WebSocket/ControlMessage.cs | 24 +++++++++++++++++++ Deepgram/Utilities/QueryParameterUtil.cs | 2 +- 8 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 Deepgram/Models/Listen/v2/WebSocket/ControlMessage.cs diff --git a/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs b/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs index 1498c7dc..059e0c5a 100644 --- a/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs +++ b/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs @@ -250,12 +250,6 @@ public virtual Task SendClose(bool nullByte = false, CancellationTokenSource? _c throw new DeepgramException("Unimplemented"); } - /// - /// Sends a binary message over the WebSocket connection. - /// - /// The data to be sent over the WebSocket. - public virtual void Send(byte[] data, int length = Constants.UseArrayLengthForSend) => SendBinary(data, length); - /// /// This method sends a binary message over the WebSocket connection. /// diff --git a/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs b/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs index 6d26ff20..03bb5064 100644 --- a/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs +++ b/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs @@ -12,9 +12,15 @@ namespace Deepgram.Clients.Interfaces.v2; public interface IListenWebSocketClient { #region Connect and Disconnect + /// + /// Connects to the Deepgram WebSocket API + /// public Task Connect(LiveSchema options, CancellationTokenSource? cancelToken = null, Dictionary? addons = null, Dictionary? headers = null); + /// + /// Disconnects from the Deepgram WebSocket API + /// public Task Stop(CancellationTokenSource? cancelToken = null, bool nullByte = false); #endregion diff --git a/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs b/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs index 1a013049..28de58e0 100644 --- a/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs +++ b/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs @@ -12,9 +12,15 @@ namespace Deepgram.Clients.Interfaces.v2; public interface ISpeakWebSocketClient { #region Connect and Disconnect + /// + /// Connects to the Deepgram WebSocket API + /// public Task Connect(SpeakSchema options, CancellationTokenSource? cancelToken = null, Dictionary? addons = null, Dictionary? headers = null); + /// + /// Disconnects from the Deepgram WebSocket API + /// public Task Stop(CancellationTokenSource? cancelToken = null, bool nullByte = false); #endregion diff --git a/Deepgram/Clients/Listen/v2/WebSocket/Client.cs b/Deepgram/Clients/Listen/v2/WebSocket/Client.cs index 05b74948..ad93626b 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/Client.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/Client.cs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT using Deepgram.Abstractions.v2; +using Abstract = Deepgram.Abstractions.v2; using Deepgram.Models.Authenticate.v1; using Deepgram.Models.Listen.v2.WebSocket; using Common = Deepgram.Models.Common.v2.WebSocket; @@ -277,7 +278,8 @@ public async Task Subscribe(EventHandler eventHandler) public async Task SendKeepAlive() { Log.Debug("SendKeepAlive", "Sending KeepAlive Message Immediately..."); - byte[] data = Encoding.ASCII.GetBytes("{\"type\": \"KeepAlive\"}"); + ControlMessage message = new ControlMessage(Constants.KeepAlive); + byte[] data = Encoding.ASCII.GetBytes(message.ToString()); await SendMessageImmediately(data); } @@ -287,10 +289,17 @@ public async Task SendKeepAlive() public async Task SendFinalize() { Log.Debug("SendFinalize", "Sending Finalize Message Immediately..."); - byte[] data = Encoding.ASCII.GetBytes("{\"type\": \"Finalize\"}"); + ControlMessage message = new ControlMessage(Constants.Finalize); + byte[] data = Encoding.ASCII.GetBytes(message.ToString()); await SendMessageImmediately(data); } + /// + /// Sends a binary message over the WebSocket connection. + /// + /// The data to be sent over the WebSocket. + public void Send(byte[] data, int length = Abstract.Constants.UseArrayLengthForSend) => SendBinary(data, length); + /// /// Sends a Close message to Deepgram /// @@ -322,7 +331,8 @@ await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebS return; } - byte[] data = Encoding.ASCII.GetBytes("{\"type\": \"CloseStream\"}"); + ControlMessage message = new ControlMessage(Constants.CloseStream); + byte[] data = Encoding.ASCII.GetBytes(message.ToString()); await SendMessageImmediately(data); } #endregion diff --git a/Deepgram/Clients/Listen/v2/WebSocket/Constants.cs b/Deepgram/Clients/Listen/v2/WebSocket/Constants.cs index 7bddab15..47a52281 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/Constants.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/Constants.cs @@ -11,5 +11,10 @@ public static class Constants { // Default flush period public const int DefaultFlushPeriodInMs = 500; + + // user message types + public const string KeepAlive = "KeepAlive"; + public const string Finalize = "Finalize"; + public const string CloseStream = "CloseStream"; } diff --git a/Deepgram/Clients/Speak/v2/WebSocket/Client.cs b/Deepgram/Clients/Speak/v2/WebSocket/Client.cs index 9569bd6d..db87a663 100644 --- a/Deepgram/Clients/Speak/v2/WebSocket/Client.cs +++ b/Deepgram/Clients/Speak/v2/WebSocket/Client.cs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT using Deepgram.Abstractions.v2; +using Abstract = Deepgram.Abstractions.v2; using Deepgram.Models.Authenticate.v1; using Deepgram.Models.Speak.v2.WebSocket; using Common = Deepgram.Models.Common.v2.WebSocket; @@ -389,10 +390,7 @@ public override void SendMessage(byte[] data, int length = Constants.UseArrayLen /// We need to override the Send function to use the SendMessage function. This is different than STT /// because we only deal in text messages for TTS where STT is sending binary (or audio) messages. /// - public override void Send(byte[] data, int length = Constants.UseArrayLengthForSend) - { - SendMessage(data, length); - } + public void Send(byte[] data, int length = Abstract.Constants.UseArrayLengthForSend) => SendMessage(data, length); #endregion internal async Task ProcessAutoFlush() diff --git a/Deepgram/Models/Listen/v2/WebSocket/ControlMessage.cs b/Deepgram/Models/Listen/v2/WebSocket/ControlMessage.cs new file mode 100644 index 00000000..cddbcaf9 --- /dev/null +++ b/Deepgram/Models/Listen/v2/WebSocket/ControlMessage.cs @@ -0,0 +1,24 @@ +// 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.Listen.v2.WebSocket; + +public class ControlMessage(string text) +{ + /// + /// Gets or sets the type of control message. + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("type")] + public string? Type { get; set; } = text; + + /// + /// Override ToString method to serialize the object + /// + public override string ToString() + { + return Regex.Unescape(JsonSerializer.Serialize(this, JsonSerializeOptions.DefaultOptions)); + } +} + diff --git a/Deepgram/Utilities/QueryParameterUtil.cs b/Deepgram/Utilities/QueryParameterUtil.cs index 48a92539..faca6118 100644 --- a/Deepgram/Utilities/QueryParameterUtil.cs +++ b/Deepgram/Utilities/QueryParameterUtil.cs @@ -46,7 +46,7 @@ public static string FormatURL(string uriSegment, S? parameter, Dictionary /// Encodes the specified parameters into a URL /// - internal static string UrlEncode(T parameters, IEnumerable? propertyInfoList, Dictionary? addons = null) + internal static string UrlEncode(T? parameters, IEnumerable? propertyInfoList, Dictionary? addons = null) { var sb = new StringBuilder(); if (propertyInfoList != null)