feat: add DeleteMessageBatchAsync method and MessageBatchDeleteResponse model with tests

This commit is contained in:
Stevan Freeborn
2025-01-14 13:41:32 -06:00
parent 7ec931dda5
commit 56b3a53a2a
6 changed files with 197 additions and 91 deletions
+8 -91
View File
@@ -8,97 +8,6 @@ using AnthropicClient.Utils;
namespace AnthropicClient;
/// <summary>
/// Represents a client for interacting with the Anthropic API.
/// </summary>
public interface IAnthropicApiClient
{
/// <summary>
/// Creates a message asynchronously.
/// </summary>
/// <param name="request">The message request to create.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/>.</returns>
Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request);
/// <summary>
/// Creates a message asynchronously and streams the response.
/// </summary>
/// <param name="request">The message request to create.</param>
/// <returns>An asynchronous enumerable that yields the response event by event.</returns>
IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request);
/// <summary>
/// Creates a batch of messages asynchronously.
/// </summary>
/// <param name="request">The message batch request to create.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request);
/// <summary>
/// Gets a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to get.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId);
/// <summary>
/// Lists the message batches asynchronously.
/// </summary>
/// <param name="request">The paging request to use for listing the message batches.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null);
/// <summary>
/// Lists all message batches asynchronously.
/// </summary>
/// <param name="limit">The maximum number of message batches to return in each page.</param>
/// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20);
/// <summary>
/// Cancels a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to cancel.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId);
/// <summary>
/// Gets the results of a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to get the results for.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="IAsyncEnumerable{T}"/> where T is <see cref="MessageBatchResultItem"/>.</returns>
Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId);
/// <summary>
/// Counts the tokens in a message asynchronously.
/// </summary>
/// <param name="request">The count message tokens request.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="TokenCountResponse"/>.</returns>
Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request);
/// <summary>
/// Lists the models asynchronously.
/// </summary>
/// <param name="request">The paging request to use for listing the models.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null);
/// <summary>
/// Lists the models asynchronously
/// </summary>
/// <param name="limit">The maximum number of models to return in each page.</param>
/// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
///
IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20);
/// <summary>
/// Gets a model by its ID asynchronously.
/// </summary>
/// <param name="modelId">The ID of the model to get.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId);
}
/// <inheritdoc cref="IAnthropicApiClient"/>
public class AnthropicApiClient : IAnthropicApiClient
{
@@ -369,6 +278,14 @@ public class AnthropicApiClient : IAnthropicApiClient
return await CreateResultAsync<MessageBatchResponse>(response);
}
/// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId)
{
var endpoint = $"{MessageBatchesEndpoint}/{batchId}";
var response = await SendRequestAsync(endpoint, HttpMethod.Delete);
return await CreateResultAsync<MessageBatchDeleteResponse>(response);
}
/// <inheritdoc/>
public async Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId)
{
+101
View File
@@ -0,0 +1,101 @@
using AnthropicClient.Models;
namespace AnthropicClient;
/// <summary>
/// Represents a client for interacting with the Anthropic API.
/// </summary>
public interface IAnthropicApiClient
{
/// <summary>
/// Creates a message asynchronously.
/// </summary>
/// <param name="request">The message request to create.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/>.</returns>
Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request);
/// <summary>
/// Creates a message asynchronously and streams the response.
/// </summary>
/// <param name="request">The message request to create.</param>
/// <returns>An asynchronous enumerable that yields the response event by event.</returns>
IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request);
/// <summary>
/// Creates a batch of messages asynchronously.
/// </summary>
/// <param name="request">The message batch request to create.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request);
/// <summary>
/// Gets a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to get.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId);
/// <summary>
/// Lists the message batches asynchronously.
/// </summary>
/// <param name="request">The paging request to use for listing the message batches.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null);
/// <summary>
/// Lists all message batches asynchronously.
/// </summary>
/// <param name="limit">The maximum number of message batches to return in each page.</param>
/// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20);
/// <summary>
/// Cancels a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to cancel.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId);
/// <summary>
/// Deletes a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to delete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchDeleteResponse"/>.</returns>
Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId);
/// <summary>
/// Gets the results of a message batch asynchronously.
/// </summary>
/// <param name="batchId">The ID of the message batch to get the results for.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="IAsyncEnumerable{T}"/> where T is <see cref="MessageBatchResultItem"/>.</returns>
Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId);
/// <summary>
/// Counts the tokens in a message asynchronously.
/// </summary>
/// <param name="request">The count message tokens request.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="TokenCountResponse"/>.</returns>
Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request);
/// <summary>
/// Lists the models asynchronously.
/// </summary>
/// <param name="request">The paging request to use for listing the models.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null);
/// <summary>
/// Lists the models asynchronously
/// </summary>
/// <param name="limit">The maximum number of models to return in each page.</param>
/// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
///
IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20);
/// <summary>
/// Gets a model by its ID asynchronously.
/// </summary>
/// <param name="modelId">The ID of the model to get.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId);
}
@@ -0,0 +1,17 @@
namespace AnthropicClient.Models;
/// <summary>
/// Represents a message batch delete response.
/// </summary>
public class MessageBatchDeleteResponse
{
/// <summary>
/// Gets the ID of the message batch that was deleted.
/// </summary>
public string Id { get; init; } = string.Empty;
/// <summary>
/// Gets the type of the message batch response.
/// </summary>
public string Type { get; init; } = string.Empty;
}
@@ -1904,4 +1904,28 @@ public class AnthropicApiClientTests : IntegrationTest
result.Error.Should().BeOfType<AnthropicError>();
result.Error.Error.Should().BeOfType<InvalidRequestError>();
}
[Fact]
public async Task DeleteMessageBatchAsync_WhenCalled_ItShouldReturnDeletionResponse()
{
var batchId = "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF";
_mockHttpMessageHandler
.WhenDeleteMessageBatchRequest(batchId)
.Respond(
HttpStatusCode.OK,
"application/json",
@"{
""id"": ""msgbatch_013Zva2CMHLNnXjNJJKqJ2EF"",
""type"": ""message_batch_deleted""
}"
);
var result = await Client.DeleteMessageBatchAsync(batchId);
result.IsSuccess.Should().BeTrue();
result.Value.Should().BeOfType<MessageBatchDeleteResponse>();
result.Value.Id.Should().Be(batchId);
result.Value.Type.Should().Be("message_batch_deleted");
}
}
@@ -97,4 +97,10 @@ public static class MockHttpMessageHandlerExtensions
return mockHttpMessageHandler
.SetupBaseRequest(HttpMethod.Post, $"{MessageBatchesEndpoint}/{batchId}/cancel");
}
public static MockedRequest WhenDeleteMessageBatchRequest(this MockHttpMessageHandler mockHttpMessageHandler, string batchId)
{
return mockHttpMessageHandler
.SetupBaseRequest(HttpMethod.Delete, $"{MessageBatchesEndpoint}/{batchId}");
}
}
@@ -0,0 +1,41 @@
namespace AnthropicClient.Tests.Unit.Models;
public class MessageBatchDeleteResponseTests : SerializationTest
{
private const string SampleJson = @"{
""id"": ""test-id"",
""type"": ""test-type""
}";
[Fact]
public void Constructor_WhenCalled_ItShouldReturnAnInstanceWithPropertiesSet()
{
var result = new MessageBatchDeleteResponse();
result.Id.Should().BeEmpty();
result.Type.Should().BeEmpty();
}
[Fact]
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
{
var result = new MessageBatchDeleteResponse
{
Id = "test-id",
Type = "test-type"
};
var json = Serialize(result);
JsonAssert.Equal(SampleJson, json);
}
[Fact]
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedValues()
{
var result = Deserialize<MessageBatchDeleteResponse>(SampleJson);
result!.Id.Should().Be("test-id");
result.Type.Should().Be("test-type");
}
}