feat: add classes to model response to get model list endpoint

This commit is contained in:
Stevan Freeborn
2025-01-02 21:45:37 -06:00
parent 9e367d3542
commit 28b7bf6a89
4 changed files with 237 additions and 0 deletions
@@ -0,0 +1,31 @@
using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents an Anthropic model.
/// </summary>
public class AnthropicModel
{
/// <summary>
/// The type of the model.
/// </summary>
public string Type { get; init; } = string.Empty;
/// <summary>
/// The id of the model.
/// </summary>
public string Id { get; init; } = string.Empty;
/// <summary>
/// The display name of the model.
/// </summary>
[JsonPropertyName("display_name")]
public string DisplayName { get; init; } = string.Empty;
/// <summary>
/// The created date of the model.
/// </summary>
[JsonPropertyName("created_at")]
public DateTimeOffset CreatedAt { get; init; }
}
+38
View File
@@ -0,0 +1,38 @@
using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a page.
/// </summary>
public class Page
{
/// <summary>
/// The id of the first item in the page.
/// </summary>
[JsonPropertyName("first_id")]
public string FirstId { get; init; } = string.Empty;
/// <summary>
/// The id of the last item in the page.
/// </summary>
[JsonPropertyName("last_id")]
public string LastId { get; init; } = string.Empty;
/// <summary>
/// Indicates whether there is more data to be retrieved.
/// </summary>
[JsonPropertyName("has_more")]
public bool HasMore { get; init; }
}
/// <summary>
/// Represents a page with data.
/// </summary>
public class Page<T> : Page
{
/// <summary>
/// The data in the page.
/// </summary>
public T[] Data { get; init; } = [];
}
@@ -0,0 +1,57 @@
namespace AnthropicClient.Tests.Unit.Models;
public class AnthropicModelTests : SerializationTest
{
private const string SampleJson = @"{
""type"": ""model"",
""id"": ""claude-3-opus-20240229"",
""display_name"": ""Claude 3 Opus"",
""created_at"": ""2024-02-29T00:00:00Z""
}";
[Fact]
public void Constructor_WhenCalled_ItShouldReturnAnInstanceWithPropertiesSet()
{
var model = new AnthropicModel();
model.Type.Should().BeEmpty();
model.Id.Should().BeEmpty();
model.DisplayName.Should().BeEmpty();
model.CreatedAt.Should().Be(default);
}
[Fact]
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedValues()
{
var result = Deserialize<AnthropicModel>(SampleJson);
result.Should().NotBeNull();
result!.Type.Should().Be("model");
result.Id.Should().Be("claude-3-opus-20240229");
result.DisplayName.Should().Be("Claude 3 Opus");
result.CreatedAt.Should().Be(new DateTimeOffset(2024, 2, 29, 0, 0, 0, TimeSpan.Zero));
}
[Fact]
public void JsonSerialization_WhenSerialized_ItShouldReturnExpectedShape()
{
var model = new AnthropicModel
{
Type = "model",
Id = "claude-3-opus-20240229",
DisplayName = "Claude 3 Opus",
CreatedAt = new DateTimeOffset(2024, 2, 29, 0, 0, 0, TimeSpan.Zero)
};
var result = Serialize(model);
var expectedJson = @"{
""type"": ""model"",
""id"": ""claude-3-opus-20240229"",
""display_name"": ""Claude 3 Opus"",
""created_at"": ""2024-02-29T00:00:00+00:00""
}";
JsonAssert.Equal(expectedJson, result);
}
}
@@ -0,0 +1,111 @@
namespace AnthropicClient.Tests.Unit.Models;
public class PageTests : SerializationTest
{
private const string BasePageJson = @"{
""first_id"": ""msg_123"",
""last_id"": ""msg_456"",
""has_more"": true
}";
private const string GenericPageJson = @"{
""first_id"": ""msg_123"",
""last_id"": ""msg_456"",
""has_more"": true,
""data"": [""item1"", ""item2""]
}";
private const string EmptyPageJson = @"{
""first_id"": """",
""last_id"": """",
""has_more"": false,
""data"": []
}";
[Fact]
public void Constructor_WhenCalled_ItShouldReturnAnInstanceWithPropertiesSet()
{
var page = new Page();
page.FirstId.Should().BeEmpty();
page.LastId.Should().BeEmpty();
page.HasMore.Should().BeFalse();
}
[Fact]
public void Constructor_WhenCalledWithGeneric_ItShouldReturnAnInstanceWithPropertiesSet()
{
var page = new Page<string>();
page.FirstId.Should().BeEmpty();
page.LastId.Should().BeEmpty();
page.HasMore.Should().BeFalse();
page.Data.Should().BeEmpty();
}
[Fact]
public void JsonDeserialization_WhenCalled_ItShouldHaveCorrectValues()
{
var result = Deserialize<Page>(BasePageJson);
result.Should().NotBeNull();
result!.FirstId.Should().Be("msg_123");
result.LastId.Should().Be("msg_456");
result.HasMore.Should().BeTrue();
}
[Fact]
public void JsonSerialization_WhenCalled_ItShouldHaveExpectedShape()
{
var page = new Page
{
FirstId = "msg_123",
LastId = "msg_456",
HasMore = true
};
var result = Serialize(page);
JsonAssert.Equal(BasePageJson, result);
}
[Fact]
public void JsonDeserialization_WhenCalledWithData_ItShouldHaveCorrectValues()
{
var result = Deserialize<Page<string>>(GenericPageJson);
result.Should().NotBeNull();
result!.FirstId.Should().Be("msg_123");
result.LastId.Should().Be("msg_456");
result.HasMore.Should().BeTrue();
result.Data.Should().BeEquivalentTo(["item1", "item2"]);
}
[Fact]
public void JsonSerialization_WhenCalledWithData_ItShouldHaveExpectedShape()
{
var page = new Page<string>
{
FirstId = "msg_123",
LastId = "msg_456",
HasMore = true,
Data = ["item1", "item2"]
};
var result = Serialize(page);
JsonAssert.Equal(GenericPageJson, result);
}
[Fact]
public void JsonDeserialization_WhenCalledWithEmptyData_ItShouldHaveCorrectValues()
{
var result = Deserialize<Page<string>>(EmptyPageJson);
result.Should().NotBeNull();
result!.FirstId.Should().BeEmpty();
result.LastId.Should().BeEmpty();
result.HasMore.Should().BeFalse();
result.Data.Should().BeEmpty();
}
}