feat: initial implementation of creating a file via the Files API

This commit is contained in:
Stevan Freeborn
2025-07-14 23:30:02 -05:00
parent 307c0da0c9
commit e674b9afe6
6 changed files with 212 additions and 0 deletions
@@ -0,0 +1,53 @@
using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a file object from the Anthropic Files API.
/// </summary>
public class AnthropicFile
{
/// <summary>
/// Unique object identifier.
/// The format and length of IDs may change over time.
/// </summary>
[JsonPropertyName("id")]
public string Id { get; init; } = string.Empty;
/// <summary>
/// Object type.
/// For files, this is always "file".
/// </summary>
[JsonPropertyName("type")]
public string Type { get; init; } = "file";
/// <summary>
/// Original filename of the uploaded file.
/// </summary>
[JsonPropertyName("file_name")]
public string FileName { get; init; } = string.Empty;
/// <summary>
/// RFC 3339 datetime string representing when the file was created.
/// </summary>
[JsonPropertyName("created_at")]
public string CreatedAt { get; init; } = string.Empty;
/// <summary>
/// Size of the file in bytes.
/// </summary>
[JsonPropertyName("size_bytes")]
public long SizeBytes { get; init; }
/// <summary>
/// MIME type of the file.
/// </summary>
[JsonPropertyName("file_type")]
public string FileType { get; init; } = string.Empty;
/// <summary>
/// Whether the file can be downloaded.
/// </summary>
[JsonPropertyName("downloadable")]
public bool Downloadable { get; init; }
}
@@ -0,0 +1,66 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a request to create a file via the Anthropic Files API.
/// </summary>
public class CreateFileRequest
{
/// <summary>
/// The file content as a byte array.
/// </summary>
public byte[] File { get; }
/// <summary>
/// The original filename of the file being uploaded.
/// </summary>
public string FileName { get; }
/// <summary>
/// The MIME type of the file.
/// </summary>
public string FileType { get; }
/// <summary>
/// Initializes a new instance of the <see cref="CreateFileRequest"/> class.
/// </summary>
/// <param name="file">The file content as a byte array.</param>
/// <param name="fileName">The original filename of the file being uploaded.</param>
/// <param name="fileType">The MIME type of the file.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="file"/>, <paramref name="fileName"/>, or <paramref name="fileType"/> is null.</exception>
public CreateFileRequest(byte[] file, string fileName, string fileType)
{
ArgumentValidator.ThrowIfNull(file, nameof(file));
ArgumentValidator.ThrowIfNullOrWhitespace(fileName, nameof(fileName));
ArgumentValidator.ThrowIfNullOrWhitespace(fileType, nameof(fileType));
File = file;
FileName = fileName;
FileType = fileType;
}
/// <summary>
/// Initializes a new instance of the <see cref="CreateFileRequest"/> class from a stream.
/// </summary>
/// <param name="stream">The stream containing the file content.</param>
/// <param name="fileName">The original filename of the file being uploaded.</param>
/// <param name="fileType">The MIME type of the file.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="stream"/>, <paramref name="fileName"/>, or <paramref name="fileType"/> is null.</exception>
public CreateFileRequest(Stream stream, string fileName, string fileType)
{
ArgumentValidator.ThrowIfNull(stream, nameof(stream));
ArgumentValidator.ThrowIfNullOrWhitespace(fileName, nameof(fileName));
ArgumentValidator.ThrowIfNullOrWhitespace(fileType, nameof(fileType));
using var memoryStream = new MemoryStream();
stream.CopyToAsync(memoryStream);
var fileContent = memoryStream.ToArray();
File = fileContent;
FileName = fileName;
FileType = fileType;
}
}