diff --git a/src/AnthropicClient/AnthropicApiClient.cs b/src/AnthropicClient/AnthropicApiClient.cs index 5ea4e02..58f0944 100644 --- a/src/AnthropicClient/AnthropicApiClient.cs +++ b/src/AnthropicClient/AnthropicApiClient.cs @@ -51,12 +51,13 @@ public class AnthropicApiClient : IAnthropicApiClient _httpClient.DefaultRequestHeaders.Add(pair.Key, pair.Value); } } + /// public async Task> CreateMessageAsync(MessageRequest request, CancellationToken cancellationToken = default) { var response = await SendRequestAsync(MessagesEndpoint, request, cancellationToken); var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(cancellationToken); + var responseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode is false) { @@ -81,7 +82,8 @@ public class AnthropicApiClient : IAnthropicApiClient if (response.IsSuccessStatusCode is false) { - var error = Deserialize(await response.Content.ReadAsStringAsync(cancellationToken)) ?? new AnthropicError(); + var errorContent = await response.Content.ReadAsStringAsync(); + var error = Deserialize(errorContent) ?? new AnthropicError(); yield return new AnthropicEvent(EventType.Error, new ErrorEventData(error.Error)); yield break; } @@ -237,26 +239,30 @@ public class AnthropicApiClient : IAnthropicApiClient } } while (true); } + /// public async Task> CreateMessageBatchAsync(MessageBatchRequest request, CancellationToken cancellationToken = default) { var response = await SendRequestAsync(MessageBatchesEndpoint, request, cancellationToken); - return await CreateResultAsync(response, cancellationToken); + return await CreateResultAsync(response); } + /// public async Task> GetMessageBatchAsync(string batchId, CancellationToken cancellationToken = default) { var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}", cancellationToken: cancellationToken); - return await CreateResultAsync(response, cancellationToken); + return await CreateResultAsync(response); } + /// public async Task>> ListMessageBatchesAsync(PagingRequest? request = null, CancellationToken cancellationToken = default) { var pagingRequest = request ?? new PagingRequest(); var endpoint = $"{MessageBatchesEndpoint}?{pagingRequest.ToQueryParameters()}"; var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken); - return await CreateResultAsync>(response, cancellationToken); + return await CreateResultAsync>(response); } + /// public async IAsyncEnumerable>> ListAllMessageBatchesAsync(int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default) { @@ -265,20 +271,23 @@ public class AnthropicApiClient : IAnthropicApiClient yield return result; } } + /// public async Task> CancelMessageBatchAsync(string batchId, CancellationToken cancellationToken = default) { var endpoint = $"{MessageBatchesEndpoint}/{batchId}/cancel"; var response = await SendRequestAsync(endpoint, HttpMethod.Post, cancellationToken); - return await CreateResultAsync(response, cancellationToken); + return await CreateResultAsync(response); } + /// public async Task> DeleteMessageBatchAsync(string batchId, CancellationToken cancellationToken = default) { var endpoint = $"{MessageBatchesEndpoint}/{batchId}"; var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken); - return await CreateResultAsync(response, cancellationToken); + return await CreateResultAsync(response); } + /// public async Task>> GetMessageBatchResultsAsync(string batchId, CancellationToken cancellationToken = default) { @@ -287,7 +296,7 @@ public class AnthropicApiClient : IAnthropicApiClient if (response.IsSuccessStatusCode is false) { - var content = await response.Content.ReadAsStringAsync(cancellationToken); + var content = await response.Content.ReadAsStringAsync(); var error = Deserialize(content) ?? new AnthropicError(); return AnthropicResult>.Failure(error, anthropicHeaders); } @@ -296,7 +305,7 @@ public class AnthropicApiClient : IAnthropicApiClient async IAsyncEnumerable ReadResultsAsync([EnumeratorCancellation] CancellationToken ct = default) { - using var responseContent = await response.Content.ReadAsStreamAsync(ct); + using var responseContent = await response.Content.ReadAsStreamAsync(); using var streamReader = new StreamReader(responseContent); var line = await streamReader.ReadLineAsync(); @@ -310,20 +319,23 @@ public class AnthropicApiClient : IAnthropicApiClient } } } + /// public async Task> CountMessageTokensAsync(CountMessageTokensRequest request, CancellationToken cancellationToken = default) { var response = await SendRequestAsync(CountTokensEndpoint, request, cancellationToken); - return await CreateResultAsync(response, cancellationToken); + return await CreateResultAsync(response); } + /// public async Task>> ListModelsAsync(PagingRequest? request = null, CancellationToken cancellationToken = default) { var pagingRequest = request ?? new PagingRequest(); var endpoint = $"{ModelsEndpoint}?{pagingRequest.ToQueryParameters()}"; var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken); - return await CreateResultAsync>(response, cancellationToken); + return await CreateResultAsync>(response); } + /// public async IAsyncEnumerable>> ListAllModelsAsync(int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default) { @@ -332,13 +344,15 @@ public class AnthropicApiClient : IAnthropicApiClient yield return result; } } + /// public async Task> GetModelAsync(string modelId, CancellationToken cancellationToken = default) { var endpoint = $"{ModelsEndpoint}/{modelId}"; var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken); - return await CreateResultAsync(response, cancellationToken); + return await CreateResultAsync(response); } + private async IAsyncEnumerable>> GetAllPagesAsync(string endpoint, int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default) { var pagingRequest = new PagingRequest(limit: limit); @@ -349,7 +363,7 @@ public class AnthropicApiClient : IAnthropicApiClient { var response = await SendRequestAsync(Endpoint(), cancellationToken: cancellationToken); var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(cancellationToken); + var responseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode is false) { @@ -392,10 +406,11 @@ public class AnthropicApiClient : IAnthropicApiClient return new ToolCall(tool, toolUse); } - private async Task> CreateResultAsync(HttpResponseMessage response, CancellationToken cancellationToken = default) where T : new() + + private async Task> CreateResultAsync(HttpResponseMessage response) where T : new() { var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(cancellationToken); + var responseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode is false) { @@ -406,6 +421,7 @@ public class AnthropicApiClient : IAnthropicApiClient var model = Deserialize(responseContent) ?? new T(); return AnthropicResult.Success(model, anthropicHeaders); } + private async Task SendRequestAsync(string endpoint, HttpMethod? method = null, CancellationToken cancellationToken = default) { var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpoint);