feat: implement GetFileInfoAsync, GetFileAsync, and DeleteFileAsync
This commit is contained in:
@@ -385,6 +385,14 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
public async Task<AnthropicResult<AnthropicFile>> CreateFileAsync(CreateFileRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = await SendFileRequestAsync(FilesEndpoint, request, cancellationToken);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
||||
return AnthropicResult<AnthropicFile>.Failure(error, new AnthropicHeaders(response.Headers));
|
||||
}
|
||||
|
||||
return await CreateResultAsync<AnthropicFile>(response);
|
||||
}
|
||||
|
||||
@@ -394,6 +402,14 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
var pagingRequest = request ?? new PagingRequest();
|
||||
var endpoint = $"{FilesEndpoint}?{pagingRequest.ToQueryParameters()}";
|
||||
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
||||
return AnthropicResult<Page<AnthropicFile>>.Failure(error, new AnthropicHeaders(response.Headers));
|
||||
}
|
||||
|
||||
return await CreateResultAsync<Page<AnthropicFile>>(response);
|
||||
}
|
||||
|
||||
@@ -406,6 +422,55 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<AnthropicResult<AnthropicFile>> GetFileInfoAsync(string fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var endpoint = $"{FilesEndpoint}/{fileId}";
|
||||
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
||||
return AnthropicResult<AnthropicFile>.Failure(error, new AnthropicHeaders(response.Headers));
|
||||
}
|
||||
|
||||
return await CreateResultAsync<AnthropicFile>(response);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<AnthropicResult<Stream>> GetFileAsync(string fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var endpoint = $"{FilesEndpoint}/{fileId}/content";
|
||||
var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
||||
return AnthropicResult<Stream>.Failure(error, new AnthropicHeaders(response.Headers));
|
||||
}
|
||||
|
||||
var stream = await response.Content.ReadAsStreamAsync();
|
||||
return AnthropicResult<Stream>.Success(stream, new AnthropicHeaders(response.Headers));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<AnthropicResult<AnthropicFileDeleteResponse>> DeleteFileAsync(string fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var endpoint = $"{FilesEndpoint}/{fileId}";
|
||||
var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
||||
return AnthropicResult<AnthropicFileDeleteResponse>.Failure(error, new AnthropicHeaders(response.Headers));
|
||||
}
|
||||
|
||||
return await CreateResultAsync<AnthropicFileDeleteResponse>(response);
|
||||
}
|
||||
|
||||
private async IAsyncEnumerable<AnthropicResult<Page<T>>> GetAllPagesAsync<T>(string endpoint, int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
{
|
||||
var pagingRequest = new PagingRequest(limit: limit);
|
||||
|
||||
@@ -135,4 +135,29 @@ public interface IAnthropicApiClient
|
||||
/// <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="AnthropicFile"/>.</returns>
|
||||
IAsyncEnumerable<AnthropicResult<Page<AnthropicFile>>> ListAllFilesAsync(int limit = 20, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a file's metadata by its ID asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="fileId">The ID of the file 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="AnthropicFile"/>.</returns>
|
||||
Task<AnthropicResult<AnthropicFile>> GetFileInfoAsync(string fileId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a file's content by its ID asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="fileId">The ID of the file to get the content 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 a stream containing the file content.</returns>
|
||||
Task<AnthropicResult<Stream>> GetFileAsync(string fileId, CancellationToken cancellationToken = default);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a file by its ID asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="fileId">The ID of the file 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="AnthropicFileDeleteResponse"/>.</returns>
|
||||
Task<AnthropicResult<AnthropicFileDeleteResponse>> DeleteFileAsync(string fileId, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the response from deleting a file in the Anthropic API.
|
||||
/// </summary>
|
||||
public class AnthropicFileDeleteResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the ID of the file that was deleted.
|
||||
/// </summary>
|
||||
public string Id { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the response type
|
||||
/// </summary>
|
||||
public string Type { get; init; } = string.Empty;
|
||||
}
|
||||
Reference in New Issue
Block a user