Skip to content

Commit

Permalink
Reorganize Factory, Add CancelToken Option to Send Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonthenen committed Nov 1, 2024
1 parent ba0f717 commit 8fa5030
Show file tree
Hide file tree
Showing 32 changed files with 176 additions and 278 deletions.
2 changes: 1 addition & 1 deletion Deepgram/Abstractions/v2/AbstractRestClient.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 16 additions & 10 deletions Deepgram/Abstractions/v2/AbstractWebSocketClient.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -245,7 +245,7 @@ public async Task<bool> Subscribe(EventHandler<ErrorResponse> eventHandler)
/// <summary>
/// Sends a Close message to Deepgram
/// </summary>
public virtual Task SendClose(bool nullByte = false)
public virtual Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null)
{
throw new DeepgramException("Unimplemented");
}
Expand All @@ -271,24 +271,27 @@ public virtual void SendMessage(byte[] data, int length = Constants.UseArrayLeng

/// <summary>
/// This method sends a binary message over the WebSocket connection immediately without queueing.
/// </summary>
public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend)
/// </summary>,
public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null)
{
if (!IsConnected())
{
Log.Debug("SendBinaryImmediately", "WebSocket is not connected. Exiting...");
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...");
if (length == Constants.UseArrayLengthForSend)
{
length = data.Length;
}
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token)
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Binary, true, _cancelToken.Token)
.ConfigureAwait(false);
}
finally
Expand All @@ -300,23 +303,26 @@ await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSoc
/// <summary>
/// This method sends a text message over the WebSocket connection immediately without queueing.
/// </summary>
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())
{
Log.Debug("SendBinaryImmediately", "WebSocket is not connected. Exiting...");
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...");
if (length == Constants.UseArrayLengthForSend)
{
length = data.Length;
}
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Text, true, _cancellationTokenSource.Token)
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Text, true, _cancelToken.Token)
.ConfigureAwait(false);
}
finally
Expand Down Expand Up @@ -613,7 +619,7 @@ public async Task<bool> 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
Expand Down
2 changes: 1 addition & 1 deletion Deepgram/Abstractions/v2/Utilities.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion Deepgram/Abstractions/v2/WebSocketMessage.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
216 changes: 116 additions & 100 deletions Deepgram/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ public static V1.IAnalyzeClient CreateAnalyzeClient(string apiKey = "", Deepgram
return new AnalyzeClient(apiKey, options, httpId);
}

/// <summary>
/// This method allows you to create an AnalyzeClient with a specific version of the client.
/// </summary>
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);
}

/// <summary>
// *********** WARNING ***********
// This function creates a LiveClient for the Deepgram API
Expand Down Expand Up @@ -63,25 +54,6 @@ public static V2.IListenWebSocketClient CreateListenWebSocketClient(string apiKe
return new ListenWebSocketClient(apiKey, options);
}

/// <summary>
/// This method allows you to create an AnalyzeClient with a specific version of the client.
/// </summary>
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));
}
}

/// <summary>
/// Create a new ManageClient
/// </summary>
Expand All @@ -94,31 +66,6 @@ public static V1.IManageClient CreateManageClient(string apiKey = "", DeepgramHt
return new ManageClient(apiKey, options, httpId);
}

/// <summary>
/// This method allows you to create an ManageClient with a specific version of the client.
/// </summary>
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);
}

/// <summary>
// *********** 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 ***********
/// </summary>
[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);
}

/// <summary>
/// Create a new SelfHostedClient
/// </summary>
Expand All @@ -132,40 +79,58 @@ public static V1.ISelfHostedClient CreateSelfHostedClient(string apiKey = "", De
}

/// <summary>
/// This method allows you to create an SelfHostedClient with a specific version of the client.
/// Create a new ListenRESTClient
/// </summary>
public static object CreateSelfHostedClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null)
/// <param name="apiKey"></param>
/// <param name="options"></param>
/// <param name="httpId"></param>
/// <returns></returns>
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);
}

/// <summary>
// *********** 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
/// </summary>
[Obsolete("Please use CreateListenRESTClient instead", false)]
public static V1.IPreRecordedClient CreatePreRecordedClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null)
/// <param name="apiKey"></param>
/// <param name="options"></param>
/// <param name="httpId"></param>
/// <returns></returns>
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);
}

/// <summary>
/// Create a new ListenRESTClient
/// Create a new AnalyzeClient
/// </summary>
/// <param name="apiKey"></param>
/// <param name="options"></param>
/// <param name="httpId"></param>
/// <returns></returns>
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.
///

/// <summary>
/// This method allows you to create an AnalyzeClient with a specific version of the client.
/// </summary>
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);
}

/// <summary>
Expand All @@ -178,51 +143,49 @@ public static object CreateListenRESTClient(int version, string apiKey = "", Dee
}

/// <summary>
// *********** 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.
/// </summary>
[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);

Check warning on line 156 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

'Client' is obsolete: 'Please use Deepgram.Clients.Listen.v2.WebSocket instead'

Check warning on line 156 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

'Client' is obsolete: 'Please use Deepgram.Clients.Listen.v2.WebSocket instead'

Check warning on line 156 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

'Client' is obsolete: 'Please use Deepgram.Clients.Listen.v2.WebSocket instead'

Check warning on line 156 in Deepgram/ClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

'Client' is obsolete: 'Please use Deepgram.Clients.Listen.v2.WebSocket instead'
case 2:
return new ListenV2.Client(apiKey, options);
default:
throw new ArgumentException("Invalid version", nameof(version));
}
}

/// <summary>
/// Create a new SpeakRESTClient
/// This method allows you to create an ManageClient with a specific version of the client.
/// </summary>
/// <param name="apiKey"></param>
/// <param name="options"></param>
/// <param name="httpId"></param>
/// <returns></returns>
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);
}

/// <summary>
/// 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.
/// </summary>
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);
}

/// <summary>
/// Create a new AnalyzeClient
/// This method allows you to create an SpeakRESTClient with a specific version of the client.
/// </summary>
/// <param name="apiKey"></param>
/// <param name="options"></param>
/// <returns></returns>
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);
}

/// <summary>
Expand All @@ -243,4 +206,57 @@ 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.
///

/// <summary>
// *********** 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 ***********
/// </summary>
[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);
}

/// <summary>
// *********** 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 ***********
/// </summary>
[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);
}

/// <summary>
// *********** 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 ***********
/// </summary>
[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);
}
}
Loading

0 comments on commit 8fa5030

Please sign in to comment.