Skip to content

Commit

Permalink
Merge pull request #233 from dvonthenen/client-improvements
Browse files Browse the repository at this point in the history
Refactor and Simplification of RestClient
  • Loading branch information
davidvonthenen authored Feb 16, 2024
2 parents 065e887 + c32574a commit 41575c4
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 280 deletions.
1 change: 0 additions & 1 deletion Deepgram.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
global using Bogus;
global using Deepgram.Abstractions;
global using Deepgram.Constants;
global using Deepgram.Extensions;
global using Deepgram.Tests.Fakes;
global using Deepgram.Utilities;
global using FluentAssertions;
Expand Down
34 changes: 14 additions & 20 deletions Deepgram.Tests/UnitTests/ClientTests/AbstractRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void GetAsync_Should_Throws_HttpRequestException_On_UnsuccessfulResponse(
var expectedResponse = new AutoFaker<ProjectsResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);
// Act & Assert
client.Invoking(y => y.GetAsync<ProjectsResponse>(UriSegments.PROJECTS))
.Should().ThrowAsync<HttpRequestException>();
Expand All @@ -42,7 +42,7 @@ public void GetAsync_Should_Throws_Exception_On_UnsuccessfulResponse()
var expectedResponse = new AutoFaker<ProjectsResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);
// Act & Assert
client.Invoking(y => y.GetAsync<ProjectsResponse>(UriSegments.PROJECTS))
.Should().ThrowAsync<Exception>();
Expand All @@ -57,7 +57,7 @@ public void PostAsync_Which_Handles_HttpContent_Should_Throw_Exception_On_Unsucc
var httpContent = Substitute.For<HttpContent>();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PostAsync<SyncResponse>(UriSegments.PROJECTS, httpContent))
Expand All @@ -72,10 +72,7 @@ public void PostAsync_Which_Handles_HttpContent_Should_Throw_HttpRequestExceptio
var response = new AutoFaker<SyncResponse>().Generate();
var httpContent = Substitute.For<HttpContent>();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());
var client = new ConcreteRestClient(_apiKey, _clientOptions)
{
_httpClientWrapper = new HttpClientWrapper(httpClient)
};
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PostAsync<SyncResponse>(UriSegments.PROJECTS, httpContent))
Expand All @@ -90,7 +87,7 @@ public void PostAsync_Should_Throw_Exception_On_UnsuccessfulResponse()
var response = new AutoFaker<SyncResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y =>
Expand All @@ -105,7 +102,7 @@ public void PostAsync_Should_Throw_HttpRequestException_On_UnsuccessfulResponse(
var response = new AutoFaker<SyncResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y =>
Expand All @@ -121,7 +118,7 @@ public async Task Delete_Should_Throws_HttpRequestException_On_Response_Containi
// Arrange
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
await client.Invoking(async y => await y.DeleteAsync(UriSegments.PROJECTS))
Expand All @@ -135,7 +132,7 @@ public async Task Delete_Should_Throws_Exception_On_Response_Containing_Error()
// Arrange
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
await client.Invoking(async y => await y.DeleteAsync(UriSegments.PROJECTS))
Expand All @@ -150,7 +147,7 @@ public void DeleteAsync_TResponse_Should_Throws_HttpRequestException_On_Unsucces
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.DeleteAsync<MessageResponse>(UriSegments.PROJECTS))
Expand All @@ -164,7 +161,7 @@ public void DeleteAsync_TResponse_Should_Throws_Exception_On_UnsuccessfulRespons
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.DeleteAsync<MessageResponse>(UriSegments.PROJECTS))
Expand All @@ -178,10 +175,7 @@ public void PatchAsync_TResponse_Should_Throws_HttpRequestException_On_Unsuccess
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

var client = new ConcreteRestClient(_apiKey, _clientOptions)
{
_httpClientWrapper = new HttpClientWrapper(httpClient)
};
var client = new ConcreteRestClient(_apiKey, _clientOptions);

//Act & Assert
client.Invoking(y => y.PatchAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
Expand All @@ -195,7 +189,7 @@ public void PatchAsync_TResponse_Should_Throws_Exception_On_UnsuccessfulResponse
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

//Act & Assert
client.Invoking(y => y.PatchAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
Expand All @@ -209,7 +203,7 @@ public void PutAsync_TResponse_Should_Throws_HttpRequestException_On_Unsuccessfu
// Arrange
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PutAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
Expand All @@ -222,7 +216,7 @@ public void PutAsync_TResponse_Should_Throws_Exception_On_UnsuccessfulResponse()
// Arrange
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

var client = new ConcreteRestClient(_apiKey, _clientOptions) { _httpClientWrapper = new HttpClientWrapper(httpClient) };
var client = new ConcreteRestClient(_apiKey, _clientOptions);
// Act & Assert
client.Invoking(y => y.PutAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<Exception>();
Expand Down
27 changes: 13 additions & 14 deletions Deepgram.Tests/UnitTests/ClientTests/AnalyzeClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public async Task AnalyzeUrl_Should_Call_PostAsync_Returning_SyncResponse()
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
analyzeClient.PostAsync<SyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<StringContent>()).Returns(expectedResponse);
Expand Down Expand Up @@ -62,7 +61,7 @@ public async Task AnalyzeUrl_Should_Throw_ArgumentException_If_CallBack_Not_Null
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
analyzeClient.PostAsync<SyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<StringContent>()).Returns(expectedResponse);

Expand All @@ -85,7 +84,7 @@ public async Task AnalyzeUrlCallBack_Should_Call_PostAsync_Returning_SyncRespons
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);
var callBackParameter = analyzeSchema.CallBack;
Expand Down Expand Up @@ -117,7 +116,7 @@ public async Task AnalyzeUrlCallBack_Should_Call_PostAsync_Returning_SyncRespons
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -145,7 +144,7 @@ public async Task AnalyzeUrlCallBack_Should_Throw_ArgumentException_With_CallBac

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);
var callBackParameter = analyzeSchema.CallBack;
Expand All @@ -169,7 +168,7 @@ public async Task AnalyzeUrlCallBack_Should_Throw_ArgumentException_With_No_Call

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<StringContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedQuery}", Arg.Any<StringContent>()).Returns(expectedResponse);

Expand All @@ -193,7 +192,7 @@ public async Task AnalyzeFile_With_Stream_Should_Call_PostAsync_Returning_SyncRe
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<SyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -222,7 +221,7 @@ public async Task AnalyzeFile_With_Bytes_Should_Call_PostAsync_Returning_SyncRes
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<SyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<SyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -250,7 +249,7 @@ public async Task AnalyzeFileCallBack_With_Stream_With_CallBack_Property_Should_
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -278,7 +277,7 @@ public async Task AnalyzeFileCallBack_With_Bytes_With_CallBack_Property_Should_C
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -308,7 +307,7 @@ public async Task AnalyzeFileCallBack_With_Stream_With_CallBack_Parameter_Should
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -342,7 +341,7 @@ public async Task AnalyzeFileCallBack_With_Bytes_With_CallBack_Parameter_Should_

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);

Expand Down Expand Up @@ -375,7 +374,7 @@ public async Task AnalyzeFileCallBack_With_Stream_Throw_ArgumentException_With_C
var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);

var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
var callBack = analyzeSchema.CallBack;
Expand All @@ -402,7 +401,7 @@ public async Task AnalyzeFileCallBack_With_Bytes_Should_Throw_ArgumentException_

var httpClient = MockHttpClient.CreateHttpClientWithResult(expectedResponse);
var analyzeClient = Substitute.For<AnalyzeClient>(_apiKey, _options);
analyzeClient._httpClientWrapper = new HttpClientWrapper(httpClient);

analyzeClient.When(x => x.PostAsync<AsyncResponse>(Arg.Any<string>(), Arg.Any<HttpContent>())).DoNotCallBase();
analyzeClient.PostAsync<AsyncResponse>($"{UriSegments.ANALYZE}?{stringedOptions}", Arg.Any<HttpContent>()).Returns(expectedResponse);
analyzeSchema.CallBack = null;
Expand Down
20 changes: 0 additions & 20 deletions Deepgram.Tests/UnitTests/ClientTests/LiveClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,6 @@ public void GetBaseUrl_Should_Return_WSS_Protocol_When_DeepgramClientOptions_Bas

}

[Test]
public void GetBaseUrl_Should_Return_WSS_Protocol_When_BaseAddress_Contains_WS_Protocol()
{
//Arrange
var expectedUrl = $"wss://{Defaults.DEFAULT_URI}";
_options.BaseAddress = $"ws://{Defaults.DEFAULT_URI}";

//Act
var result = LiveClient.GetBaseUrl(_options);


//Assert
using (new AssertionScope())
{
result.Should().NotBeNullOrEmpty();
result.Should().StartWith("wss://");
result.Should().BeEquivalentTo(expectedUrl);
}
}

[Test]
public void GetBaseUrl_Should_Return_WSS_Protocol_When_BaseAddress_Contains_WSS_Protocol()
{
Expand Down
Loading

0 comments on commit 41575c4

Please sign in to comment.