refactor: remove unnecessary if checks and add test to handle getting null file
This commit is contained in:
@@ -385,14 +385,6 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
public async Task<AnthropicResult<AnthropicFile>> CreateFileAsync(CreateFileRequest request, CancellationToken cancellationToken = default)
|
public async Task<AnthropicResult<AnthropicFile>> CreateFileAsync(CreateFileRequest request, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var response = await SendFileRequestAsync(FilesEndpoint, request, cancellationToken);
|
var response = await SendFileRequestAsync(FilesEndpoint, request, cancellationToken);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode is false)
|
|
||||||
{
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
|
||||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
|
||||||
return AnthropicResult<AnthropicFile>.Failure(error, new AnthropicHeaders(response.Headers));
|
|
||||||
}
|
|
||||||
|
|
||||||
return await CreateResultAsync<AnthropicFile>(response);
|
return await CreateResultAsync<AnthropicFile>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,14 +394,6 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
var pagingRequest = request ?? new PagingRequest();
|
var pagingRequest = request ?? new PagingRequest();
|
||||||
var endpoint = $"{FilesEndpoint}?{pagingRequest.ToQueryParameters()}";
|
var endpoint = $"{FilesEndpoint}?{pagingRequest.ToQueryParameters()}";
|
||||||
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode is false)
|
|
||||||
{
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
|
||||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
|
||||||
return AnthropicResult<Page<AnthropicFile>>.Failure(error, new AnthropicHeaders(response.Headers));
|
|
||||||
}
|
|
||||||
|
|
||||||
return await CreateResultAsync<Page<AnthropicFile>>(response);
|
return await CreateResultAsync<Page<AnthropicFile>>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -427,14 +411,6 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
{
|
{
|
||||||
var endpoint = $"{FilesEndpoint}/{fileId}";
|
var endpoint = $"{FilesEndpoint}/{fileId}";
|
||||||
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode is false)
|
|
||||||
{
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
|
||||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
|
||||||
return AnthropicResult<AnthropicFile>.Failure(error, new AnthropicHeaders(response.Headers));
|
|
||||||
}
|
|
||||||
|
|
||||||
return await CreateResultAsync<AnthropicFile>(response);
|
return await CreateResultAsync<AnthropicFile>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,14 +436,6 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
{
|
{
|
||||||
var endpoint = $"{FilesEndpoint}/{fileId}";
|
var endpoint = $"{FilesEndpoint}/{fileId}";
|
||||||
var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken);
|
var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode is false)
|
|
||||||
{
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
|
||||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
|
||||||
return AnthropicResult<AnthropicFileDeleteResponse>.Failure(error, new AnthropicHeaders(response.Headers));
|
|
||||||
}
|
|
||||||
|
|
||||||
return await CreateResultAsync<AnthropicFileDeleteResponse>(response);
|
return await CreateResultAsync<AnthropicFileDeleteResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2331,6 +2331,26 @@ public class AnthropicApiClientTests : IntegrationTest
|
|||||||
result.Error.Error.Should().BeOfType<InvalidRequestError>();
|
result.Error.Error.Should().BeOfType<InvalidRequestError>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[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<AnthropicError>();
|
||||||
|
result.Error.Error.Should().BeOfType<ApiError>();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task DeleteFileAsync_WhenCalled_ItShouldReturnDeletionResponse()
|
public async Task DeleteFileAsync_WhenCalled_ItShouldReturnDeletionResponse()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user