Skip to content

Commit

Permalink
Added new using directives in ExtensionMethods.cs for improved func…
Browse files Browse the repository at this point in the history
…tionality and clarity.

Modified webhook handling logic to deserialize the request body into an `OttWebhook` object and check if the topic is in the `attribute.Topics`, invoking the method if it matches, or returning a 400 Bad Request status otherwise.
  • Loading branch information
Lulalaby committed Dec 19, 2024
1 parent 10d9f55 commit 618a930
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
20 changes: 5 additions & 15 deletions AITSYS.Vimeo.Ott/Attributes/VimeoOttWebhookAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
// Copyright 2025 Aiko IT Systems. See https://github.com/Aiko-IT-Systems/AITSYS.Vimeo.OTT/blob/main/LICENSE.md for the license.

namespace AITSYS.Vimeo.Ott.Attributes;

/// <summary>
/// Marks a method to handle a topic for incoming ott webhooks.
/// Marks a method to handle one or more topics for incoming OTT webhooks.
/// </summary>
/// <param name="topics">The topics to handle.</param>
[AttributeUsage(AttributeTargets.Method)]
public sealed class VimeoOttWebhookAttribute : Attribute
public sealed class VimeoOttWebhookAttribute(params string[] topics) : Attribute
{
/// <summary>
/// Marks a method to handle a topic for incoming ott webhooks.
/// </summary>
/// <param name="topic">The topic to handle.</param>
public VimeoOttWebhookAttribute(string topic)
{
this.Topic = topic;
}

/// <summary>
/// Gets the webhooks topic.
/// Gets the webhooks topics.
/// </summary>
public string Topic { get; }
public IEnumerable<string> Topics { get; } = topics;
}
21 changes: 15 additions & 6 deletions AITSYS.Vimeo.Ott/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// Copyright 2025 Aiko IT Systems. See https://github.com/Aiko-IT-Systems/AITSYS.Vimeo.OTT/blob/main/LICENSE.md for the license.

using System.Reflection;

using AITSYS.Vimeo.Ott.Attributes;
using AITSYS.Vimeo.Ott.Clients;
using AITSYS.Vimeo.Ott.Entities;
using AITSYS.Vimeo.Ott.Logging;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;

using Newtonsoft.Json;

namespace AITSYS.Vimeo.Ott;

/// <summary>
/// Represents various extension methods.
/// Represents various extension methods.
/// </summary>
public static class ExtensionMethods
{
Expand Down Expand Up @@ -61,9 +63,16 @@ public static IEndpointRouteBuilder MapVimeoOttWebhook(this IEndpointRouteBuilde
if (attribute != null)
endpoints.MapPost(pattern, async context =>
{
var instance = Activator.CreateInstance(method.DeclaringType);
var parameters = method.GetParameters().Select(p => context.RequestServices.GetService(p.ParameterType)).ToArray();
await (Task)method.Invoke(instance, parameters);
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
var webhook = JsonConvert.DeserializeObject<OttWebhook>(body);
if (webhook != null && attribute.Topics.Contains(webhook.Topic))
{
var instance = Activator.CreateInstance(method.DeclaringType!);
var parameters = method.GetParameters().Select(p => context.RequestServices.GetService(p.ParameterType)).ToArray();
await (Task)method.Invoke(instance, parameters)!;
}
else
context.Response.StatusCode = StatusCodes.Status400BadRequest;
})
.RequireAuthorization(new AuthorizeAttribute
{
Expand Down

0 comments on commit 618a930

Please sign in to comment.