diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index fcbfcc3..75c677a 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -37,7 +37,15 @@ namespace Selector.CLI OptionsHelper.ConfigureOptions(options, context.Configuration); }); - return OptionsHelper.ConfigureOptions(context.Configuration); + var config = OptionsHelper.ConfigureOptions(context.Configuration); + + services.Configure(options => + { + options.ClientId = config.ClientId; + options.ClientSecret = config.ClientSecret; + }); + + return config; } public static void ConfigureLastFm(RootOptions config, IServiceCollection services) diff --git a/Selector.CLI/WatcherService.cs b/Selector.CLI/WatcherService.cs index 9ca5226..a403430 100644 --- a/Selector.CLI/WatcherService.cs +++ b/Selector.CLI/WatcherService.cs @@ -52,8 +52,6 @@ namespace Selector.CLI LastAuth = lastAuth; Cache = cache; Subscriber = subscriber; - - SpotifyFactory.Initialise(Config.ClientId, Config.ClientSecret); } public async Task StartAsync(CancellationToken cancellationToken) diff --git a/Selector.Tests/Spotify/Providers.cs b/Selector.Tests/Spotify/Providers.cs deleted file mode 100644 index 593a2ca..0000000 --- a/Selector.Tests/Spotify/Providers.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Xunit; -using Moq; -using FluentAssertions; -using SpotifyAPI.Web; - -using Selector; - -namespace Selector.Tests -{ - public class RefreshTokenFactoryProviderTests - { - [Fact] - public void Init() - { - var provider = new RefreshTokenFactoryProvider(); - - provider.Initialised.Should().BeFalse(); - - provider.Initialise("a", "b"); - - provider.Initialised.Should().BeTrue(); - - provider.Initialise("a", ""); - - provider.Initialised.Should().BeFalse(); - - provider.Initialise(null, "b"); - - provider.Initialised.Should().BeFalse(); - } - - [Fact] - public void Get() - { - var provider = new RefreshTokenFactoryProvider(); - - provider.Initialise("a", "b"); - - var consumer = provider.GetFactory("a"); - - consumer.Should().NotBeNull(); - consumer.Result.Should().NotBeNull(); - } - } -} diff --git a/Selector.Web/Services/SpotifyInitialiser.cs b/Selector.Web/Services/SpotifyInitialiser.cs deleted file mode 100644 index 5bf0c1b..0000000 --- a/Selector.Web/Services/SpotifyInitialiser.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace Selector.Web.Service { - public class SpotifyInitialiser : IHostedService - { - private readonly ILogger Logger; - private readonly IRefreshTokenFactoryProvider FactoryProvider; - private readonly RootOptions Config; - - public SpotifyInitialiser( - ILogger logger, - IRefreshTokenFactoryProvider factoryProvider, - IOptions config - ) - { - Logger = logger; - FactoryProvider = factoryProvider; - Config = config.Value; - } - - public Task StartAsync(CancellationToken cancellationToken) - { - Logger.LogInformation("Initialising Spotify Factory"); - - if(string.IsNullOrEmpty(Config.ClientId) || string.IsNullOrEmpty(Config.ClientSecret)) - { - Logger.LogError("Unable to initialise Spotify factory, null client id or secret"); - } - else - { - FactoryProvider.Initialise(Config.ClientId, Config.ClientSecret); - } - - return Task.CompletedTask; - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - } -} \ No newline at end of file diff --git a/Selector.Web/Startup.cs b/Selector.Web/Startup.cs index 0eaa067..0c5f5ae 100644 --- a/Selector.Web/Startup.cs +++ b/Selector.Web/Startup.cs @@ -41,6 +41,12 @@ namespace Selector.Web }); var config = OptionsHelper.ConfigureOptions(Configuration); + services.Configure(options => + { + options.ClientId = config.ClientId; + options.ClientSecret = config.ClientSecret; + }); + services.AddRazorPages().AddRazorRuntimeCompilation(); services.AddControllers(); services.AddSignalR(o => o.EnableDetailedErrors = true); @@ -117,7 +123,6 @@ namespace Selector.Web services.AddTransient(services => services.GetService().GetSubscriber()); } - services.AddHostedService(); services.AddSingleton(); services.AddSingleton(); diff --git a/Selector/Spotify/Credentials.cs b/Selector/Spotify/Credentials.cs new file mode 100644 index 0000000..1be4ed6 --- /dev/null +++ b/Selector/Spotify/Credentials.cs @@ -0,0 +1,14 @@ +namespace Selector { + public class SpotifyAppCredentials { + public string ClientId { get; set; } + public string ClientSecret { get; set; } + + public SpotifyAppCredentials() { } + + public SpotifyAppCredentials(string clientId, string clientSecret) + { + ClientId = clientId; + ClientSecret = clientSecret; + } + } +} \ No newline at end of file diff --git a/Selector/Spotify/FactoryProvider/CachingRefreshTokenFactoryProvider.cs b/Selector/Spotify/FactoryProvider/CachingRefreshTokenFactoryProvider.cs index 07648ce..ce4707f 100644 --- a/Selector/Spotify/FactoryProvider/CachingRefreshTokenFactoryProvider.cs +++ b/Selector/Spotify/FactoryProvider/CachingRefreshTokenFactoryProvider.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using SpotifyAPI.Web; @@ -11,7 +12,7 @@ namespace Selector { protected readonly ILogger Logger; - public CachingRefreshTokenFactoryProvider(ILogger logger) + public CachingRefreshTokenFactoryProvider(IOptions credentials, ILogger logger) : base(credentials) { Logger = logger; } diff --git a/Selector/Spotify/FactoryProvider/IRefreshTokenFactoryProvider.cs b/Selector/Spotify/FactoryProvider/IRefreshTokenFactoryProvider.cs index 11ba334..c4b9b5d 100644 --- a/Selector/Spotify/FactoryProvider/IRefreshTokenFactoryProvider.cs +++ b/Selector/Spotify/FactoryProvider/IRefreshTokenFactoryProvider.cs @@ -4,8 +4,6 @@ namespace Selector { public interface IRefreshTokenFactoryProvider { - public void Initialise(string clientId, string clientSecret); - public bool Initialised { get; } public Task GetFactory(string refreshToken); } } diff --git a/Selector/Spotify/FactoryProvider/RefreshTokenFactoryProvider.cs b/Selector/Spotify/FactoryProvider/RefreshTokenFactoryProvider.cs index 46ccf5f..b8d5c06 100644 --- a/Selector/Spotify/FactoryProvider/RefreshTokenFactoryProvider.cs +++ b/Selector/Spotify/FactoryProvider/RefreshTokenFactoryProvider.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using SpotifyAPI.Web; @@ -12,22 +13,18 @@ namespace Selector /// public class RefreshTokenFactoryProvider : IRefreshTokenFactoryProvider { - protected string ClientId { get; set; } - protected string ClientSecret { get; set; } + protected readonly SpotifyAppCredentials Credentials; - public void Initialise(string clientId, string clientSecret){ - ClientId = clientId; - ClientSecret = clientSecret; + public RefreshTokenFactoryProvider(IOptions credentials) + { + Credentials = credentials.Value; } - public bool Initialised => !string.IsNullOrWhiteSpace(ClientId) && !string.IsNullOrWhiteSpace(ClientSecret); - public virtual Task GetFactory(string refreshToken) { - if(!Initialised) throw new InvalidOperationException("Factory not initialised"); if(string.IsNullOrEmpty(refreshToken)) throw new ArgumentException("Null or empty refresh key provided"); - return Task.FromResult(new RefreshTokenFactory(ClientId, ClientSecret, refreshToken)); + return Task.FromResult(new RefreshTokenFactory(Credentials.ClientId, Credentials.ClientSecret, refreshToken)); } } }