From ea4c8230bc471cf3fff872edb9ab8d763a8c4ff4 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 14 Jul 2025 19:17:35 -0500 Subject: [PATCH] refactor: remove unnecessary if checks and add test to handle getting null file --- src/AnthropicClient/AnthropicApiClient.cs | 32 ------------------- .../Integration/AnthropicApiClientTests.cs | 20 ++++++++++++ 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/AnthropicClient/AnthropicApiClient.cs b/src/AnthropicClient/AnthropicApiClient.cs index 1870fbd..e872c38 100644 --- a/src/AnthropicClient/AnthropicApiClient.cs +++ b/src/AnthropicClient/AnthropicApiClient.cs @@ -385,14 +385,6 @@ public class AnthropicApiClient : IAnthropicApiClient public async Task> CreateFileAsync(CreateFileRequest request, CancellationToken cancellationToken = default) { var response = await SendFileRequestAsync(FilesEndpoint, request, cancellationToken); - - if (response.IsSuccessStatusCode is false) - { - var content = await response.Content.ReadAsStringAsync(); - var error = Deserialize(content) ?? new AnthropicError(); - return AnthropicResult.Failure(error, new AnthropicHeaders(response.Headers)); - } - return await CreateResultAsync(response); } @@ -402,14 +394,6 @@ public class AnthropicApiClient : IAnthropicApiClient var pagingRequest = request ?? new PagingRequest(); var endpoint = $"{FilesEndpoint}?{pagingRequest.ToQueryParameters()}"; var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken); - - if (response.IsSuccessStatusCode is false) - { - var content = await response.Content.ReadAsStringAsync(); - var error = Deserialize(content) ?? new AnthropicError(); - return AnthropicResult>.Failure(error, new AnthropicHeaders(response.Headers)); - } - return await CreateResultAsync>(response); } @@ -427,14 +411,6 @@ public class AnthropicApiClient : IAnthropicApiClient { var endpoint = $"{FilesEndpoint}/{fileId}"; var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken); - - if (response.IsSuccessStatusCode is false) - { - var content = await response.Content.ReadAsStringAsync(); - var error = Deserialize(content) ?? new AnthropicError(); - return AnthropicResult.Failure(error, new AnthropicHeaders(response.Headers)); - } - return await CreateResultAsync(response); } @@ -460,14 +436,6 @@ public class AnthropicApiClient : IAnthropicApiClient { var endpoint = $"{FilesEndpoint}/{fileId}"; var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken); - - if (response.IsSuccessStatusCode is false) - { - var content = await response.Content.ReadAsStringAsync(); - var error = Deserialize(content) ?? new AnthropicError(); - return AnthropicResult.Failure(error, new AnthropicHeaders(response.Headers)); - } - return await CreateResultAsync(response); } diff --git a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs index 168f552..c0ee144 100644 --- a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs +++ b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs @@ -2331,6 +2331,26 @@ public class AnthropicApiClientTests : IntegrationTest result.Error.Error.Should().BeOfType(); } + [Fact] + public async Task GetFileAsync_WhenCalledAndCanNotDeserializeResponse_ItShouldReturnError() + { + var fileId = "file_013Zva2CMHLNnXjNJJKqJ2EF"; + + _mockHttpMessageHandler + .WhenGetFileContentRequest(fileId) + .Respond( + HttpStatusCode.BadRequest, + "application/json", + @"null" + ); + + var result = await Client.GetFileAsync(fileId); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().BeOfType(); + result.Error.Error.Should().BeOfType(); + } + [Fact] public async Task DeleteFileAsync_WhenCalled_ItShouldReturnDeletionResponse() {