Skip to content

Commit

Permalink
Merge pull request #349 from dvonthenen/update-v2-interface
Browse files Browse the repository at this point in the history
Convenience Changes, Removal of Plain `Send()` to Avoid Confusion
  • Loading branch information
davidvonthenen authored Nov 1, 2024
2 parents 4f1f671 + efc15b5 commit 5991268
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 14 deletions.
6 changes: 0 additions & 6 deletions Deepgram/Abstractions/v2/AbstractWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,6 @@ public virtual Task SendClose(bool nullByte = false, CancellationTokenSource? _c
throw new DeepgramException("Unimplemented");
}

/// <summary>
/// Sends a binary message over the WebSocket connection.
/// </summary>
/// <param name="data">The data to be sent over the WebSocket.</param>
public virtual void Send(byte[] data, int length = Constants.UseArrayLengthForSend) => SendBinary(data, length);

/// <summary>
/// This method sends a binary message over the WebSocket connection.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ namespace Deepgram.Clients.Interfaces.v2;
public interface IListenWebSocketClient
{
#region Connect and Disconnect
/// <summary>
/// Connects to the Deepgram WebSocket API
/// </summary>
public Task<bool> Connect(LiveSchema options, CancellationTokenSource? cancelToken = null, Dictionary<string, string>? addons = null,
Dictionary<string, string>? headers = null);

/// <summary>
/// Disconnects from the Deepgram WebSocket API
/// </summary>
public Task<bool> Stop(CancellationTokenSource? cancelToken = null, bool nullByte = false);
#endregion

Expand Down
6 changes: 6 additions & 0 deletions Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ namespace Deepgram.Clients.Interfaces.v2;
public interface ISpeakWebSocketClient
{
#region Connect and Disconnect
/// <summary>
/// Connects to the Deepgram WebSocket API
/// </summary>
public Task<bool> Connect(SpeakSchema options, CancellationTokenSource? cancelToken = null, Dictionary<string, string>? addons = null,
Dictionary<string, string>? headers = null);

/// <summary>
/// Disconnects from the Deepgram WebSocket API
/// </summary>
public Task<bool> Stop(CancellationTokenSource? cancelToken = null, bool nullByte = false);
#endregion

Expand Down
16 changes: 13 additions & 3 deletions Deepgram/Clients/Listen/v2/WebSocket/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,7 +278,8 @@ public async Task<bool> Subscribe(EventHandler<UnhandledResponse> 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);
}

Expand All @@ -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);
}

/// <summary>
/// Sends a binary message over the WebSocket connection.
/// </summary>
/// <param name="data">The data to be sent over the WebSocket.</param>
public void Send(byte[] data, int length = Abstract.Constants.UseArrayLengthForSend) => SendBinary(data, length);

/// <summary>
/// Sends a Close message to Deepgram
/// </summary>
Expand Down Expand Up @@ -322,7 +331,8 @@ await _clientWebSocket.SendAsync(new ArraySegment<byte>(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
Expand Down
5 changes: 5 additions & 0 deletions Deepgram/Clients/Listen/v2/WebSocket/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

6 changes: 2 additions & 4 deletions Deepgram/Clients/Speak/v2/WebSocket/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
/// </summary>
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()
Expand Down
24 changes: 24 additions & 0 deletions Deepgram/Models/Listen/v2/WebSocket/ControlMessage.cs
Original file line number Diff line number Diff line change
@@ -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)
{
/// <summary>
/// Gets or sets the type of control message.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("type")]
public string? Type { get; set; } = text;

/// <summary>
/// Override ToString method to serialize the object
/// </summary>
public override string ToString()
{
return Regex.Unescape(JsonSerializer.Serialize(this, JsonSerializeOptions.DefaultOptions));
}
}

2 changes: 1 addition & 1 deletion Deepgram/Utilities/QueryParameterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static string FormatURL<S>(string uriSegment, S? parameter, Dictionary<st
/// <summary>
/// Encodes the specified parameters into a URL
/// </summary>
internal static string UrlEncode<T>(T parameters, IEnumerable<PropertyInfo>? propertyInfoList, Dictionary<string, string>? addons = null)
internal static string UrlEncode<T>(T? parameters, IEnumerable<PropertyInfo>? propertyInfoList, Dictionary<string, string>? addons = null)
{
var sb = new StringBuilder();
if (propertyInfoList != null)
Expand Down

0 comments on commit 5991268

Please sign in to comment.