feat: implement GetMessageBatchAsync method
This commit is contained in:
@@ -34,7 +34,14 @@ public interface IAnthropicApiClient
|
|||||||
/// <param name="request">The message batch request to create.</param>
|
/// <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>
|
/// <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);
|
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>
|
/// <summary>
|
||||||
/// Counts the tokens in a message asynchronously.
|
/// Counts the tokens in a message asynchronously.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -312,6 +319,23 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
return AnthropicResult<MessageBatchResponse>.Success(msgBatchResponse, anthropicHeaders);
|
return AnthropicResult<MessageBatchResponse>.Success(msgBatchResponse, anthropicHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public async Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId)
|
||||||
|
{
|
||||||
|
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}");
|
||||||
|
var anthropicHeaders = new AnthropicHeaders(response.Headers);
|
||||||
|
var responseContent = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
if (response.IsSuccessStatusCode is false)
|
||||||
|
{
|
||||||
|
var error = Deserialize<AnthropicError>(responseContent) ?? new AnthropicError();
|
||||||
|
return AnthropicResult<MessageBatchResponse>.Failure(error, anthropicHeaders);
|
||||||
|
}
|
||||||
|
|
||||||
|
var msgBatchResponse = Deserialize<MessageBatchResponse>(responseContent) ?? new MessageBatchResponse();
|
||||||
|
return AnthropicResult<MessageBatchResponse>.Success(msgBatchResponse, anthropicHeaders);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request)
|
public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -361,4 +361,25 @@ public class ClientTests(ConfigurationFixture configFixture) : EndToEndTest(conf
|
|||||||
result.Value.Should().BeOfType<MessageBatchResponse>();
|
result.Value.Should().BeOfType<MessageBatchResponse>();
|
||||||
result.Value.Id.Should().NotBeNullOrEmpty();
|
result.Value.Id.Should().NotBeNullOrEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMessageBatchAsync_WhenCalled_ItShouldReturnResponse()
|
||||||
|
{
|
||||||
|
var request = new MessageBatchRequest([
|
||||||
|
new(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
new(
|
||||||
|
model: AnthropicModels.Claude3Haiku,
|
||||||
|
messages: [new(MessageRole.User, [new TextContent("Hello!")])]
|
||||||
|
)
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
var createResult = await _client.CreateMessageBatchAsync(request);
|
||||||
|
var getResult = await _client.GetMessageBatchAsync(createResult.Value.Id);
|
||||||
|
|
||||||
|
getResult.IsSuccess.Should().BeTrue();
|
||||||
|
getResult.Value.Should().BeOfType<MessageBatchResponse>();
|
||||||
|
getResult.Value.Id.Should().Be(createResult.Value.Id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1098,4 +1098,124 @@ public class AnthropicApiClientTests : IntegrationTest
|
|||||||
result.Value.Should().BeOfType<MessageBatchResponse>();
|
result.Value.Should().BeOfType<MessageBatchResponse>();
|
||||||
result.Value.Should().BeEquivalentTo(new MessageBatchResponse());
|
result.Value.Should().BeEquivalentTo(new MessageBatchResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMessageBatchAsync_WhenCalled_ItShouldReturnBatch()
|
||||||
|
{
|
||||||
|
var batchId = "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF";
|
||||||
|
|
||||||
|
_mockHttpMessageHandler
|
||||||
|
.WhenGetMessageBatchRequest(batchId)
|
||||||
|
.Respond(
|
||||||
|
HttpStatusCode.OK,
|
||||||
|
"application/json",
|
||||||
|
@"{
|
||||||
|
""id"": ""msgbatch_013Zva2CMHLNnXjNJJKqJ2EF"",
|
||||||
|
""type"": ""message_batch"",
|
||||||
|
""processing_status"": ""in_progress"",
|
||||||
|
""request_counts"": {
|
||||||
|
""processing"": 100,
|
||||||
|
""succeeded"": 50,
|
||||||
|
""errored"": 30,
|
||||||
|
""canceled"": 10,
|
||||||
|
""expired"": 10
|
||||||
|
},
|
||||||
|
""ended_at"": ""2024-08-20T18:37:24.100435Z"",
|
||||||
|
""created_at"": ""2024-08-20T18:37:24.100435Z"",
|
||||||
|
""expires_at"": ""2024-08-20T18:37:24.100435Z"",
|
||||||
|
""archived_at"": ""2024-08-20T18:37:24.100435Z"",
|
||||||
|
""cancel_initiated_at"": ""2024-08-20T18:37:24.100435Z"",
|
||||||
|
""results_url"": ""https://api.anthropic.com/v1/messages/batches/msgbatch_013Zva2CMHLNnXjNJJKqJ2EF/results""
|
||||||
|
}"
|
||||||
|
);
|
||||||
|
|
||||||
|
var result = await Client.GetMessageBatchAsync(batchId);
|
||||||
|
|
||||||
|
result.IsSuccess.Should().BeTrue();
|
||||||
|
result.Value.Should().BeOfType<MessageBatchResponse>();
|
||||||
|
result.Value.Id.Should().Be("msgbatch_013Zva2CMHLNnXjNJJKqJ2EF");
|
||||||
|
result.Value.Type.Should().Be("message_batch");
|
||||||
|
result.Value.ProcessingStatus.Should().Be("in_progress");
|
||||||
|
result.Value.RequestCounts.Should().BeEquivalentTo(new MessageBatchRequestCounts
|
||||||
|
{
|
||||||
|
Processing = 100,
|
||||||
|
Succeeded = 50,
|
||||||
|
Errored = 30,
|
||||||
|
Canceled = 10,
|
||||||
|
Expired = 10
|
||||||
|
});
|
||||||
|
|
||||||
|
result.Value.EndedAt.Should().Be(DateTimeOffset.Parse("2024-08-20T18:37:24.100435Z"));
|
||||||
|
result.Value.CreatedAt.Should().Be(DateTimeOffset.Parse("2024-08-20T18:37:24.100435Z"));
|
||||||
|
result.Value.ExpiresAt.Should().Be(DateTimeOffset.Parse("2024-08-20T18:37:24.100435Z"));
|
||||||
|
result.Value.ArchivedAt.Should().Be(DateTimeOffset.Parse("2024-08-20T18:37:24.100435Z"));
|
||||||
|
result.Value.CancelInitiatedAt.Should().Be(DateTimeOffset.Parse("2024-08-20T18:37:24.100435Z"));
|
||||||
|
result.Value.ResultsUrl.Should()
|
||||||
|
.Be("https://api.anthropic.com/v1/messages/batches/msgbatch_013Zva2CMHLNnXjNJJKqJ2EF/results");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMessageBatchAsync_WhenCalledAndErrorReturned_ItShouldHandleError()
|
||||||
|
{
|
||||||
|
var batchId = "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF";
|
||||||
|
|
||||||
|
_mockHttpMessageHandler
|
||||||
|
.WhenGetMessageBatchRequest(batchId)
|
||||||
|
.Respond(
|
||||||
|
HttpStatusCode.BadRequest,
|
||||||
|
"application/json",
|
||||||
|
@"{
|
||||||
|
""type"": ""error"",
|
||||||
|
""error"": {
|
||||||
|
""type"": ""invalid_request_error"",
|
||||||
|
""message"": ""messages: roles must alternate between user and assistant, but found multiple user roles in a row""
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
);
|
||||||
|
|
||||||
|
var result = await Client.GetMessageBatchAsync(batchId);
|
||||||
|
|
||||||
|
result.IsSuccess.Should().BeFalse();
|
||||||
|
result.Error.Should().BeOfType<AnthropicError>();
|
||||||
|
result.Error.Error.Should().BeOfType<InvalidRequestError>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMessageBatchAsync_WhenCalledRequestFailsAndErrorCanNotBeDeserialized_ItShouldReturnUnknownError()
|
||||||
|
{
|
||||||
|
var batchId = "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF";
|
||||||
|
|
||||||
|
_mockHttpMessageHandler
|
||||||
|
.WhenGetMessageBatchRequest(batchId)
|
||||||
|
.Respond(
|
||||||
|
HttpStatusCode.BadRequest,
|
||||||
|
"application/json",
|
||||||
|
@"{}"
|
||||||
|
);
|
||||||
|
|
||||||
|
var result = await Client.GetMessageBatchAsync(batchId);
|
||||||
|
|
||||||
|
result.IsSuccess.Should().BeFalse();
|
||||||
|
result.Error.Should().BeOfType<AnthropicError>();
|
||||||
|
result.Error.Error.Should().BeOfType<ApiError>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMessageBatchAsync_WhenCalledAndCanNotDeserializeResponse_ItShouldReturnEmptyResponse()
|
||||||
|
{
|
||||||
|
var batchId = "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF";
|
||||||
|
|
||||||
|
_mockHttpMessageHandler
|
||||||
|
.WhenGetMessageBatchRequest(batchId)
|
||||||
|
.Respond(
|
||||||
|
HttpStatusCode.OK,
|
||||||
|
"application/json",
|
||||||
|
@"{}"
|
||||||
|
);
|
||||||
|
|
||||||
|
var result = await Client.GetMessageBatchAsync(batchId);
|
||||||
|
|
||||||
|
result.IsSuccess.Should().BeTrue();
|
||||||
|
result.Value.Should().BeOfType<MessageBatchResponse>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -71,4 +71,10 @@ public static class MockHttpMessageHandlerExtensions
|
|||||||
return mockHttpMessageHandler
|
return mockHttpMessageHandler
|
||||||
.SetupBaseRequest(HttpMethod.Post, MessageBatchesEndpoint);
|
.SetupBaseRequest(HttpMethod.Post, MessageBatchesEndpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MockedRequest WhenGetMessageBatchRequest(this MockHttpMessageHandler mockHttpMessageHandler, string batchId)
|
||||||
|
{
|
||||||
|
return mockHttpMessageHandler
|
||||||
|
.SetupBaseRequest(HttpMethod.Get, $"{MessageBatchesEndpoint}/{batchId}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user