diff --git a/examples/FileOperationsExample.cs b/examples/FileOperationsExample.cs new file mode 100644 index 0000000..e509d84 --- /dev/null +++ b/examples/FileOperationsExample.cs @@ -0,0 +1,89 @@ +using AnthropicClient; +using AnthropicClient.Models; + +namespace AnthropicClient.Examples; + +/// +/// Example demonstrating file operations with the Anthropic API. +/// +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}"); + } + } +} \ No newline at end of file diff --git a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientFileTests.cs b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientFileTests.cs index 6c25011..a7059ac 100644 --- a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientFileTests.cs +++ b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientFileTests.cs @@ -161,59 +161,4 @@ public class AnthropicApiClientFileTests : IntegrationTest result.Error.Should().BeOfType(); result.Error.Error.Should().BeOfType(); } - - [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>>(); - 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"); - } } \ No newline at end of file