Skip to content

Commit

Permalink
Merge pull request #237 from dvonthenen/refactor-live-client
Browse files Browse the repository at this point in the history
Refactor and Simplify Live Client
  • Loading branch information
davidvonthenen authored Feb 21, 2024
2 parents 1968db0 + e5166db commit 47fe63e
Show file tree
Hide file tree
Showing 44 changed files with 1,190 additions and 923 deletions.
43 changes: 43 additions & 0 deletions Deepgram.Dev.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31912.275
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deepgram", "Deepgram\Deepgram.csproj", "{B8B59E33-2D73-4A27-8FC6-E464CB28357D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deepgram.Tests", "Deepgram.Tests\Deepgram.Tests.csproj", "{12C80273-08DD-494C-B06D-DFC6D40B1D95}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Streaming", "examples\streaming\Streaming.csproj", "{5DD81E37-D575-476B-9D1C-803A093A4E3C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prerecorded", "examples\prerecorded\Prerecorded.csproj", "{70B63CBA-1130-46D1-A022-6CD31C37C60E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B8B59E33-2D73-4A27-8FC6-E464CB28357D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8B59E33-2D73-4A27-8FC6-E464CB28357D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8B59E33-2D73-4A27-8FC6-E464CB28357D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8B59E33-2D73-4A27-8FC6-E464CB28357D}.Release|Any CPU.Build.0 = Release|Any CPU
{12C80273-08DD-494C-B06D-DFC6D40B1D95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12C80273-08DD-494C-B06D-DFC6D40B1D95}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12C80273-08DD-494C-B06D-DFC6D40B1D95}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12C80273-08DD-494C-B06D-DFC6D40B1D95}.Release|Any CPU.Build.0 = Release|Any CPU
{5DD81E37-D575-476B-9D1C-803A093A4E3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DD81E37-D575-476B-9D1C-803A093A4E3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DD81E37-D575-476B-9D1C-803A093A4E3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DD81E37-D575-476B-9D1C-803A093A4E3C}.Release|Any CPU.Build.0 = Release|Any CPU
{70B63CBA-1130-46D1-A022-6CD31C37C60E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70B63CBA-1130-46D1-A022-6CD31C37C60E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70B63CBA-1130-46D1-A022-6CD31C37C60E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70B63CBA-1130-46D1-A022-6CD31C37C60E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D4ABC6D-7126-4EE2-9303-43A954616B2A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Encapsulations;
using Deepgram.Models.Authenticate.v1;
using Deepgram.Models.Manage.v1;
using Deepgram.Models.PreRecorded.v1;
using Deepgram.Clients.Manage.v1;

namespace Deepgram.Tests.UnitTests.ClientTests;

public class AbstractRestfulClientTests
{


DeepgramClientOptions _clientOptions;
string _apiKey;

Expand Down
58 changes: 29 additions & 29 deletions Deepgram.Tests/UnitTests/ClientTests/AnalyzeClientTests.cs

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions Deepgram.Tests/UnitTests/ClientTests/LiveClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.DeepgramEventArgs;
using System.Net.WebSockets;

using Deepgram.Models.Authenticate.v1;
using Deepgram.Models.Live.v1;
using System.Net.WebSockets;
using Deepgram.Clients.Live.v1;

namespace Deepgram.Tests.UnitTests.ClientTests;

Expand Down Expand Up @@ -35,10 +36,10 @@ public void ProcessDataReceived_Should_Raise_TranscriptReceived_Event_When_Respo
//Arrange
var liveTranscriptionResponse = new AutoFaker<TranscriptionResponse>().Generate();
// ensure the right type is set for testing
liveTranscriptionResponse.Type = Enums.LiveType.Results;
liveTranscriptionResponse.Type = LiveType.Results;
var json = JsonSerializer.Serialize(liveTranscriptionResponse);
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json));
ResponseReceivedEventArgs? eventArgs = null;
ResponseEventArgs? eventArgs = null;

_liveClient.EventResponseReceived += (sender, args) => eventArgs = args;

Expand All @@ -59,11 +60,11 @@ public void ProcessDataReceived_Should_Raise_MetaDataReceived_Event_When_Respons
//Arrange
var liveMetadataResponse = new AutoFaker<MetadataResponse>().Generate();
// ensure the right type is set for testing
liveMetadataResponse.Type = Enums.LiveType.Metadata;
liveMetadataResponse.Type = LiveType.Metadata;
var json = JsonSerializer.Serialize(liveMetadataResponse);
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json));

ResponseReceivedEventArgs? eventArgs = null;
ResponseEventArgs? eventArgs = null;

_liveClient.EventResponseReceived += (sender, args) => eventArgs = args;

Expand All @@ -87,7 +88,7 @@ public void ProcessDataReceived_Should_Raise_LiveError_Event_When_Response_Conta
var json = JsonSerializer.Serialize(unknownDataResponse);
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json));

ResponseReceivedEventArgs? eventArgs = null;
ResponseEventArgs? eventArgs = null;

_liveClient.EventResponseReceived += (sender, args) => eventArgs = args;

Expand Down
2 changes: 1 addition & 1 deletion Deepgram.Tests/UnitTests/ClientTests/ManageClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Encapsulations;
using Deepgram.Models.Authenticate.v1;
using Deepgram.Models.Manage.v1;
using Deepgram.Clients.Manage.v1;

namespace Deepgram.Tests.UnitTests.ClientTests;

Expand Down
3 changes: 1 addition & 2 deletions Deepgram.Tests/UnitTests/ClientTests/OnPremClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Encapsulations;
using Deepgram.Models.Authenticate.v1;
using Deepgram.Models.Manage.v1;
using Deepgram.Models.OnPrem.v1;
using Deepgram.Clients.OnPrem.v1;

namespace Deepgram.Tests.UnitTests.ClientTests;

Expand Down
32 changes: 16 additions & 16 deletions Deepgram.Tests/UnitTests/ClientTests/PrerecordedClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Encapsulations;
using Deepgram.Models.Authenticate.v1;
using Deepgram.Models.PreRecorded.v1;
using Deepgram.Clients.PreRecorded.v1;

namespace Deepgram.Tests.UnitTests.ClientTests;

public class PrerecordedClientTests
public class PreRecordedClientTests
{
DeepgramClientOptions _options;
string _apiKey;
Expand All @@ -31,7 +31,7 @@ public async Task TranscribeUrl_Should_Call_PostAsync_Returning_SyncResponse()
var source = new AutoFaker<UrlSource>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);


prerecordedClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
Expand Down Expand Up @@ -61,7 +61,7 @@ public async Task TranscribeUrl_Should_Throw_ArgumentException_If_CallBack_Not_N
var source = new AutoFaker<UrlSource>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
prerecordedClient.PostAsync<SyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<StringContent>()).Returns(expectedResponse);
Expand All @@ -84,7 +84,7 @@ public async Task TranscribeUrlCallBack_Should_Call_PostAsync_Returning_SyncResp
var stringedQuery = QueryParameterUtil.GetParameters(prerecordedSchema);
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -116,7 +116,7 @@ public async Task TranscribeUrlCallBack_Should_Call_PostAsync_Returning_SyncResp
var stringedQuery = QueryParameterUtil.GetParameters(prerecordedSchema);
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -144,7 +144,7 @@ public async Task TranscribeUrlCallBack_Should_Throw_ArgumentException_With_Call
var stringedQuery = QueryParameterUtil.GetParameters(prerecordedSchema);

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);
Expand All @@ -168,7 +168,7 @@ public async Task TranscribeUrlCallBack_Should_Throw_ArgumentException_With_No_C
var stringedQuery = QueryParameterUtil.GetParameters(prerecordedSchema);

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);
Expand All @@ -192,7 +192,7 @@ public async Task TranscribeFile_With_Stream_Should_Call_PostAsync_Returning_Syn
var source = GetFakeStream(GetFakeByteArray());
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<SyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -221,7 +221,7 @@ public async Task TranscribeFile_With_Bytes_Should_Call_PostAsync_Returning_Sync
var source = GetFakeByteArray();
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<SyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -249,7 +249,7 @@ public async Task TranscribeFileCallBack_With_Stream_With_CallBack_Property_Shou
var source = GetFakeStream(GetFakeByteArray());
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -277,7 +277,7 @@ public async Task TranscribeFileCallBack_With_Bytes_With_CallBack_Property_Shoul
var source = GetFakeByteArray();
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task TranscribeFileCallBack_With_Stream_With_CallBack_Parameter_Sho
var source = GetFakeStream(GetFakeByteArray());
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -341,7 +341,7 @@ public async Task TranscribeFileCallBack_With_Bytes_With_CallBack_Parameter_Shou
var source = GetFakeByteArray();

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -374,7 +374,7 @@ public async Task TranscribeFileCallBack_With_Stream_Throw_ArgumentException_Wit
var source = GetFakeStream(GetFakeByteArray());
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand All @@ -401,7 +401,7 @@ public async Task TranscribeFileCallBack_With_Bytes_Should_Throw_ArgumentExcepti
var source = GetFakeByteArray();

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var prerecordedClient = Substitute.For<PrerecordedClient>(_apiKey, _options);
var prerecordedClient = Substitute.For<PreRecordedClient>(_apiKey, _options);

prerecordedClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
prerecordedClient.PostAsync<AsyncResponse>($"{UriSegments.LISTEN}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Should_Return_HttpClient_With_Default_BaseAddress()
{
//Arrange
var httpClient = MockHttpClient.CreateHttpClientWithResult(new MessageResponse(), HttpStatusCode.OK);
_httpClientFactory.CreateClient(Defaults.HTTPCLIENT_NAME).Returns(httpClient);
_httpClientFactory.CreateClient().Returns(httpClient);

//Act
var SUT = HttpClientFactory.ConfigureDeepgram(httpClient, _apiKey, _clientOptions);
Expand All @@ -50,7 +50,7 @@ public void Should_Return_HttpClient_With_Custom_BaseAddress()
var customBaseAddress = $"https://{_customUrl}";
_clientOptions.BaseAddress = customBaseAddress;
var httpClient = MockHttpClient.CreateHttpClientWithResult(new MessageResponse(), HttpStatusCode.OK, expectedBaseAddress);
_httpClientFactory.CreateClient(Defaults.HTTPCLIENT_NAME).Returns(httpClient);
_httpClientFactory.CreateClient().Returns(httpClient);

//Act
var SUT = HttpClientFactory.ConfigureDeepgram(httpClient, _apiKey, _clientOptions);
Expand All @@ -69,7 +69,7 @@ public void Should_Return_HttpClient_With_Default_BaseAddress_And_Custom_Headers
//Arrange
_clientOptions.Headers = FakeHeaders();
var httpClient = MockHttpClient.CreateHttpClientWithResult(new MessageResponse(), HttpStatusCode.OK);
_httpClientFactory.CreateClient(Defaults.HTTPCLIENT_NAME).Returns(httpClient);
_httpClientFactory.CreateClient().Returns(httpClient);

//Act
var SUT = HttpClientFactory.ConfigureDeepgram(httpClient, _apiKey, _clientOptions);
Expand All @@ -91,7 +91,7 @@ public void Should_Return_HttpClient_With_Custom_BaseAddress_And_Custom_Headers(

var httpClient = MockHttpClient.CreateHttpClientWithResult(new MessageResponse(), HttpStatusCode.OK);
httpClient.BaseAddress = null;
_httpClientFactory.CreateClient(Defaults.HTTPCLIENT_NAME).Returns(httpClient);
_httpClientFactory.CreateClient().Returns(httpClient);

var expectedBaseAddress = $"https://{_customUrl}/v1/";
var customBaseAddress = $"https://{_customUrl}";
Expand Down Expand Up @@ -120,7 +120,7 @@ public void Should_Return_HttpClient_With_Predefined_values()
_clientOptions.BaseAddress = customBaseAddress;
var httpClient = MockHttpClient.CreateHttpClientWithResult(new MessageResponse(), HttpStatusCode.OK, expectedBaseAddress);

_httpClientFactory.CreateClient(Defaults.HTTPCLIENT_NAME).Returns(httpClient);
_httpClientFactory.CreateClient().Returns(httpClient);

//Act
var SUT = HttpClientFactory.ConfigureDeepgram(httpClient, _apiKey, _clientOptions);
Expand Down
Loading

0 comments on commit 47fe63e

Please sign in to comment.