feat: begin adding subscription to YT notifications

This commit is contained in:
Stevan Freeborn
2025-05-26 11:56:43 -05:00
parent a8de49cee3
commit a1ef35d0a7
9 changed files with 149 additions and 1 deletions
+12 -1
View File
@@ -1,7 +1,18 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddOptionsWithValidateOnStart<SubscriptionOptions>()
.BindConfiguration(nameof(SubscriptionOptions))
.ValidateDataAnnotations();
builder.Services
.AddHttpClient<IPubSubClient, PubSubClient>()
.AddStandardResilienceHandler();
builder.Services.AddOpenApi();
builder.Services.AddHostedService<SubscriptionWorker>();
var app = builder.Build();
if (app.Environment.IsDevelopment())
@@ -11,4 +22,4 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection();
app.Run();
app.Run();
@@ -8,6 +8,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
</ItemGroup>
</Project>
@@ -0,0 +1,49 @@
namespace StevesBot.Webhook;
internal sealed class SubscriptionWorker(
ILogger<SubscriptionWorker> logger,
IOptions<SubscriptionOptions> options,
IPubSubClient pubSubClient
) : IHostedLifecycleService
{
private readonly ILogger<SubscriptionWorker> _logger = logger;
private readonly SubscriptionOptions _options = options.Value;
private readonly IPubSubClient _pubSubClient = pubSubClient;
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StartingAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public async Task StartedAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Subscribing to notifications");
await _pubSubClient.SubscribeAsync(
_options.CallbackBaseUrl,
_options.TopicUrl,
cancellationToken
);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StoppingAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StoppedAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
+6
View File
@@ -0,0 +1,6 @@
global using System.ComponentModel.DataAnnotations;
global using Microsoft.Extensions.Options;
global using StevesBot.Webhook;
global using StevesBot.Webhook.YouTube;
@@ -0,0 +1,16 @@
namespace StevesBot.Webhook.YouTube;
internal interface IPubSubClient
{
Task SubscribeAsync(
string callbackUrl,
string topicUrl,
CancellationToken cancellationToken = default
);
Task UnsubscribeAsync(
string callbackUrl,
string topicUrl,
CancellationToken cancellationToken = default
);
}
@@ -0,0 +1,32 @@
namespace StevesBot.Webhook.YouTube;
internal sealed class PubSubClient(HttpClient httpClient) : IPubSubClient
{
private const string SubscribeEndpoint = "subscribe";
private readonly HttpClient _httpClient = httpClient;
public async Task SubscribeAsync(string callbackUrl, string topicUrl, CancellationToken cancellationToken = default)
{
var uri = new Uri(SubscribeEndpoint, UriKind.Relative);
var formFields = new Dictionary<string, string>()
{
{ "hub.callback", callbackUrl },
{ "hub.topic", topicUrl },
{ "hub.verify", "async" },
{ "hub.mode", "subscribe" }
};
using var form = new FormUrlEncodedContent(formFields);
var response = await _httpClient.PostAsync(uri, form, cancellationToken);
if (response.IsSuccessStatusCode is false)
{
throw new PubSubClientException("Failed to subscribe");
}
}
public Task UnsubscribeAsync(string callbackUrl, string topicUrl, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,16 @@
namespace StevesBot.Webhook.YouTube;
internal sealed class PubSubClientException : Exception
{
public PubSubClientException()
{
}
public PubSubClientException(string message) : base(message)
{
}
public PubSubClientException(string message, Exception innerException) : base(message, innerException)
{
}
}
@@ -0,0 +1,10 @@
namespace StevesBot.Webhook.YouTube;
internal sealed record SubscriptionOptions
{
[Required]
public string CallbackBaseUrl { get; init; } = string.Empty;
[Required]
public string TopicUrl { get; init; } = string.Empty;
}
@@ -0,0 +1,6 @@
{
"SubscriptionOptions": {
"CallbackUrl": "CallbackUrl",
"TopicUrl": "TopicUrl"
}
}