-
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.
Merge pull request #32 from AssemblyAI/niels/di-extensions
Add dependency injection extensions
- Loading branch information
Showing
9 changed files
with
284 additions
and
24 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 |
---|---|---|
|
@@ -476,4 +476,5 @@ $RECYCLE.BIN/ | |
# Windows shortcuts | ||
*.lnk | ||
|
||
.idea | ||
.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 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
Oops, something went wrong.