feat: add model for DocumentContent and DocumentSource
This commit is contained in:
@@ -16,6 +16,7 @@ class ContentConverter : JsonConverter<Content>
|
|||||||
{
|
{
|
||||||
ContentType.Text => JsonSerializer.Deserialize<TextContent>(root.GetRawText(), options)!,
|
ContentType.Text => JsonSerializer.Deserialize<TextContent>(root.GetRawText(), options)!,
|
||||||
ContentType.Image => JsonSerializer.Deserialize<ImageContent>(root.GetRawText(), options)!,
|
ContentType.Image => JsonSerializer.Deserialize<ImageContent>(root.GetRawText(), options)!,
|
||||||
|
ContentType.Document => JsonSerializer.Deserialize<DocumentContent>(root.GetRawText(), options)!,
|
||||||
ContentType.ToolUse => JsonSerializer.Deserialize<ToolUseContent>(root.GetRawText(), options)!,
|
ContentType.ToolUse => JsonSerializer.Deserialize<ToolUseContent>(root.GetRawText(), options)!,
|
||||||
ContentType.ToolResult => JsonSerializer.Deserialize<ToolResultContent>(root.GetRawText(), options)!,
|
ContentType.ToolResult => JsonSerializer.Deserialize<ToolResultContent>(root.GetRawText(), options)!,
|
||||||
_ => throw new JsonException($"Unknown content type: {type}")
|
_ => throw new JsonException($"Unknown content type: {type}")
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
using AnthropicClient.Utils;
|
||||||
|
|
||||||
|
namespace AnthropicClient.Models;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents content from a document that is part of a message.
|
||||||
|
/// </summary>
|
||||||
|
public class DocumentContent : Content
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the source of the document.
|
||||||
|
/// </summary>
|
||||||
|
public DocumentSource Source { get; init; } = new();
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
|
internal DocumentContent()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Validate(string mediaType, string data)
|
||||||
|
{
|
||||||
|
ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType));
|
||||||
|
ArgumentValidator.ThrowIfNull(data, nameof(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="DocumentContent"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mediaType">The media type of the document.</param>
|
||||||
|
/// <param name="data">The data of the document.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">Thrown when the media type or data is null.</exception>
|
||||||
|
/// <returns>A new instance of the <see cref="DocumentContent"/> class.</returns>
|
||||||
|
public DocumentContent(string mediaType, string data) : base(ContentType.Document)
|
||||||
|
{
|
||||||
|
Validate(mediaType, data);
|
||||||
|
|
||||||
|
Source = new(mediaType, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="DocumentContent"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mediaType">The media type of the document.</param>
|
||||||
|
/// <param name="data">The data of the document.</param>
|
||||||
|
/// <param name="cacheControl">The cache control to be used for the content.</param>
|
||||||
|
/// <returns>A new instance of the <see cref="DocumentContent"/> class.</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">Thrown when the media type, data, or cache control is null.</exception>
|
||||||
|
public DocumentContent(string mediaType, string data, CacheControl cacheControl) : base(ContentType.Document, cacheControl)
|
||||||
|
{
|
||||||
|
Validate(mediaType, data);
|
||||||
|
|
||||||
|
Source = new(mediaType, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
using AnthropicClient.Utils;
|
||||||
|
|
||||||
|
namespace AnthropicClient.Models;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a document source.
|
||||||
|
/// </summary>
|
||||||
|
public class DocumentSource
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the media type of the document.
|
||||||
|
/// </summary>
|
||||||
|
[JsonPropertyName("media_type")]
|
||||||
|
public string MediaType { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the data of the document.
|
||||||
|
/// </summary>
|
||||||
|
public string Data { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of encoding of the document data.
|
||||||
|
/// </summary>
|
||||||
|
public string Type { get; init; } = "base64";
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
|
internal DocumentSource()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="DocumentSource"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mediaType">The media type of the document.</param>
|
||||||
|
/// <param name="data">The data of the document.</param>
|
||||||
|
/// <exception cref="ArgumentException">Thrown when the media type is invalid.</exception>
|
||||||
|
/// <exception cref="ArgumentNullException">Thrown when the media type or data is null.</exception>
|
||||||
|
/// <returns>A new instance of the <see cref="DocumentSource"/> class.</returns>
|
||||||
|
public DocumentSource(string mediaType, string data)
|
||||||
|
{
|
||||||
|
ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType));
|
||||||
|
ArgumentValidator.ThrowIfNull(data, nameof(data));
|
||||||
|
|
||||||
|
MediaType = mediaType;
|
||||||
|
Data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
namespace AnthropicClient.Tests.Unit.Models;
|
||||||
|
|
||||||
|
public class DocumentContentTests : SerializationTest
|
||||||
|
{
|
||||||
|
private readonly string _testJson = @"{
|
||||||
|
""source"": {
|
||||||
|
""media_type"": ""application/pdf"",
|
||||||
|
""data"": ""data"",
|
||||||
|
""type"": ""base64""
|
||||||
|
},
|
||||||
|
""type"": ""document""
|
||||||
|
}";
|
||||||
|
|
||||||
|
private readonly string _testJsonWithCacheControl = @"{
|
||||||
|
""source"": {
|
||||||
|
""media_type"": ""application/pdf"",
|
||||||
|
""data"": ""data"",
|
||||||
|
""type"": ""base64""
|
||||||
|
},
|
||||||
|
""cache_control"": { ""type"": ""ephemeral"" },
|
||||||
|
""type"": ""document""
|
||||||
|
}";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalled_ItShouldInitializeSource()
|
||||||
|
{
|
||||||
|
var expectedMediaType = "application/pdf";
|
||||||
|
var expectedData = "data";
|
||||||
|
|
||||||
|
var result = new DocumentContent(expectedMediaType, expectedData);
|
||||||
|
|
||||||
|
result.Source.Should().BeEquivalentTo(new DocumentSource(expectedMediaType, expectedData));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledAndMediaTypeIsNull_ItShouldThrowArgumentNullException()
|
||||||
|
{
|
||||||
|
var expectedData = "data";
|
||||||
|
|
||||||
|
var action = () => new DocumentContent(null!, expectedData);
|
||||||
|
|
||||||
|
action.Should().Throw<ArgumentNullException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledAndDataIsNull_ItShouldThrowArgumentNullException()
|
||||||
|
{
|
||||||
|
var expectedMediaType = "application/pdf";
|
||||||
|
|
||||||
|
var action = () => new DocumentContent(expectedMediaType, null!);
|
||||||
|
|
||||||
|
action.Should().Throw<ArgumentNullException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledWithCacheControl_ItShouldInitializeSourceAndCacheControl()
|
||||||
|
{
|
||||||
|
var expectedMediaType = "application/pdf";
|
||||||
|
var expectedData = "data";
|
||||||
|
var cacheControl = new EphemeralCacheControl();
|
||||||
|
|
||||||
|
var result = new DocumentContent(expectedMediaType, expectedData, cacheControl);
|
||||||
|
|
||||||
|
result.Source.Should().BeEquivalentTo(new DocumentSource(expectedMediaType, expectedData));
|
||||||
|
result.CacheControl.Should().BeSameAs(cacheControl);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledWithCacheControlAndMediaTypeIsNull_ItShouldThrowArgumentNullException()
|
||||||
|
{
|
||||||
|
var expectedData = "data";
|
||||||
|
var cacheControl = new EphemeralCacheControl();
|
||||||
|
|
||||||
|
var action = () => new DocumentContent(null!, expectedData, cacheControl);
|
||||||
|
|
||||||
|
action.Should().Throw<ArgumentNullException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledWithCacheControlAndDataIsNull_ItShouldThrowArgumentNullException()
|
||||||
|
{
|
||||||
|
var expectedMediaType = "application/pdf";
|
||||||
|
var cacheControl = new EphemeralCacheControl();
|
||||||
|
|
||||||
|
var action = () => new DocumentContent(expectedMediaType, null!, cacheControl);
|
||||||
|
|
||||||
|
action.Should().Throw<ArgumentNullException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
|
||||||
|
{
|
||||||
|
var content = new DocumentContent("application/pdf", "data");
|
||||||
|
|
||||||
|
var actual = Serialize(content);
|
||||||
|
|
||||||
|
JsonAssert.Equal(_testJson, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonSerialization_WhenSerializedWithCacheControl_ItShouldHaveExpectedShape()
|
||||||
|
{
|
||||||
|
var content = new DocumentContent("application/pdf", "data", new EphemeralCacheControl());
|
||||||
|
|
||||||
|
var actual = Serialize(content);
|
||||||
|
|
||||||
|
JsonAssert.Equal(_testJsonWithCacheControl, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape()
|
||||||
|
{
|
||||||
|
var expected = new DocumentContent("application/pdf", "data");
|
||||||
|
|
||||||
|
var actual = Deserialize<DocumentContent>(_testJson);
|
||||||
|
|
||||||
|
actual.Should().BeEquivalentTo(expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
namespace AnthropicClient.Tests.Unit.Models;
|
||||||
|
|
||||||
|
public class DocumentSourceTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledWithValidArguments_ItShouldSetProperties()
|
||||||
|
{
|
||||||
|
var mediaType = "application/pdf";
|
||||||
|
var data = "base64data";
|
||||||
|
|
||||||
|
var source = new DocumentSource(mediaType, data);
|
||||||
|
|
||||||
|
source.MediaType.Should().Be(mediaType);
|
||||||
|
source.Data.Should().Be(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledWithNullMediaType_ItShouldThrowArgumentNullException()
|
||||||
|
{
|
||||||
|
string? mediaType = null;
|
||||||
|
var data = "base64data";
|
||||||
|
|
||||||
|
var action = () => new DocumentSource(mediaType!, data);
|
||||||
|
|
||||||
|
action.Should().Throw<ArgumentNullException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalledWithNullData_ItShouldThrowArgumentNullException()
|
||||||
|
{
|
||||||
|
var mediaType = "application/pdf";
|
||||||
|
string? data = null;
|
||||||
|
|
||||||
|
var action = () => new DocumentSource(mediaType, data!);
|
||||||
|
|
||||||
|
action.Should().Throw<ArgumentNullException>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Constructor_WhenCalled_ItShouldHaveTypePropertySetToBase64()
|
||||||
|
{
|
||||||
|
var mediaType = "application/pdf";
|
||||||
|
var data = "base64data";
|
||||||
|
|
||||||
|
var source = new DocumentSource(mediaType, data);
|
||||||
|
|
||||||
|
source.Type.Should().Be("base64");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user