-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
509 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -478,4 +478,3 @@ $RECYCLE.BIN/ | |
|
||
.idea | ||
.runsettings | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Text.Json; | ||
using AssemblyAI.Core; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using NUnit.Framework; | ||
|
||
namespace AssemblyAI.Test; | ||
|
||
[TestFixture] | ||
public class DependencyInjectionClientOptionsTests | ||
{ | ||
private static readonly ClientOptions ValidAssemblyAIOptions = new() | ||
{ | ||
ApiKey = "MyAssemblyAIApiKey", | ||
BaseUrl = "https://api.assemblyai.com", | ||
MaxRetries = 2, | ||
TimeoutInSeconds = 30 | ||
}; | ||
|
||
[Test] | ||
public void AddAssemblyAIClient_With_Callback_Should_Match_Configuration() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddSingleton(BuildEmptyConfiguration()); | ||
serviceCollection.AddAssemblyAIClient((_, options) => | ||
{ | ||
options.ApiKey = ValidAssemblyAIOptions.ApiKey; | ||
options.BaseUrl = ValidAssemblyAIOptions.BaseUrl; | ||
options.MaxRetries = ValidAssemblyAIOptions.MaxRetries; | ||
options.TimeoutInSeconds = ValidAssemblyAIOptions.TimeoutInSeconds; | ||
}); | ||
|
||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
var assemblyAIClientOptions = serviceProvider.GetService<IOptions<ClientOptions>>()?.Value; | ||
|
||
var expectedJson = JsonSerializer.Serialize(ValidAssemblyAIOptions); | ||
var actualJson = JsonSerializer.Serialize(assemblyAIClientOptions); | ||
|
||
Assert.That(actualJson, Is.EqualTo(expectedJson)); | ||
} | ||
|
||
[Test] | ||
public async Task AddAssemblyAIClient_From_Configuration_Should_Reload_On_Change() | ||
{ | ||
const string optionsFile = "ClientOptions.json"; | ||
if (File.Exists(optionsFile)) File.Delete(optionsFile); | ||
var jsonText = JsonSerializer.Serialize(new { AssemblyAI = ValidAssemblyAIOptions }); | ||
await File.WriteAllTextAsync(optionsFile, jsonText); | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddJsonFile(optionsFile, optional: false, reloadOnChange: true) | ||
.Build(); | ||
|
||
serviceCollection.AddSingleton<IConfiguration>(configuration); | ||
serviceCollection.AddAssemblyAIClient(); | ||
|
||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
ClientOptions updatedOptions = new() | ||
{ | ||
ApiKey = "UpdatedApiKey", | ||
BaseUrl = "https://api.updated.assemblyai.com", | ||
MaxRetries = 3, | ||
TimeoutInSeconds = 45 | ||
}; | ||
|
||
jsonText = JsonSerializer.Serialize(new { AssemblyAI = updatedOptions }); | ||
await File.WriteAllTextAsync(optionsFile, jsonText); | ||
|
||
// Simulate waiting for the option change to be detected | ||
await Task.Delay(1000); // This is a simplification. In real tests, use a more reliable method to wait for changes. | ||
|
||
var monitor = serviceProvider.GetRequiredService<IOptionsMonitor<ClientOptions>>(); | ||
var options = monitor.CurrentValue; | ||
|
||
var expectedJson = JsonSerializer.Serialize(updatedOptions); | ||
var actualJson = JsonSerializer.Serialize(options); | ||
Assert.That(actualJson, Is.EqualTo(expectedJson)); | ||
} | ||
|
||
private static IConfiguration BuildEmptyConfiguration() => new ConfigurationBuilder().Build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using NUnit.Framework; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace AssemblyAI.Test; | ||
|
||
[TestFixture] | ||
public class DiClientTests | ||
{ | ||
[Test] | ||
public void AssemblyAIClient_Should_Be_Correctly_Configured_From_DI() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new Dictionary<string, string> | ||
{ | ||
{ "AssemblyAI:ApiKey", "test_api_key" }, | ||
{ "AssemblyAI:BaseUrl", "https://api.test.assemblyai.com" }, | ||
{ "AssemblyAI:MaxRetries", "3" }, | ||
{ "AssemblyAI:TimeoutInSeconds", "60" } | ||
}!) | ||
.Build(); | ||
|
||
services.AddSingleton<IConfiguration>(configuration); | ||
services.AddAssemblyAIClient(); | ||
|
||
// Act | ||
var serviceProvider = services.BuildServiceProvider(); | ||
var client = serviceProvider.GetService<AssemblyAIClient>(); | ||
|
||
// Assert | ||
Assert.That(client, Is.Not.Null); | ||
Assert.That(client.Files, Is.Not.Null); | ||
Assert.That(client.Transcripts, Is.Not.Null); | ||
Assert.That(client.Realtime, Is.Not.Null); | ||
Assert.That(client.Lemur, Is.Not.Null); | ||
} | ||
|
||
[Test] | ||
public void AssemblyAIClient_Throws_Exception_When_Configuration_Missing() | ||
{ | ||
var services = new ServiceCollection(); | ||
var configuration = new ConfigurationBuilder().Build(); // Empty configuration | ||
|
||
services.AddSingleton<IConfiguration>(configuration); | ||
services.AddAssemblyAIClient(); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var exception = Assert.Throws<OptionsValidationException>(() => serviceProvider.GetService<AssemblyAIClient>()); | ||
Assert.That(exception.Message, Is.EqualTo("AssemblyAI:ApiKey is required.")); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using NUnit.Framework; | ||
|
||
namespace AssemblyAI.Test; | ||
|
||
[TestFixture] | ||
public class TranscriptTests | ||
{ | ||
[Test] | ||
public async Task TestTranscript() | ||
{ | ||
var client = new AssemblyAIClient(""); | ||
var transcript = await client.Transcripts.SubmitAsync(new TranscriptParams | ||
{ | ||
AudioUrl = "https://storage.googleapis.com/aai-docs-samples/nbc.mp3" | ||
}); | ||
Assert.That(transcript, Is.Not.Null); | ||
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Queued)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using NUnit.Framework; | ||
|
||
namespace AssemblyAI.Test; | ||
|
||
[TestFixture] | ||
public class UserAgentTests | ||
{ | ||
[Test] | ||
public void TestDefaultUserAgent() | ||
{ | ||
Assert.That(UserAgent.Default, Is.Not.Null); | ||
var userAgentString = UserAgent.Default.ToAssemblyAIUserAgentString(); | ||
Assert.That(userAgentString, Is.Not.Null); | ||
Assert.That(userAgentString, Is.Not.Empty); | ||
Assert.That(userAgentString, Does.StartWith("AssemblyAI/1.0 (")); | ||
Assert.That(userAgentString, Does.EndWith(")")); | ||
Assert.That(userAgentString, Does.Contain("sdk=CSharp/")); | ||
Assert.That(userAgentString, Does.Contain("runtime_env=.NET/8")); | ||
} | ||
|
||
|
||
[Test] | ||
public void TestMergeUserAgent() | ||
{ | ||
var userAgent = new UserAgent(UserAgent.Default, new UserAgent(new Dictionary<string, UserAgentItem?>() | ||
{ | ||
["integration"] = new("SemanticKernel", "1.0"), | ||
["runtime_env"] = null | ||
})); | ||
var userAgentString = userAgent.ToAssemblyAIUserAgentString(); | ||
Assert.That(userAgentString, Is.Not.Null); | ||
Assert.That(userAgentString, Is.Not.Empty); | ||
Assert.That(userAgentString, Does.StartWith("AssemblyAI/1.0 (")); | ||
Assert.That(userAgentString, Does.EndWith(")")); | ||
Assert.That(userAgentString, Does.Contain("sdk=CSharp/")); | ||
Assert.That(userAgentString, Does.Not.Contain("runtime_env")); | ||
Assert.That(userAgentString, Does.Contain("integration=SemanticKernel/1.0")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#nullable enable | ||
|
||
using System.Net.Http; | ||
using AssemblyAI.Core; | ||
|
||
namespace AssemblyAI; | ||
|
||
public class ClientOptions | ||
{ | ||
/// <summary> | ||
/// The AssemblyAI API key | ||
/// </summary> | ||
public required string ApiKey { get; set; } | ||
|
||
/// <summary> | ||
/// The Base URL for the API. | ||
/// </summary> | ||
public string BaseUrl { get; set; } = Environments.DEFAULT; | ||
|
||
private UserAgent? _userAgent = UserAgent.Default; | ||
|
||
/// <summary> | ||
/// The AssemblyAI user agent | ||
/// </summary> | ||
public UserAgent? UserAgent | ||
{ | ||
get => _userAgent; | ||
set | ||
{ | ||
if (value == null) | ||
{ | ||
_userAgent = null; | ||
return; | ||
} | ||
_userAgent = new UserAgent(UserAgent.Default, value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The http client used to make requests. | ||
/// </summary> | ||
public HttpClient? HttpClient { get; set; } | ||
|
||
/// <summary> | ||
/// The http client used to make requests. | ||
/// </summary> | ||
public int MaxRetries { get; set; } = 2; | ||
|
||
/// <summary> | ||
/// The timeout for the request in seconds. | ||
/// </summary> | ||
public int TimeoutInSeconds { get; set; } = 30; | ||
} |
Oops, something went wrong.