From 992811ccad0aaebc505d6f2afb62b6f3bf3c7f10 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Tue, 27 May 2025 17:10:57 -0500 Subject: [PATCH] refactor: extract to handler --- .../Handlers/NotificationHandler.cs | 51 ++++++++++++++++++ .../VerifySubscriptionHandler.cs | 2 +- src/src/StevesBot.Webhook/Program.cs | 54 +------------------ src/src/StevesBot.Webhook/Usings.cs | 2 + 4 files changed, 56 insertions(+), 53 deletions(-) create mode 100644 src/src/StevesBot.Webhook/Handlers/NotificationHandler.cs rename src/src/StevesBot.Webhook/{YouTube => Handlers}/VerifySubscriptionHandler.cs (98%) diff --git a/src/src/StevesBot.Webhook/Handlers/NotificationHandler.cs b/src/src/StevesBot.Webhook/Handlers/NotificationHandler.cs new file mode 100644 index 0000000..ba1cb5a --- /dev/null +++ b/src/src/StevesBot.Webhook/Handlers/NotificationHandler.cs @@ -0,0 +1,51 @@ +namespace StevesBot.Webhook.Handlers; + +// TODO: Implement logic to do the following: +// - if is stream create discord message +internal static class NotificationHandler +{ + public static async Task HandleAsync( + HttpContext context, + [FromServices] ILogger logger, + [FromServices] IYouTubeDataApiClient youTubeDataApiClient + ) + { + using StreamReader stream = new(context.Request.Body); + var body = await stream.ReadToEndAsync(); + + var videoIdRegex = VideoIdRegex.Regex(); + var match = videoIdRegex.Match(body); + + if (match.Success is false) + { + logger.LogWarning("No video ID found in the request body."); + return Results.BadRequest("No video ID found in the request body."); + } + + var videoId = match.Groups[1].Value; + var parts = new string[] { "liveStreamingDetails", "snippet" }; + var video = await youTubeDataApiClient.GetVideoByIdAsync(videoId, parts); + + if (video is null) + { + logger.LogWarning("Video with ID {VideoId} not found.", videoId); + return Results.NotFound(); + } + + if (video.IsLiveStream is false) + { + logger.LogInformation("Video ID {VideoId} is not a live stream.", videoId); + return Results.Ok(); + } + + logger.LogInformation("Video ID {VideoId} is a live stream.", videoId); + + return Results.Ok(); + } +} + +internal static partial class VideoIdRegex +{ + [GeneratedRegex(@"(.*?)")] + public static partial Regex Regex(); +} \ No newline at end of file diff --git a/src/src/StevesBot.Webhook/YouTube/VerifySubscriptionHandler.cs b/src/src/StevesBot.Webhook/Handlers/VerifySubscriptionHandler.cs similarity index 98% rename from src/src/StevesBot.Webhook/YouTube/VerifySubscriptionHandler.cs rename to src/src/StevesBot.Webhook/Handlers/VerifySubscriptionHandler.cs index 0286856..14282bf 100644 --- a/src/src/StevesBot.Webhook/YouTube/VerifySubscriptionHandler.cs +++ b/src/src/StevesBot.Webhook/Handlers/VerifySubscriptionHandler.cs @@ -1,4 +1,4 @@ -namespace StevesBot.Webhook.YouTube; +namespace StevesBot.Webhook.Handlers; internal static class VerifySubscriptionHandler { diff --git a/src/src/StevesBot.Webhook/Program.cs b/src/src/StevesBot.Webhook/Program.cs index 29155f0..c768d10 100644 --- a/src/src/StevesBot.Webhook/Program.cs +++ b/src/src/StevesBot.Webhook/Program.cs @@ -1,11 +1,5 @@ -using System.Text.RegularExpressions; - var builder = WebApplication.CreateBuilder(args); -builder.Services.Configure( - static options => options.ShutdownTimeout = TimeSpan.FromSeconds(30) -); - builder.Services .AddOptionsWithValidateOnStart() .BindConfiguration(nameof(SubscriptionOptions)) @@ -54,52 +48,8 @@ if (app.Environment.IsDevelopment()) app.MapOpenApi(); } -// app.UseHttpsRedirection(); - const string ytCallback = "yt-callback"; - app.MapGet(ytCallback, VerifySubscriptionHandler.HandleAsync); +app.MapPost(ytCallback, NotificationHandler.HandleAsync); -// TODO: Implement logic to do the following: -// - if is stream create discord message -app.MapPost(ytCallback, static async (HttpContext context, [FromServices] ILogger logger, [FromServices] IYouTubeDataApiClient youTubeDataApiClient) => -{ - var body = ""; - using StreamReader stream = new(context.Request.Body); - body = await stream.ReadToEndAsync(); - - var videoIdRegex = VideoIdRegex(); - var match = videoIdRegex.Match(body); - - if (match.Success is false) - { - logger.LogWarning("No video ID found in the request body."); - } - - var videoId = match.Groups[1].Value; - var video = await youTubeDataApiClient.GetVideoByIdAsync(videoId, ["liveStreamingDetails"]); - - if (video is null) - { - logger.LogWarning("Video with ID {VideoId} not found.", videoId); - return Results.NotFound(); - } - - if (video.IsLiveStream is false) - { - logger.LogInformation("Video ID {VideoId} is not a live stream.", videoId); - return Results.Ok(); - } - - logger.LogInformation("Video ID {VideoId} is a live stream.", videoId); - - return Results.Ok(); -}); - -app.Run(); - -internal partial class Program -{ - [GeneratedRegex(@"(.*?)")] - private static partial Regex VideoIdRegex(); -} \ No newline at end of file +app.Run(); \ No newline at end of file diff --git a/src/src/StevesBot.Webhook/Usings.cs b/src/src/StevesBot.Webhook/Usings.cs index 73e000f..0346b11 100644 --- a/src/src/StevesBot.Webhook/Usings.cs +++ b/src/src/StevesBot.Webhook/Usings.cs @@ -1,10 +1,12 @@ global using System.Collections.Concurrent; global using System.ComponentModel.DataAnnotations; global using System.Text.Json.Serialization; +global using System.Text.RegularExpressions; global using Microsoft.AspNetCore.Mvc; global using Microsoft.Extensions.Options; +global using StevesBot.Webhook.Handlers; global using StevesBot.Webhook.YouTube; global using StevesBot.Webhook.YouTube.Data; global using StevesBot.Webhook.YouTube.Tasks; \ No newline at end of file