Merge pull request #34 from StevanFreeborn/stevanfreeborn/fix/add-cancellation-token-support

fix: add cancellation token support
This commit is contained in:
Stevan Freeborn
2025-05-19 15:33:09 -05:00
committed by GitHub
7 changed files with 95 additions and 71 deletions
+6 -6
View File
@@ -13,10 +13,10 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
token: ${{ secrets.ACTIONS_PAT }} token: ${{ secrets.ACTIONS_PAT }}
- name: Setup .NET 8 - name: Setup .NET 9
uses: actions/setup-dotnet@v4 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 8.x dotnet-version: 9.x
- name: Install versionize - name: Install versionize
run: dotnet tool install --global Versionize run: dotnet tool install --global Versionize
- name: Setup git - name: Setup git
@@ -53,10 +53,10 @@ jobs:
fetch-depth: 0 fetch-depth: 0
ref: ${{ github.ref }} ref: ${{ github.ref }}
token: ${{ secrets.ACTIONS_PAT }} token: ${{ secrets.ACTIONS_PAT }}
- name: Setup .NET 8 - name: Setup .NET 9
uses: actions/setup-dotnet@v4 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 8.x dotnet-version: 9.x
- name: Get project version - name: Get project version
uses: kzrnm/get-net-sdk-project-versions-action@v1 uses: kzrnm/get-net-sdk-project-versions-action@v1
id: get-version id: get-version
@@ -88,10 +88,10 @@ jobs:
fetch-depth: 0 fetch-depth: 0
ref: ${{ github.ref }} ref: ${{ github.ref }}
token: ${{ secrets.ACTIONS_PAT }} token: ${{ secrets.ACTIONS_PAT }}
- name: Setup .NET 8 - name: Setup .NET 9
uses: actions/setup-dotnet@v4 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 8.x dotnet-version: 9.x
- name: Install Docfx - name: Install Docfx
run: dotnet tool install --global docfx run: dotnet tool install --global docfx
- name: Get project version - name: Get project version
+4 -4
View File
@@ -15,10 +15,10 @@ jobs:
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup .NET 8 - name: Setup .NET 9
uses: actions/setup-dotnet@v4 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 8.x dotnet-version: 9.x
- name: Restore dependencies - name: Restore dependencies
run: dotnet restore run: dotnet restore
- name: Format code - name: Format code
@@ -28,10 +28,10 @@ jobs:
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup .NET 8 - name: Setup .NET 9
uses: actions/setup-dotnet@v4 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 8.x dotnet-version: 9.x
- name: Install report generator - name: Install report generator
run: dotnet tool install --global dotnet-reportgenerator-globaltool --version 5.3.7 run: dotnet tool install --global dotnet-reportgenerator-globaltool --version 5.3.7
- name: Restore dependencies - name: Restore dependencies
+9
View File
@@ -3,9 +3,18 @@
"dotnet.defaultSolution": "AnthropicClient.sln", "dotnet.defaultSolution": "AnthropicClient.sln",
"cSpell.words": [ "cSpell.words": [
"Browsable", "Browsable",
"buildtransitive",
"contentfiles",
"Docfx",
"globaltool",
"haikus", "haikus",
"Linq",
"msbuild",
"nameof", "nameof",
"reportgenerator",
"reporttypes",
"Szalay", "Szalay",
"targetdir",
"typeof" "typeof"
], ],
"dotnet.unitTests.runSettingsPath": "./tests/AnthropicClient.Tests/.runsettings" "dotnet.unitTests.runSettingsPath": "./tests/AnthropicClient.Tests/.runsettings"
+35 -33
View File
@@ -1,4 +1,5 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
@@ -52,9 +53,9 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request) public async Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(MessagesEndpoint, request); var response = await SendRequestAsync(MessagesEndpoint, request, cancellationToken);
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync();
@@ -75,13 +76,14 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request) public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(MessagesEndpoint, request); var response = await SendRequestAsync(MessagesEndpoint, request, cancellationToken);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
{ {
var error = Deserialize<AnthropicError>(await response.Content.ReadAsStringAsync()) ?? new AnthropicError(); var errorContent = await response.Content.ReadAsStringAsync();
var error = Deserialize<AnthropicError>(errorContent) ?? new AnthropicError();
yield return new AnthropicEvent(EventType.Error, new ErrorEventData(error.Error)); yield return new AnthropicEvent(EventType.Error, new ErrorEventData(error.Error));
yield break; yield break;
} }
@@ -239,57 +241,57 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request) public async Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(MessageBatchesEndpoint, request); var response = await SendRequestAsync(MessageBatchesEndpoint, request, cancellationToken);
return await CreateResultAsync<MessageBatchResponse>(response); return await CreateResultAsync<MessageBatchResponse>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId) public async Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}"); var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}", cancellationToken: cancellationToken);
return await CreateResultAsync<MessageBatchResponse>(response); return await CreateResultAsync<MessageBatchResponse>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null) public async Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null, CancellationToken cancellationToken = default)
{ {
var pagingRequest = request ?? new PagingRequest(); var pagingRequest = request ?? new PagingRequest();
var endpoint = $"{MessageBatchesEndpoint}?{pagingRequest.ToQueryParameters()}"; var endpoint = $"{MessageBatchesEndpoint}?{pagingRequest.ToQueryParameters()}";
var response = await SendRequestAsync(endpoint); var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
return await CreateResultAsync<Page<MessageBatchResponse>>(response); return await CreateResultAsync<Page<MessageBatchResponse>>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20) public async IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
await foreach (var result in GetAllPagesAsync<MessageBatchResponse>(MessageBatchesEndpoint, limit)) await foreach (var result in GetAllPagesAsync<MessageBatchResponse>(MessageBatchesEndpoint, limit, cancellationToken))
{ {
yield return result; yield return result;
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId) public async Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{MessageBatchesEndpoint}/{batchId}/cancel"; var endpoint = $"{MessageBatchesEndpoint}/{batchId}/cancel";
var response = await SendRequestAsync(endpoint, HttpMethod.Post); var response = await SendRequestAsync(endpoint, HttpMethod.Post, cancellationToken);
return await CreateResultAsync<MessageBatchResponse>(response); return await CreateResultAsync<MessageBatchResponse>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId) public async Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{MessageBatchesEndpoint}/{batchId}"; var endpoint = $"{MessageBatchesEndpoint}/{batchId}";
var response = await SendRequestAsync(endpoint, HttpMethod.Delete); var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken);
return await CreateResultAsync<MessageBatchDeleteResponse>(response); return await CreateResultAsync<MessageBatchDeleteResponse>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId) public async Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}/results"); var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}/results", cancellationToken: cancellationToken);
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
@@ -319,39 +321,39 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request) public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(CountTokensEndpoint, request); var response = await SendRequestAsync(CountTokensEndpoint, request, cancellationToken);
return await CreateResultAsync<TokenCountResponse>(response); return await CreateResultAsync<TokenCountResponse>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null) public async Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null, CancellationToken cancellationToken = default)
{ {
var pagingRequest = request ?? new PagingRequest(); var pagingRequest = request ?? new PagingRequest();
var endpoint = $"{ModelsEndpoint}?{pagingRequest.ToQueryParameters()}"; var endpoint = $"{ModelsEndpoint}?{pagingRequest.ToQueryParameters()}";
var response = await SendRequestAsync(endpoint); var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
return await CreateResultAsync<Page<AnthropicModel>>(response); return await CreateResultAsync<Page<AnthropicModel>>(response);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20) public async IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
await foreach (var result in GetAllPagesAsync<AnthropicModel>(ModelsEndpoint, limit)) await foreach (var result in GetAllPagesAsync<AnthropicModel>(ModelsEndpoint, limit, cancellationToken))
{ {
yield return result; yield return result;
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId) public async Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{ModelsEndpoint}/{modelId}"; var endpoint = $"{ModelsEndpoint}/{modelId}";
var response = await SendRequestAsync(endpoint); var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
return await CreateResultAsync<AnthropicModel>(response); return await CreateResultAsync<AnthropicModel>(response);
} }
private async IAsyncEnumerable<AnthropicResult<Page<T>>> GetAllPagesAsync<T>(string endpoint, int limit = 20) private async IAsyncEnumerable<AnthropicResult<Page<T>>> GetAllPagesAsync<T>(string endpoint, int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
var pagingRequest = new PagingRequest(limit: limit); var pagingRequest = new PagingRequest(limit: limit);
string Endpoint() => $"{endpoint}?{pagingRequest.ToQueryParameters()}"; string Endpoint() => $"{endpoint}?{pagingRequest.ToQueryParameters()}";
@@ -359,7 +361,7 @@ public class AnthropicApiClient : IAnthropicApiClient
do do
{ {
var response = await SendRequestAsync(Endpoint()); var response = await SendRequestAsync(Endpoint(), cancellationToken: cancellationToken);
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync();
@@ -420,17 +422,17 @@ public class AnthropicApiClient : IAnthropicApiClient
return AnthropicResult<T>.Success(model, anthropicHeaders); return AnthropicResult<T>.Success(model, anthropicHeaders);
} }
private async Task<HttpResponseMessage> SendRequestAsync(string endpoint, HttpMethod? method = null) private async Task<HttpResponseMessage> SendRequestAsync(string endpoint, HttpMethod? method = null, CancellationToken cancellationToken = default)
{ {
var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpoint); var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpoint);
return await _httpClient.SendAsync(request); return await _httpClient.SendAsync(request, cancellationToken);
} }
private async Task<HttpResponseMessage> SendRequestAsync<T>(string endpoint, T request) private async Task<HttpResponseMessage> SendRequestAsync<T>(string endpoint, T request, CancellationToken cancellationToken = default)
{ {
var requestJson = Serialize(request); var requestJson = Serialize(request);
var requestContent = new StringContent(requestJson, Encoding.UTF8, JsonContentType); var requestContent = new StringContent(requestJson, Encoding.UTF8, JsonContentType);
return await _httpClient.PostAsync(endpoint, requestContent); return await _httpClient.PostAsync(endpoint, requestContent, cancellationToken);
} }
private string Serialize<T>(T obj) => JsonSerializer.Serialize(obj, JsonSerializationOptions.DefaultOptions); private string Serialize<T>(T obj) => JsonSerializer.Serialize(obj, JsonSerializationOptions.DefaultOptions);
+2 -2
View File
@@ -34,8 +34,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.5" />
<PackageReference Include="System.Text.Json" Version="8.0.5" /> <PackageReference Include="System.Text.Json" Version="9.0.5" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
+28 -15
View File
@@ -11,91 +11,104 @@ public interface IAnthropicApiClient
/// Creates a message asynchronously. /// Creates a message asynchronously.
/// </summary> /// </summary>
/// <param name="request">The message request to create.</param> /// <param name="request">The message request to create.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/>.</returns> /// <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); Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Creates a message asynchronously and streams the response. /// Creates a message asynchronously and streams the response.
/// </summary> /// </summary>
/// <param name="request">The message request to create.</param> /// <param name="request">The message request to create.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>An asynchronous enumerable that yields the response event by event.</returns> /// <returns>An asynchronous enumerable that yields the response event by event.</returns>
IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request); IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Creates a batch of messages asynchronously. /// Creates a batch of messages asynchronously.
/// </summary> /// </summary>
/// <param name="request">The message batch request to create.</param> /// <param name="request">The message batch request to create.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Gets a message batch asynchronously. /// Gets a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to get.</param> /// <param name="batchId">The ID of the message batch to get.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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>> GetMessageBatchAsync(string batchId); Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists the message batches asynchronously. /// Lists the message batches asynchronously.
/// </summary> /// </summary>
/// <param name="request">The paging request to use for listing the message batches.</param> /// <param name="request">The paging request to use for listing the message batches.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists all message batches asynchronously. /// Lists all message batches asynchronously.
/// </summary> /// </summary>
/// <param name="limit">The maximum number of message batches to return in each page.</param> /// <param name="limit">The maximum number of message batches to return in each page.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Cancels a message batch asynchronously. /// Cancels a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to cancel.</param> /// <param name="batchId">The ID of the message batch to cancel.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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>> CancelMessageBatchAsync(string batchId); Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Deletes a message batch asynchronously. /// Deletes a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to delete.</param> /// <param name="batchId">The ID of the message batch to delete.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Gets the results of a message batch asynchronously. /// Gets the results of a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to get the results for.</param> /// <param name="batchId">The ID of the message batch to get the results for.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Counts the tokens in a message asynchronously. /// Counts the tokens in a message asynchronously.
/// </summary> /// </summary>
/// <param name="request">The count message tokens request.</param> /// <param name="request">The count message tokens request.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists the models asynchronously. /// Lists models asynchronously, returning a single page of results.
/// </summary> /// </summary>
/// <param name="request">The paging request to use for listing the models.</param> /// <param name="request">The paging request to use for listing the models.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists the models asynchronously /// Lists all models asynchronously, returning every page of results.
/// </summary> /// </summary>
/// <param name="limit">The maximum number of models to return in each page.</param> /// <param name="limit">The maximum number of models to return in each page.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Gets a model by its ID asynchronously. /// Gets a model by its ID asynchronously.
/// </summary> /// </summary>
/// <param name="modelId">The ID of the model to get.</param> /// <param name="modelId">The ID of the model to get.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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> /// <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); Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId, CancellationToken cancellationToken = default);
} }
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
@@ -10,21 +10,21 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.2" /> <PackageReference Include="FluentAssertions" Version="7.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" /> <PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" /> <PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="SystemTextJson.JsonDiffPatch.Xunit" Version="2.0.0" /> <PackageReference Include="SystemTextJson.JsonDiffPatch.Xunit" Version="2.0.0" />
<PackageReference Include="xunit" Version="2.9.2" /> <PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="3.1.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2"> <PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.2"> <PackageReference Include="coverlet.msbuild" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>