added IWatcherFactory, watcher options

This commit is contained in:
andy 2021-10-11 23:19:19 +01:00
parent e7fbb23376
commit 539143ad5e
11 changed files with 195 additions and 34 deletions

View File

@ -1,10 +1,50 @@

using System.Collections.Generic;
namespace Selector.CLI
{
class RootOptions
{
public const string Key = "Selector";
public int Number { get; set; } = 2;
/// <summary>
/// Spotify client ID
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// Spotify app secret
/// </summary>
public string ClientSecret { get; set; }
public WatcherOptions WatcherOptions { get; set; } = new();
public EqualityChecker Equality { get; set; } = EqualityChecker.Uri;
}
enum EqualityChecker
{
Uri, String
}
class WatcherOptions
{
public const string Key = "Watcher";
public List<WatcherInstanceOptions> Instances { get; set; } = new();
}
class WatcherInstanceOptions
{
public const string Key = "Instances";
public string Name { get; set; }
public string AccessKey { get; set; }
public string RefreshKey { get; set; }
public int PollPeriod { get; set; } = 5000;
public WatcherType Type { get; set; } = WatcherType.Player;
public string? PlaylistUri { get; set; }
}
enum WatcherType
{
Player, Playlist
}
}

View File

@ -21,14 +21,24 @@ namespace Selector.CLI
.ConfigureServices((context, services) => {
// CONFIG
services.Configure<RootOptions>(options =>
context.Configuration.GetSection(RootOptions.Key).Bind(options)
);
services.Configure<RootOptions>(options => {
context.Configuration.GetSection(RootOptions.Key).Bind(options);
context.Configuration.GetSection($"{RootOptions.Key}:{WatcherOptions.Key}").Bind(options.WatcherOptions);
});
// SERVICES
services.AddTransient<IPlayerWatcher, PlayerWatcher>();
services.AddTransient<IWatcherCollection, WatcherCollection>();
services.AddTransient<IEqual, UriEqual>();
//services.AddTransient<IWatcherFactory, PlayerWatcher>();
//services.AddTransient<IWatcherCollection, WatcherCollection>();
switch(context.Configuration.GetValue<EqualityChecker>("selector:equality"))
{
case EqualityChecker.Uri:
services.AddTransient<IEqual, UriEqual>();
break;
case EqualityChecker.String:
services.AddTransient<IEqual, StringEqual>();
break;
}
// HOSTED SERVICES
services.AddHostedService<WatcherService>();

View File

@ -16,6 +16,8 @@ namespace Selector.CLI
private readonly ILogger<WatcherService> Logger;
private readonly RootOptions Config;
private Dictionary<string, IWatcherCollection> Watchers { get; set; } = new();
public WatcherService(ILogger<WatcherService> logger, IOptions<RootOptions> config)
{
Logger = logger;
@ -26,12 +28,21 @@ namespace Selector.CLI
{
Logger.LogInformation("Starting up");
Config.WatcherOptions.Instances.ForEach(i => Logger.LogInformation($"Config: {i.Type}"));
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("Shutting down");
foreach((var key, var watcher) in Watchers)
{
Logger.LogInformation($"Stopping watcher collection: {key}");
watcher.Stop();
}
return Task.CompletedTask;
}
}

View File

@ -1,8 +1,21 @@
{
//"Selector": {
// "Number2": 4,
// "Number": 4
//},
"Selector": {
"ClientId": "",
"ClientSecret" "",
"Equality": "uri",
"Watcher": {
"Instances": [
{
"name": "sarsoo",
"type": "player"
},
{
"name": "andy",
"type": "playlist"
}
]
}
},
"Logging": {
"LogLevel": {
"Default": "Information"

View File

@ -62,7 +62,7 @@ namespace Selector.Tests
[MemberData(nameof(TrackData))]
public void TrackEquality(FullTrack track1, FullTrack track2, bool shouldEqual)
{
var eq = Equal.String();
var eq = new StringEqual();
eq.IsEqual(track1, track2).Should().Be(shouldEqual);
}
@ -105,7 +105,7 @@ namespace Selector.Tests
[MemberData(nameof(AlbumData))]
public void AlbumEquality(SimpleAlbum album1, SimpleAlbum album2, bool shouldEqual)
{
var eq = Equal.String();
var eq = new StringEqual();
eq.IsEqual(album1, album2).Should().Be(shouldEqual);
}
@ -130,7 +130,7 @@ namespace Selector.Tests
[MemberData(nameof(ArtistData))]
public void ArtistEquality(SimpleArtist artist1, SimpleArtist artist2, bool shouldEqual)
{
var eq = Equal.String();
var eq = new StringEqual();
eq.IsEqual(artist1, artist2).Should().Be(shouldEqual);
}
@ -155,7 +155,7 @@ namespace Selector.Tests
[MemberData(nameof(EpisodeData))]
public void EpisodeEquality(FullEpisode episode1, FullEpisode episode2, bool shouldEqual)
{
var eq = Equal.String();
var eq = new StringEqual();
eq.IsEqual(episode1, episode2).Should().Be(shouldEqual);
}
}

View File

@ -7,25 +7,7 @@ namespace Selector
{
public class Equal : IEqual
{
private Dictionary<Type, object> comps;
public static Equal String() {
return new Equal(){
comps = new Dictionary<Type, object>(){
{typeof(FullTrack), new FullTrackStringComparer()},
{typeof(FullEpisode), new FullEpisodeStringComparer()},
{typeof(FullAlbum), new FullAlbumStringComparer()},
{typeof(FullShow), new FullShowStringComparer()},
{typeof(FullArtist), new FullArtistStringComparer()},
{typeof(SimpleTrack), new SimpleTrackStringComparer()},
{typeof(SimpleEpisode), new SimpleEpisodeStringComparer()},
{typeof(SimpleAlbum), new SimpleAlbumStringComparer()},
{typeof(SimpleShow), new SimpleShowStringComparer()},
{typeof(SimpleArtist), new SimpleArtistStringComparer()},
}
};
}
protected Dictionary<Type, object> comps;
public bool IsEqual<T>(T item, T other)
{

View File

@ -0,0 +1,25 @@
using SpotifyAPI.Web;
namespace Selector
{
public class StringEqual: Equal
{
public StringEqual()
{
comps = new()
{
{ typeof(FullTrack), new FullTrackStringComparer() },
{ typeof(FullEpisode), new FullEpisodeStringComparer() },
{ typeof(FullAlbum), new FullAlbumStringComparer() },
{ typeof(FullShow), new FullShowStringComparer() },
{ typeof(FullArtist), new FullArtistStringComparer() },
{ typeof(SimpleTrack), new SimpleTrackStringComparer() },
{ typeof(SimpleEpisode), new SimpleEpisodeStringComparer() },
{ typeof(SimpleAlbum), new SimpleAlbumStringComparer() },
{ typeof(SimpleShow), new SimpleShowStringComparer() },
{ typeof(SimpleArtist), new SimpleArtistStringComparer() },
};
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using SpotifyAPI.Web;
namespace Selector
{
public interface ISpotifyClientFactory
{
public Task<SpotifyClientConfig> GetConfig();
}
}

View File

@ -0,0 +1,42 @@
using System.Threading.Tasks;
using SpotifyAPI.Web;
namespace Selector
{
/// <summary>
/// Get config from a refresh token
/// </summary>
public class RefreshTokenFactory : ISpotifyClientFactory
{
private string ClientId { get; set; }
private string ClientSecret { get; set; }
private string RefreshToken { get; set; }
public RefreshTokenFactory(string clientId, string clientSecret, string refreshToken) {
ClientId = clientId;
ClientSecret = clientSecret;
RefreshToken = refreshToken;
}
public async Task<SpotifyClientConfig> GetConfig()
{
var refreshed = await new OAuthClient()
.RequestToken(new AuthorizationCodeRefreshRequest(ClientId, ClientSecret, RefreshToken));
var config = SpotifyClientConfig
.CreateDefault()
.WithAuthenticator(new AuthorizationCodeAuthenticator(ClientId, ClientSecret, new(){
AccessToken = refreshed.AccessToken,
TokenType = refreshed.TokenType,
ExpiresIn = refreshed.ExpiresIn,
Scope = refreshed.Scope,
RefreshToken = refreshed.RefreshToken,
CreatedAt = refreshed.CreatedAt
}));
return config;
}
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Selector
{
public interface IWatcherFactory
{
public IWatcher Create();
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Selector
{
//class PlayerWatcherFactory : IWatcherFactory
//{
// public IWatcher Create()
// {
// }
//}
}