Skip to content

Commit

Permalink
Temporary Fix for v3 Timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonthenen committed Feb 5, 2024
1 parent e735bfa commit dda072d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 48 deletions.
45 changes: 44 additions & 1 deletion Deepgram.Tests/UtilitiesTests/HttpClientUtilTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System;
using System.Net.Http;
using Deepgram.Utilities;
using Xunit;

Expand All @@ -21,7 +22,49 @@ public void GetUserAgent_Should_Return_HttpClient_With_Accept_And_UserAgent_Head
Assert.IsAssignableFrom<HttpClient>(result);
Assert.Equal("application/json", result.DefaultRequestHeaders.Accept.ToString());
Assert.Equal(agent, result.DefaultRequestHeaders.UserAgent.ToString());
}

[Fact]
public async Task ExecuteAsync_NotExecutedWithinTimeout_ThrowsExecptionAsync()
{
// Arrange
var endpoint = "https://google.com/";
var timeoutShouldFail = TimeSpan.FromMilliseconds(1);
var timeoutShouldSucceed = TimeSpan.FromMilliseconds(2000);

var httpClientUtil = new HttpClientUtil();
httpClientUtil.SetTimeOut(timeoutShouldFail);

var client = httpClientUtil.HttpClient;
bool resultShouldFail = false;
bool resultShouldSucceed = false;

// Act
try
{
await client.GetAsync(endpoint);
}
catch (TaskCanceledException)
{
resultShouldFail = true;
}

// Act
httpClientUtil.SetTimeOut(timeoutShouldSucceed);
client = httpClientUtil.HttpClient;

try
{
await client.GetAsync(endpoint);
}
catch (TaskCanceledException)
{
resultShouldSucceed = true;
}

// Assert
Assert.True(resultShouldFail);
Assert.False(resultShouldSucceed);
}
}
}
7 changes: 5 additions & 2 deletions Deepgram/Deepgram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
<PackageTags>speech-to-text,captions,speech-recognition,deepgram</PackageTags>
<PackageIcon>dg_logo.png</PackageIcon>
<Product>Deepgram.NET SDK</Product>
<PackageId>Deepgram</PackageId>
<Title>Deepgram.NET</Title>
<PackageId>Deepgram .NET SDK</PackageId>
<Title>Deepgram .NET SDK</Title>
<Version>3.4.1</Version>
<Authors>Deepgram Contributors</Authors>
<Company>Deepgram</Company>
</PropertyGroup>

<ItemGroup>
Expand Down
83 changes: 38 additions & 45 deletions Deepgram/Utilities/HttpClientUtil.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Deepgram.Utilities
{
public class 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 HttpClient Create()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgentUtil.GetUserAgent());

return httpClient;
}

/// <summary>
/// sets timeout on the httpclient
/// </summary>
/// <param name="timeSpan"></param>
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;
}
}
}
using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Deepgram.Utilities
{
public class 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 HttpClient Create()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgentUtil.GetUserAgent());

return httpClient;
}

/// <summary>
/// sets timeout on the httpclient
/// </summary>
/// <param name="timeSpan"></param>
public void SetTimeOut(TimeSpan timeSpan)
{
HttpClient.Timeout = timeSpan;
}
}
}

0 comments on commit dda072d

Please sign in to comment.