Skip to content

Commit

Permalink
fix: crash on multiple set time out calls (#118)
Browse files Browse the repository at this point in the history
* Recreate the HttpClient if we're setting a new timeout for http calls in the Deepgram base client.

* Modifies base behaviour to not use a static HttpClientUtil but instead pass an instance around as required.
  • Loading branch information
ThindalTV authored Aug 23, 2023
1 parent f653e3b commit 4024f82
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Deepgram.Tests/ClientTests/DeepgramClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public void Should_Initialize_LiveTranscriptionClient()
[InlineData(30)]
public void Should_Set_TimeOut_On_HttpClient(double timeSpan)
{

//Arrange
var SUT = new DeepgramClient(new CredentialsFaker().Generate());
var httpClientUtil = new HttpClientUtil();
var SUT = new DeepgramClient(new CredentialsFaker().Generate(), httpClientUtil);

//Act
SUT.SetHttpClientTimeout(TimeSpan.FromSeconds(timeSpan));
var httpClient = HttpClientUtil.HttpClient;
var httpClient = httpClientUtil.HttpClient;

//Assert
Assert.NotNull(httpClient);
Expand Down
6 changes: 3 additions & 3 deletions Deepgram.Tests/UtilitiesTests/HttpClientUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class HttpClientUtilTests
[Fact]
public void GetUserAgent_Should_Return_HttpClient_With_Accept_And_UserAgent_Headers_Set()
{
//Arrange
//Arrange
var httpClientUtil = new HttpClientUtil();
var agent = UserAgentUtil.GetUserAgent();

//Act
var result = HttpClientUtil.HttpClient;

var result = httpClientUtil.HttpClient;

//Assert
Assert.NotNull(result);
Expand Down
6 changes: 5 additions & 1 deletion Deepgram/Clients/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public abstract class BaseClient : IBaseClient
public IApiRequest ApiRequest { get; set; }
public IRequestMessageBuilder RequestMessageBuilder { get; set; }

public BaseClient(Credentials credentials)
protected HttpClientUtil HttpClientUtil { get; }

public BaseClient(Credentials credentials, HttpClientUtil httpClientUtil)
{
HttpClientUtil = httpClientUtil;

Credentials = credentials;
ApiRequest = new ApiRequest(HttpClientUtil.HttpClient);
RequestMessageBuilder = new RequestMessageBuilder();
Expand Down
4 changes: 3 additions & 1 deletion Deepgram/Clients/BillingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using System.Threading.Tasks;
using Deepgram.Models;
using Deepgram.Request;
using Deepgram.Utilities;

namespace Deepgram.Clients
{
public class BillingClient : BaseClient
{
public BillingClient(Credentials credentials) : base(credentials) { }
public BillingClient(Credentials credentials, HttpClientUtil httpClientUtil)
: base(credentials, httpClientUtil) { }

/// <summary>
/// Generates a list of outstanding balances for the specified project. To see balances, the authenticated account must be a project owner or administrator
Expand Down
8 changes: 4 additions & 4 deletions Deepgram/Clients/KeyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using System.Threading.Tasks;
using Deepgram.Interfaces;
using Deepgram.Models;
using Deepgram.Utilities;

namespace Deepgram.Clients
{
public class KeyClient : BaseClient, IKeyClient
{
public KeyClient(Credentials credentials) : base(credentials)
{
}

public KeyClient(Credentials credentials, HttpClientUtil httpClientUtil)
: base(credentials, httpClientUtil) { }

/// <summary>
/// Returns all Deepgram API keys associated with the project provided
Expand Down
5 changes: 4 additions & 1 deletion Deepgram/Clients/PrerecordedTranscriptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
using System.Threading.Tasks;
using Deepgram.Interfaces;
using Deepgram.Models;
using Deepgram.Utilities;

namespace Deepgram.Clients
{
public class PrerecordedTranscriptionClient : BaseClient, IPrerecordedTranscriptionClient
{
public PrerecordedTranscriptionClient(Credentials credentials) : base(credentials) { }
public PrerecordedTranscriptionClient(Credentials credentials, HttpClientUtil httpClientUtil)
: base(credentials, httpClientUtil) { }

/// <inheritdoc />
public async Task<PrerecordedTranscription> GetTranscriptionAsync(UrlSource source, PrerecordedTranscriptionOptions options)
{
Expand Down
5 changes: 4 additions & 1 deletion Deepgram/Clients/ProjectClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
using System.Threading.Tasks;
using Deepgram.Interfaces;
using Deepgram.Models;
using Deepgram.Utilities;

namespace Deepgram.Clients
{
public class ProjectClient : BaseClient, IProjectClient
{
public ProjectClient(Credentials credentials) : base(credentials) { }
public ProjectClient(Credentials credentials, HttpClientUtil httpClientUtil)
: base(credentials, httpClientUtil) { }

/// <summary>
/// Returns all Deepgram projects
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions Deepgram/Clients/TranscriptionClient.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Deepgram.Interfaces;
using Deepgram.Models;
using Deepgram.Utilities;

namespace Deepgram.Clients
{
public class TranscriptionClient : ITranscriptionClient
{
public IPrerecordedTranscriptionClient Prerecorded { get; protected set; }

public TranscriptionClient(Credentials credentials)
public TranscriptionClient(Credentials credentials, HttpClientUtil httpClientUtil)
{
Prerecorded = new PrerecordedTranscriptionClient(credentials);
Prerecorded = new PrerecordedTranscriptionClient(credentials, httpClientUtil);
}


Expand Down
4 changes: 3 additions & 1 deletion Deepgram/Clients/UsageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using System.Threading.Tasks;
using Deepgram.Interfaces;
using Deepgram.Models;
using Deepgram.Utilities;

namespace Deepgram.Clients
{
public class UsageClient : BaseClient, IUsageClient
{

public UsageClient(Credentials credentials) : base(credentials) { }
public UsageClient(Credentials credentials, HttpClientUtil httpClientUtil)
: base(credentials, httpClientUtil) { }

/// <summary>
/// Generates a list of requests sent to the Deepgram API for the specified project over a given time range.
Expand Down
22 changes: 15 additions & 7 deletions Deepgram/DeepgramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Deepgram
{
public class DeepgramClient
public class DeepgramClient : BaseClient
{
private Credentials Credentials;

Expand All @@ -17,7 +17,16 @@ public class DeepgramClient
public ILiveTranscriptionClient CreateLiveTranscriptionClient() => new LiveTranscriptionClient(Credentials);
public DeepgramClient() : this(null) { }

public DeepgramClient(Credentials credentials)
public DeepgramClient(Credentials credentials) : this(credentials, new HttpClientUtil()) { }

/// <summary>
/// Only directly called by unit tests,
/// but indirectly called by the other constructors.
/// </summary>
/// <param name="credentials">Credentials to pass for access to the deepgram services</param>
/// <param name="httpClientUtil">The instance to get the HttpClient from</param>
internal DeepgramClient(Credentials credentials, HttpClientUtil httpClientUtil)
: base(credentials, httpClientUtil)
{
Initialize(credentials);
}
Expand All @@ -37,11 +46,10 @@ private void Initialize(Credentials credentials)

protected void InitializeClients()
{

Keys = new KeyClient(Credentials);
Projects = new ProjectClient(Credentials);
Transcription = new TranscriptionClient(Credentials);
Usage = new UsageClient(Credentials);
Keys = new KeyClient(Credentials, HttpClientUtil);
Projects = new ProjectClient(Credentials, HttpClientUtil);
Transcription = new TranscriptionClient(Credentials, HttpClientUtil);
Usage = new UsageClient(Credentials, HttpClientUtil);
}
}
}
24 changes: 17 additions & 7 deletions Deepgram/Utilities/HttpClientUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ namespace Deepgram.Utilities
{
public class HttpClientUtil
{
// Global client used in all instance when needed
internal static HttpClient HttpClient = Create();
static HttpClientUtil() { }
// Client used in instance when needed
internal HttpClient HttpClient { get; private set; }

internal HttpClientUtil() {
HttpClient = Create();
}

/// <summary>
/// Create a Httpclient set common headers
/// </summary>
/// <returns></returns>
private static HttpClient Create()
private HttpClient Create()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -27,9 +30,16 @@ private static HttpClient Create()
/// sets timeout on the httpclient
/// </summary>
/// <param name="timeSpan"></param>
public static void SetTimeOut(TimeSpan timeSpan) =>
HttpClient.Timeout = timeSpan;

public void SetTimeOut(TimeSpan timeSpan)
{
// If the timeout has a new value, create a new HttpClient
if (HttpClient.Timeout != timeSpan)
{
HttpClient = Create();
}

// Set the timeout
HttpClient.Timeout = timeSpan;
}
}
}

0 comments on commit 4024f82

Please sign in to comment.