-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🌿 Fern Regeneration -- August 1, 2024 #37
Changes from 8 commits
93db068
af49570
a85df3a
45e74bd
0731aa4
3667121
321b017
634b686
b596a4c
fea248b
6713ddd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using AssemblyAI.Realtime; | ||
|
||
namespace AssemblyAI.IntegrationTests; | ||
|
||
[TestFixture] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using AssemblyAI.Core; | ||
|
||
#nullable enable | ||
|
||
namespace AssemblyAI.Core; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to expose a |
||
|
||
/// <summary> | ||
/// This exception type will be thrown for any non-2XX API responses. | ||
/// </summary> | ||
public class AssemblyAIClientApiException(string message, int statusCode, object body) | ||
: AssemblyAIClientException(message) | ||
{ | ||
/// <summary> | ||
/// The error code of the response that triggered the exception. | ||
/// </summary> | ||
public int StatusCode { get; } = statusCode; | ||
|
||
/// <summary> | ||
/// The body of the response that triggered the exception. | ||
/// </summary> | ||
public object Body { get; } = body; | ||
|
||
public override string ToString() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exceptions shouldn't override the |
||
{ | ||
return $"AssemblyAIClientApiException {{ message: {Message}, statusCode: {StatusCode}, body: {Body} }}"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace AssemblyAI.Core; | ||
|
||
public class Environments | ||
public class AssemblyAIClientEnvironment | ||
{ | ||
public static string DEFAULT = "https://api.assemblyai.com"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
#nullable enable | ||
|
||
namespace AssemblyAI.Core; | ||
|
||
/// <summary> | ||
/// Base exception class for all exceptions thrown by the SDK. | ||
/// </summary> | ||
public class AssemblyAIClientException(string message, Exception? innerException = null) | ||
: Exception(message, innerException) { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace AssemblyAI.Core; | ||
|
||
public class DateTimeSerializer : JsonConverter<DateTime> | ||
{ | ||
public override DateTime Read( | ||
ref Utf8JsonReader reader, | ||
System.Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
return DateTime.Parse(reader.GetString()!, null, DateTimeStyles.RoundtripKind); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString(Constants.DateTimeFormat)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace AssemblyAI.Core; | ||
|
||
public static class JsonOptions | ||
{ | ||
public static readonly JsonSerializerOptions JsonSerializerOptions; | ||
|
||
static JsonOptions() | ||
{ | ||
JsonSerializerOptions = new JsonSerializerOptions | ||
{ | ||
Converters = { new DateTimeSerializer() }, | ||
WriteIndented = true, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to configure this, so that The API should be able to handle null values, but it doesn't properly for some properties, which has been reported. |
||
}; | ||
} | ||
} | ||
|
||
public static class JsonUtils | ||
{ | ||
public static string Serialize<T>(T obj) | ||
{ | ||
return JsonSerializer.Serialize(obj, JsonOptions.JsonSerializerOptions); | ||
} | ||
|
||
public static T Deserialize<T>(string json) | ||
{ | ||
return JsonSerializer.Deserialize<T>(json, JsonOptions.JsonSerializerOptions)!; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using AssemblyAI.Lemur; | ||
|
||
namespace AssemblyAI.Core; | ||
|
||
public class LemurResponseConverter : JsonConverter<LemurResponse> | ||
{ | ||
public override LemurResponse? Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
if (reader.TokenType is JsonTokenType.Null) | ||
return default; | ||
|
||
var jsonElement = JsonSerializer.Deserialize<JsonElement>(ref reader, options); | ||
if (jsonElement.GetProperty("response").ValueKind == JsonValueKind.Array) | ||
{ | ||
return jsonElement.Deserialize<LemurQuestionAnswerResponse>() ?? | ||
throw new Exception("Failed to deserialize LemurQuestionAnswerResponse"); | ||
} | ||
|
||
return jsonElement.Deserialize<LemurStringResponse>() ?? | ||
throw new Exception("Failed to deserialize LemurStringResponse"); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, LemurResponse value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value.Value, options); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to keep using this custom client options which has
set
instead ofinit
, and includes the API key.These custom client options are used for the DI extensions.