injecting spotify app credentials, closes #7

This commit is contained in:
andy 2021-11-11 08:11:55 +00:00
parent 3564ce7d6d
commit bfb2a5d2cd
9 changed files with 37 additions and 113 deletions

View File

@ -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<SpotifyAppCredentials>(options =>
{
options.ClientId = config.ClientId;
options.ClientSecret = config.ClientSecret;
});
return config;
}
public static void ConfigureLastFm(RootOptions config, IServiceCollection services)

View File

@ -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)

View File

@ -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();
}
}
}

View File

@ -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<SpotifyInitialiser> Logger;
private readonly IRefreshTokenFactoryProvider FactoryProvider;
private readonly RootOptions Config;
public SpotifyInitialiser(
ILogger<SpotifyInitialiser> logger,
IRefreshTokenFactoryProvider factoryProvider,
IOptions<RootOptions> 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;
}
}
}

View File

@ -41,6 +41,12 @@ namespace Selector.Web
});
var config = OptionsHelper.ConfigureOptions(Configuration);
services.Configure<SpotifyAppCredentials>(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<ISubscriber>(services => services.GetService<ConnectionMultiplexer>().GetSubscriber());
}
services.AddHostedService<SpotifyInitialiser>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
services.AddSingleton<AudioFeaturePuller>();

View File

@ -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;
}
}
}

View File

@ -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<CachingRefreshTokenFactoryProvider> Logger;
public CachingRefreshTokenFactoryProvider(ILogger<CachingRefreshTokenFactoryProvider> logger)
public CachingRefreshTokenFactoryProvider(IOptions<SpotifyAppCredentials> credentials, ILogger<CachingRefreshTokenFactoryProvider> logger) : base(credentials)
{
Logger = logger;
}

View File

@ -4,8 +4,6 @@ namespace Selector
{
public interface IRefreshTokenFactoryProvider
{
public void Initialise(string clientId, string clientSecret);
public bool Initialised { get; }
public Task<RefreshTokenFactory> GetFactory(string refreshToken);
}
}

View File

@ -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
/// </summary>
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<SpotifyAppCredentials> credentials)
{
Credentials = credentials.Value;
}
public bool Initialised => !string.IsNullOrWhiteSpace(ClientId) && !string.IsNullOrWhiteSpace(ClientSecret);
public virtual Task<RefreshTokenFactory> 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));
}
}
}