Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-v3] Temporary Fix for v3 Timeouts #207

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 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,62 @@ 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_ThrowsExceptionAsync()
{
// Arrange
var endpoint = "https://google.com/";
var timeoutShouldFail = TimeSpan.FromMilliseconds(1);

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

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

// Act
try
{
await client.GetAsync(endpoint);
}
catch (TaskCanceledException)
{
// we are expecting this to timeout
resultShouldFail = true;
}

// Assert
Assert.True(resultShouldFail);
}

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

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

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

// Act
try
{
await client.GetAsync(endpoint);
}
catch (TaskCanceledException)
{
// we should not see the exception thrown
resultShouldSucceed = true;
}

// Assert
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;
}
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading