2022-08-03 22:51:39 +01:00
|
|
|
|
using System;
|
2023-10-12 21:17:32 +01:00
|
|
|
|
using System.Linq;
|
2022-08-03 22:51:39 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CloudNative.CloudEvents;
|
|
|
|
|
using Google.Cloud.Functions.Framework;
|
2024-01-21 11:12:23 +00:00
|
|
|
|
using Google.Cloud.Functions.Hosting;
|
2022-08-03 22:51:39 +01:00
|
|
|
|
using Google.Events.Protobuf.Cloud.PubSub.V1;
|
2024-01-21 11:12:23 +00:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2022-08-03 22:51:39 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-10-12 23:17:50 +01:00
|
|
|
|
using Mixonomer.Fire;
|
2024-01-23 17:43:25 +00:00
|
|
|
|
using Mixonomer.Playlist;
|
2024-01-21 20:43:46 +00:00
|
|
|
|
using SpotifyAPI.Web;
|
2022-08-03 22:51:39 +01:00
|
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
|
namespace Mixonomer.Func;
|
|
|
|
|
|
2024-01-21 11:12:23 +00:00
|
|
|
|
[FunctionsStartup(typeof(RunUserPlaylistStartup))]
|
2024-01-18 22:30:34 +00:00
|
|
|
|
public class RunUserPlaylist : ICloudEventFunction<MessagePublishedData>
|
2022-08-03 22:51:39 +01:00
|
|
|
|
{
|
2024-01-18 22:30:34 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2024-01-21 20:43:46 +00:00
|
|
|
|
private readonly UserRepo _userRepo;
|
|
|
|
|
private readonly SpotifyNetworkProvider _spotifyMetworkProvider;
|
2022-08-03 22:51:39 +01:00
|
|
|
|
|
2024-01-21 20:43:46 +00:00
|
|
|
|
public RunUserPlaylist(ILogger<RunUserPlaylist> logger, UserRepo userRepo, SpotifyNetworkProvider spotifyMetworkProvider)
|
2024-01-18 22:30:34 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2024-01-21 20:43:46 +00:00
|
|
|
|
_userRepo = userRepo;
|
|
|
|
|
_spotifyMetworkProvider = spotifyMetworkProvider;
|
2024-01-18 22:30:34 +00:00
|
|
|
|
}
|
2022-08-03 22:51:39 +01:00
|
|
|
|
|
|
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
|
public async Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Received message in C# {data.Message}, {cloudEvent.GetPopulatedAttributes()}");
|
2022-08-03 22:51:39 +01:00
|
|
|
|
|
2024-01-21 20:43:46 +00:00
|
|
|
|
var user = await _userRepo.GetUser(data.Message.Attributes["username"]);
|
2023-10-12 23:17:50 +01:00
|
|
|
|
|
2024-01-21 20:43:46 +00:00
|
|
|
|
var spotifyConfig = await _spotifyMetworkProvider.GetUserConfig(user);
|
|
|
|
|
var spotifyClient = new SpotifyClient(spotifyConfig);
|
2023-10-12 23:17:50 +01:00
|
|
|
|
|
2024-01-18 22:30:34 +00:00
|
|
|
|
_logger.LogInformation($"{user.username} was last refreshed at {user.last_refreshed}");
|
2022-08-03 22:51:39 +01:00
|
|
|
|
}
|
2024-01-21 11:12:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RunUserPlaylistStartup : FunctionsStartup
|
|
|
|
|
{
|
|
|
|
|
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
base.ConfigureServices(context, services);
|
|
|
|
|
|
|
|
|
|
services.AddSecretManagerServiceClient();
|
2024-01-21 20:43:46 +00:00
|
|
|
|
// services.AddFirestoreClient();
|
|
|
|
|
|
|
|
|
|
services.AddTransient<SpotifyNetworkProvider>()
|
|
|
|
|
.AddTransient<PlaylistGenerator>()
|
2024-06-14 08:07:56 +01:00
|
|
|
|
.AddTransient<PartTreeWalker>()
|
2024-01-21 20:43:46 +00:00
|
|
|
|
.AddSingleton<UserRepo>();
|
2024-01-21 11:12:23 +00:00
|
|
|
|
}
|
2024-01-18 22:30:34 +00:00
|
|
|
|
}
|