Skip to content

Commit

Permalink
Serialize LemurResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Aug 1, 2024
1 parent 321b017 commit 634b686
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ src/AssemblyAI/AssemblyAIClient.cs
src/AssemblyAI/DependencyInjectionExtensions.cs
src/AssemblyAI/Files/ExtendedFilesClient.cs
src/AssemblyAI/Transcripts/ExtendedTranscriptsClient.cs
src/AssemblyAI/Lemur/Types/LemurResponse.cs
src/AssemblyAI/Realtime/RealtimeTranscriber.cs
src/AssemblyAI/Realtime/WebsocketClient
src/AssemblyAI/Realtime/Types/RealtimeTranscript.cs
Expand Down
9 changes: 5 additions & 4 deletions src/AssemblyAI.IntegrationTests/LemurTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AssemblyAI.Lemur;

namespace AssemblyAI.IntegrationTests;

[TestFixture]
Expand All @@ -6,8 +8,7 @@ public class LemurTests
private static string ApiKey => AssemblyAITestParameters.ApiKey;
private static string[] TranscriptIds => AssemblyAITestParameters.TranscriptIds;

// TODO: uncomment when Fern fixes params generation
/*[Test]
[Test]
public async Task Should_Generate_Summary()
{
var client = new AssemblyAIClient(ApiKey);
Expand Down Expand Up @@ -105,7 +106,7 @@ public async Task Should_Return_Response()
}).ConfigureAwait(false);

var taskResponse2OneOf = await client.Lemur.GetResponseAsync(taskResponse.RequestId).ConfigureAwait(false);
var taskResponse2 = (LemurStringResponse) taskResponse2OneOf.Value;
var taskResponse2 = (LemurStringResponse)taskResponse2OneOf.Value;
Assert.That(taskResponse2.RequestId, Is.EqualTo(taskResponse.RequestId));
Assert.That(taskResponse2.Response, Is.EqualTo(taskResponse.Response));
}
Expand All @@ -124,5 +125,5 @@ public async Task Should_Purge_Request_Data()
var deletionRequest = await client.Lemur.PurgeRequestDataAsync(summaryResponse.RequestId).ConfigureAwait(false);
Assert.That(deletionRequest.Deleted, Is.True);
Assert.That(summaryResponse.RequestId, Is.EqualTo(deletionRequest.RequestIdToPurge));
}*/
}
}
33 changes: 33 additions & 0 deletions src/AssemblyAI/Core/LemurResponseConverter.cs
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);
}
}
6 changes: 2 additions & 4 deletions src/AssemblyAI/Lemur/LemurClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public async Task<LemurActionItemsResponse> ActionItemsAsync(
/// <summary>
/// Retrieve a LeMUR response that was previously generated.
/// </summary>
public async Task<OneOf<LemurStringResponse, LemurQuestionAnswerResponse>> GetResponseAsync(
public async Task<LemurResponse> GetResponseAsync(
string requestId,
RequestOptions? options = null
)
Expand All @@ -290,9 +290,7 @@ public async Task<OneOf<LemurStringResponse, LemurQuestionAnswerResponse>> GetRe
{
try
{
return JsonUtils.Deserialize<
OneOf<LemurStringResponse, LemurQuestionAnswerResponse>
>(responseBody)!;
return JsonUtils.Deserialize<LemurResponse>(responseBody)!;
}
catch (JsonException e)
{
Expand Down
22 changes: 22 additions & 0 deletions src/AssemblyAI/Lemur/Types/LemurResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;
using AssemblyAI.Core;
using OneOf;

namespace AssemblyAI.Lemur;

[JsonConverter(typeof(LemurResponseConverter))]
public class LemurResponse : OneOfBase<LemurStringResponse, LemurQuestionAnswerResponse>
{
private LemurResponse(OneOf<LemurStringResponse, LemurQuestionAnswerResponse> response) : base(response)
{
}

public static implicit operator LemurResponse(OneOf<LemurStringResponse, LemurQuestionAnswerResponse> _) => new(_);
public static implicit operator LemurResponse(LemurStringResponse _) => new(_);
public static implicit operator LemurResponse(LemurQuestionAnswerResponse _) => new(_);

public bool IsLemurStringResponse => IsT0;
public bool IsLemurQuestionAnswerResponse => IsT1;
public LemurStringResponse AsLemurStringResponse => AsT0;
public LemurQuestionAnswerResponse AsLemurQuestionAnswerResponse => AsT1;
}

0 comments on commit 634b686

Please sign in to comment.