Skip to content

Commit c9d41c1

Browse files
feat(api): manual updates
1 parent 38c681b commit c9d41c1

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 33
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic%2Fanthropic-7fce94a3c9f72c1d81df2009682a3b554e4b641b32443fd7c1f09f566420f711.yml
33
openapi_spec_hash: ae7e30bb8d093b5546cdc2b180f7b8e0
4-
config_hash: 32f037e4bc66dfaca12016b0e5110140
4+
config_hash: 35ba9c665cfdad811e3e0af88f50a5fc
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Text.Json;
2+
using Anthropic.Core;
3+
using Anthropic.Exceptions;
4+
using Anthropic.Models;
5+
6+
namespace Anthropic.Tests.Models;
7+
8+
public class ErrorTypeTest : TestBase
9+
{
10+
[Theory]
11+
[InlineData(ErrorType.InvalidRequestError)]
12+
[InlineData(ErrorType.AuthenticationError)]
13+
[InlineData(ErrorType.PermissionError)]
14+
[InlineData(ErrorType.NotFoundError)]
15+
[InlineData(ErrorType.RateLimitError)]
16+
[InlineData(ErrorType.TimeoutError)]
17+
[InlineData(ErrorType.OverloadedError)]
18+
[InlineData(ErrorType.ApiError)]
19+
[InlineData(ErrorType.BillingError)]
20+
public void Validation_Works(ErrorType rawValue)
21+
{
22+
// force implicit conversion because Theory can't do that for us
23+
ApiEnum<string, ErrorType> value = rawValue;
24+
value.Validate();
25+
}
26+
27+
[Fact]
28+
public void InvalidEnumValidationThrows_Works()
29+
{
30+
var value = JsonSerializer.Deserialize<ApiEnum<string, ErrorType>>(
31+
JsonSerializer.SerializeToElement("invalid value"),
32+
ModelBase.SerializerOptions
33+
);
34+
35+
Assert.NotNull(value);
36+
Assert.Throws<AnthropicInvalidDataException>(() => value.Validate());
37+
}
38+
39+
[Theory]
40+
[InlineData(ErrorType.InvalidRequestError)]
41+
[InlineData(ErrorType.AuthenticationError)]
42+
[InlineData(ErrorType.PermissionError)]
43+
[InlineData(ErrorType.NotFoundError)]
44+
[InlineData(ErrorType.RateLimitError)]
45+
[InlineData(ErrorType.TimeoutError)]
46+
[InlineData(ErrorType.OverloadedError)]
47+
[InlineData(ErrorType.ApiError)]
48+
[InlineData(ErrorType.BillingError)]
49+
public void SerializationRoundtrip_Works(ErrorType rawValue)
50+
{
51+
// force implicit conversion because Theory can't do that for us
52+
ApiEnum<string, ErrorType> value = rawValue;
53+
54+
string json = JsonSerializer.Serialize(value, ModelBase.SerializerOptions);
55+
var deserialized = JsonSerializer.Deserialize<ApiEnum<string, ErrorType>>(
56+
json,
57+
ModelBase.SerializerOptions
58+
);
59+
60+
Assert.Equal(value, deserialized);
61+
}
62+
63+
[Fact]
64+
public void InvalidEnumSerializationRoundtrip_Works()
65+
{
66+
var value = JsonSerializer.Deserialize<ApiEnum<string, ErrorType>>(
67+
JsonSerializer.SerializeToElement("invalid value"),
68+
ModelBase.SerializerOptions
69+
);
70+
string json = JsonSerializer.Serialize(value, ModelBase.SerializerOptions);
71+
var deserialized = JsonSerializer.Deserialize<ApiEnum<string, ErrorType>>(
72+
json,
73+
ModelBase.SerializerOptions
74+
);
75+
76+
Assert.Equal(value, deserialized);
77+
}
78+
}

src/Anthropic/Core/ModelBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json;
22
using Anthropic.Exceptions;
3+
using Anthropic.Models;
34
using Anthropic.Models.Beta;
45
using Anthropic.Models.Messages;
56
using Batches = Anthropic.Models.Messages.Batches;
@@ -26,6 +27,7 @@ protected ModelBase(ModelBase modelBase)
2627
Converters =
2728
{
2829
new FrozenDictionaryConverterFactory(),
30+
new ApiEnumConverter<string, ErrorType>(),
2931
new ApiEnumConverter<string, MediaType>(),
3032
new ApiEnumConverter<string, BashCodeExecutionToolResultErrorCode>(),
3133
new ApiEnumConverter<string, Ttl>(),

src/Anthropic/Models/ErrorType.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
using Anthropic.Exceptions;
5+
6+
namespace Anthropic.Models;
7+
8+
[JsonConverter(typeof(ErrorTypeConverter))]
9+
public enum ErrorType
10+
{
11+
InvalidRequestError,
12+
AuthenticationError,
13+
PermissionError,
14+
NotFoundError,
15+
RateLimitError,
16+
TimeoutError,
17+
OverloadedError,
18+
ApiError,
19+
BillingError,
20+
}
21+
22+
sealed class ErrorTypeConverter : JsonConverter<ErrorType>
23+
{
24+
public override ErrorType Read(
25+
ref Utf8JsonReader reader,
26+
Type typeToConvert,
27+
JsonSerializerOptions options
28+
)
29+
{
30+
return JsonSerializer.Deserialize<string>(ref reader, options) switch
31+
{
32+
"invalid_request_error" => ErrorType.InvalidRequestError,
33+
"authentication_error" => ErrorType.AuthenticationError,
34+
"permission_error" => ErrorType.PermissionError,
35+
"not_found_error" => ErrorType.NotFoundError,
36+
"rate_limit_error" => ErrorType.RateLimitError,
37+
"timeout_error" => ErrorType.TimeoutError,
38+
"overloaded_error" => ErrorType.OverloadedError,
39+
"api_error" => ErrorType.ApiError,
40+
"billing_error" => ErrorType.BillingError,
41+
_ => (ErrorType)(-1),
42+
};
43+
}
44+
45+
public override void Write(
46+
Utf8JsonWriter writer,
47+
ErrorType value,
48+
JsonSerializerOptions options
49+
)
50+
{
51+
JsonSerializer.Serialize(
52+
writer,
53+
value switch
54+
{
55+
ErrorType.InvalidRequestError => "invalid_request_error",
56+
ErrorType.AuthenticationError => "authentication_error",
57+
ErrorType.PermissionError => "permission_error",
58+
ErrorType.NotFoundError => "not_found_error",
59+
ErrorType.RateLimitError => "rate_limit_error",
60+
ErrorType.TimeoutError => "timeout_error",
61+
ErrorType.OverloadedError => "overloaded_error",
62+
ErrorType.ApiError => "api_error",
63+
ErrorType.BillingError => "billing_error",
64+
_ => throw new AnthropicInvalidDataException(
65+
string.Format("Invalid value '{0}' in {1}", value, nameof(value))
66+
),
67+
},
68+
options
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)