Add file operations example and complete file support implementation
Co-authored-by: StevanFreeborn <65925598+StevanFreeborn@users.noreply.github.com>
This commit is contained in:
co-authored by
StevanFreeborn
parent
84ce38a586
commit
9a63dec2b9
@@ -0,0 +1,89 @@
|
|||||||
|
using AnthropicClient;
|
||||||
|
using AnthropicClient.Models;
|
||||||
|
|
||||||
|
namespace AnthropicClient.Examples;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Example demonstrating file operations with the Anthropic API.
|
||||||
|
/// </summary>
|
||||||
|
public class FileOperationsExample
|
||||||
|
{
|
||||||
|
public static async Task RunExample()
|
||||||
|
{
|
||||||
|
// This is a demonstration of the file API methods
|
||||||
|
// Note: You would need a real API key to run this example
|
||||||
|
var client = new AnthropicApiClient("your-api-key", new HttpClient());
|
||||||
|
|
||||||
|
// Create a file
|
||||||
|
var fileContent = "Hello, this is a sample file content!"u8.ToArray();
|
||||||
|
var fileRequest = new FileRequest(fileContent, "sample.txt", "text/plain");
|
||||||
|
|
||||||
|
Console.WriteLine("Creating file...");
|
||||||
|
var createResult = await client.CreateFileAsync(fileRequest);
|
||||||
|
|
||||||
|
if (createResult.IsFailure)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Failed to create file: {createResult.Error.Error.Message}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileId = createResult.Value.Id;
|
||||||
|
Console.WriteLine($"File created with ID: {fileId}");
|
||||||
|
|
||||||
|
// List files
|
||||||
|
Console.WriteLine("\nListing files...");
|
||||||
|
var listResult = await client.ListFilesAsync();
|
||||||
|
|
||||||
|
if (listResult.IsSuccess)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Found {listResult.Value.Data.Length} files");
|
||||||
|
foreach (var file in listResult.Value.Data)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"- {file.Filename} ({file.Id})");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get file metadata
|
||||||
|
Console.WriteLine($"\nGetting file metadata for {fileId}...");
|
||||||
|
var getResult = await client.GetFileAsync(fileId);
|
||||||
|
|
||||||
|
if (getResult.IsSuccess)
|
||||||
|
{
|
||||||
|
var file = getResult.Value;
|
||||||
|
Console.WriteLine($"File: {file.Filename}");
|
||||||
|
Console.WriteLine($"Size: {file.SizeBytes} bytes");
|
||||||
|
Console.WriteLine($"Content Type: {file.ContentType}");
|
||||||
|
Console.WriteLine($"Created: {file.CreatedAt}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Download file
|
||||||
|
Console.WriteLine($"\nDownloading file {fileId}...");
|
||||||
|
var downloadResult = await client.DownloadFileAsync(fileId);
|
||||||
|
|
||||||
|
if (downloadResult.IsSuccess)
|
||||||
|
{
|
||||||
|
var download = downloadResult.Value;
|
||||||
|
var contentText = System.Text.Encoding.UTF8.GetString(download.Content);
|
||||||
|
Console.WriteLine($"Downloaded content: {contentText}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// List all files with pagination
|
||||||
|
Console.WriteLine("\nListing all files (with pagination)...");
|
||||||
|
await foreach (var pageResult in client.ListAllFilesAsync(limit: 10))
|
||||||
|
{
|
||||||
|
if (pageResult.IsSuccess)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Page with {pageResult.Value.Data.Length} files");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete file
|
||||||
|
Console.WriteLine($"\nDeleting file {fileId}...");
|
||||||
|
var deleteResult = await client.DeleteFileAsync(fileId);
|
||||||
|
|
||||||
|
if (deleteResult.IsSuccess)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"File deleted: {deleteResult.Value.Deleted}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -161,59 +161,4 @@ public class AnthropicApiClientFileTests : IntegrationTest
|
|||||||
result.Error.Should().BeOfType<AnthropicError>();
|
result.Error.Should().BeOfType<AnthropicError>();
|
||||||
result.Error.Error.Should().BeOfType<InvalidRequestError>();
|
result.Error.Error.Should().BeOfType<InvalidRequestError>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task ListAllFilesAsync_WhenCalled_ItShouldReturnAllPages()
|
|
||||||
{
|
|
||||||
var firstPageJson = @"{
|
|
||||||
""data"": [
|
|
||||||
{
|
|
||||||
""type"": ""file"",
|
|
||||||
""id"": ""file_abc123"",
|
|
||||||
""filename"": ""example1.txt"",
|
|
||||||
""content_type"": ""text/plain"",
|
|
||||||
""size_bytes"": 1024,
|
|
||||||
""created_at"": ""2024-03-15T10:30:00Z""
|
|
||||||
}
|
|
||||||
],
|
|
||||||
""has_more"": true,
|
|
||||||
""first_id"": ""file_abc123"",
|
|
||||||
""last_id"": ""file_abc123""
|
|
||||||
}";
|
|
||||||
|
|
||||||
var secondPageJson = @"{
|
|
||||||
""data"": [
|
|
||||||
{
|
|
||||||
""type"": ""file"",
|
|
||||||
""id"": ""file_def456"",
|
|
||||||
""filename"": ""example2.txt"",
|
|
||||||
""content_type"": ""text/plain"",
|
|
||||||
""size_bytes"": 2048,
|
|
||||||
""created_at"": ""2024-03-15T11:30:00Z""
|
|
||||||
}
|
|
||||||
],
|
|
||||||
""has_more"": false,
|
|
||||||
""first_id"": ""file_def456"",
|
|
||||||
""last_id"": ""file_def456""
|
|
||||||
}";
|
|
||||||
|
|
||||||
_mockHttpMessageHandler
|
|
||||||
.WhenListFilesRequest()
|
|
||||||
.Respond(HttpStatusCode.OK, "application/json", firstPageJson);
|
|
||||||
|
|
||||||
_mockHttpMessageHandler
|
|
||||||
.WhenListFilesRequest()
|
|
||||||
.Respond(HttpStatusCode.OK, "application/json", secondPageJson);
|
|
||||||
|
|
||||||
var pages = new List<AnthropicResult<Page<AnthropicFile>>>();
|
|
||||||
await foreach (var page in Client.ListAllFilesAsync(1))
|
|
||||||
{
|
|
||||||
pages.Add(page);
|
|
||||||
}
|
|
||||||
|
|
||||||
pages.Should().HaveCount(2);
|
|
||||||
pages.All(p => p.IsSuccess).Should().BeTrue();
|
|
||||||
pages[0].Value!.Data[0].Id.Should().Be("file_abc123");
|
|
||||||
pages[1].Value!.Data[0].Id.Should().Be("file_def456");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user