From 7ec931dda53678899713cc92fad0b068ae622313 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Tue, 14 Jan 2025 13:14:14 -0600 Subject: [PATCH] refactor: add private method to dry class up a bit --- src/AnthropicClient/AnthropicApiClient.cs | 99 +++++------------------ 1 file changed, 22 insertions(+), 77 deletions(-) diff --git a/src/AnthropicClient/AnthropicApiClient.cs b/src/AnthropicClient/AnthropicApiClient.cs index 34244c3..13ceafe 100644 --- a/src/AnthropicClient/AnthropicApiClient.cs +++ b/src/AnthropicClient/AnthropicApiClient.cs @@ -333,34 +333,14 @@ public class AnthropicApiClient : IAnthropicApiClient public async Task> CreateMessageBatchAsync(MessageBatchRequest request) { var response = await SendRequestAsync(MessageBatchesEndpoint, request); - var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(); - - if (response.IsSuccessStatusCode is false) - { - var error = Deserialize(responseContent) ?? new AnthropicError(); - return AnthropicResult.Failure(error, anthropicHeaders); - } - - var msgBatchResponse = Deserialize(responseContent) ?? new MessageBatchResponse(); - return AnthropicResult.Success(msgBatchResponse, anthropicHeaders); + return await CreateResultAsync(response); } /// public async Task> 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(responseContent) ?? new AnthropicError(); - return AnthropicResult.Failure(error, anthropicHeaders); - } - - var msgBatchResponse = Deserialize(responseContent) ?? new MessageBatchResponse(); - return AnthropicResult.Success(msgBatchResponse, anthropicHeaders); + return await CreateResultAsync(response); } /// @@ -369,17 +349,7 @@ public class AnthropicApiClient : IAnthropicApiClient var pagingRequest = request ?? new PagingRequest(); var endpoint = $"{MessageBatchesEndpoint}?{pagingRequest.ToQueryParameters()}"; var response = await SendRequestAsync(endpoint); - var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(); - - if (response.IsSuccessStatusCode is false) - { - var error = Deserialize(responseContent) ?? new AnthropicError(); - return AnthropicResult>.Failure(error, anthropicHeaders); - } - - var page = Deserialize>(responseContent) ?? new Page(); - return AnthropicResult>.Success(page, anthropicHeaders); + return await CreateResultAsync>(response); } /// @@ -396,17 +366,7 @@ public class AnthropicApiClient : IAnthropicApiClient { var endpoint = $"{MessageBatchesEndpoint}/{batchId}/cancel"; var response = await SendRequestAsync(endpoint, HttpMethod.Post); - var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(); - - if (response.IsSuccessStatusCode is false) - { - var error = Deserialize(responseContent) ?? new AnthropicError(); - return AnthropicResult.Failure(error, anthropicHeaders); - } - - var msgBatchResponse = Deserialize(responseContent) ?? new MessageBatchResponse(); - return AnthropicResult.Success(msgBatchResponse, anthropicHeaders); + return await CreateResultAsync(response); } /// @@ -445,17 +405,7 @@ public class AnthropicApiClient : IAnthropicApiClient public async Task> CountMessageTokensAsync(CountMessageTokensRequest request) { var response = await SendRequestAsync(CountTokensEndpoint, request); - var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(); - - if (response.IsSuccessStatusCode is false) - { - var error = Deserialize(responseContent) ?? new AnthropicError(); - return AnthropicResult.Failure(error, anthropicHeaders); - } - - var msgResponse = Deserialize(responseContent) ?? new TokenCountResponse(); - return AnthropicResult.Success(msgResponse, anthropicHeaders); + return await CreateResultAsync(response); } /// @@ -464,17 +414,7 @@ public class AnthropicApiClient : IAnthropicApiClient var pagingRequest = request ?? new PagingRequest(); var endpoint = $"{ModelsEndpoint}?{pagingRequest.ToQueryParameters()}"; var response = await SendRequestAsync(endpoint); - var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(); - - if (response.IsSuccessStatusCode is false) - { - var error = Deserialize(responseContent) ?? new AnthropicError(); - return AnthropicResult>.Failure(error, anthropicHeaders); - } - - var page = Deserialize>(responseContent) ?? new Page(); - return AnthropicResult>.Success(page, anthropicHeaders); + return await CreateResultAsync>(response); } /// @@ -491,17 +431,7 @@ public class AnthropicApiClient : IAnthropicApiClient { var endpoint = $"{ModelsEndpoint}/{modelId}"; var response = await SendRequestAsync(endpoint); - var anthropicHeaders = new AnthropicHeaders(response.Headers); - var responseContent = await response.Content.ReadAsStringAsync(); - - if (response.IsSuccessStatusCode is false) - { - var error = Deserialize(responseContent) ?? new AnthropicError(); - return AnthropicResult.Failure(error, anthropicHeaders); - } - - var model = Deserialize(responseContent) ?? new AnthropicModel(); - return AnthropicResult.Success(model, anthropicHeaders); + return await CreateResultAsync(response); } private async IAsyncEnumerable>> GetAllPagesAsync(string endpoint, int limit = 20) @@ -558,6 +488,21 @@ public class AnthropicApiClient : IAnthropicApiClient return new ToolCall(tool, toolUse); } + private async Task> CreateResultAsync(HttpResponseMessage response) where T : new() + { + var anthropicHeaders = new AnthropicHeaders(response.Headers); + var responseContent = await response.Content.ReadAsStringAsync(); + + if (response.IsSuccessStatusCode is false) + { + var error = Deserialize(responseContent) ?? new AnthropicError(); + return AnthropicResult.Failure(error, anthropicHeaders); + } + + var model = Deserialize(responseContent) ?? new T(); + return AnthropicResult.Success(model, anthropicHeaders); + } + private async Task SendRequestAsync(string endpoint, HttpMethod? method = null) { var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpoint);