Skip to content

Commit

Permalink
Enhance Vimeo OTT project with new features and attributes
Browse files Browse the repository at this point in the history
Updated project file to include new ASP.NET Core package references for improved web utilities, authorization, and OpenAPI support.

Added multiple properties to the `OttCustomer` class for better customer data handling.

Introduced `MapVimeoOttWebhook` method in `ExtensionMethods.cs` for enhanced routing of webhook events.

Created `VimeoOttWebhookAttribute` to mark methods handling OTT webhooks, facilitating structured event management.

Added `OttWebhook` class to represent webhook events from Vimeo, including properties for embedded data and event details.
  • Loading branch information
Lulalaby committed Dec 19, 2024
1 parent 054e7ad commit 354d763
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
4 changes: 4 additions & 0 deletions AITSYS.Vimeo.Ott/AITSYS.Vimeo.Ott.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions AITSYS.Vimeo.Ott/Attributes/VimeoOttWebhookAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace AITSYS.Vimeo.Ott.Attributes;

/// <summary>
/// Marks a method to handle a topic for incoming ott webhooks.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class VimeoOttWebhookAttribute : 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.
/// </summary>
public string Topic { get; }
}
72 changes: 72 additions & 0 deletions AITSYS.Vimeo.Ott/Entities/Customers/OttCustomer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,78 @@ public sealed class OttCustomer<TOttEmbedded> : OttIdObject<OttCustomerLinks, TO
[JsonProperty("subscribed_to_site")]
public bool SubscribedToSite { get; internal set; }

/// <summary>
/// The campaign associated with the customer.
/// </summary>
[JsonProperty("campaign", NullValueHandling = NullValueHandling.Ignore)]
public string? Campaign { get; set; }

/// <summary>
/// The coupon code used by the customer.
/// </summary>
[JsonProperty("coupon_code", NullValueHandling = NullValueHandling.Ignore)]
public string? CouponCode { get; set; }

/// <summary>
/// The first name of the customer.
/// </summary>
[JsonProperty("first_name", NullValueHandling = NullValueHandling.Ignore)]
public string FirstName { get; set; }

/// <summary>
/// The last name of the customer.
/// </summary>
[JsonProperty("last_name", NullValueHandling = NullValueHandling.Ignore)]
public string LastName { get; set; }

/// <summary>
/// The date of the last payment made by the customer.
/// </summary>
[JsonProperty("last_payment_date", NullValueHandling = NullValueHandling.Ignore)]
public DateTime? LastPaymentDate { get; set; }

/// <summary>
/// The date of the next payment due from the customer.
/// </summary>
[JsonProperty("next_payment_date", NullValueHandling = NullValueHandling.Ignore)]
public DateTime? NextPaymentDate { get; set; }

/// <summary>
/// The end date of the customer's subscription pause.
/// </summary>
[JsonProperty("pause_end_date", NullValueHandling = NullValueHandling.Ignore)]
public DateTime? PauseEndDate { get; set; }

/// <summary>
/// The promotion code used by the customer.
/// </summary>
[JsonProperty("promotion_code", NullValueHandling = NullValueHandling.Ignore)]
public string? PromotionCode { get; set; }

/// <summary>
/// The referrer of the customer.
/// </summary>
[JsonProperty("referrer", NullValueHandling = NullValueHandling.Ignore)]
public string? Referrer { get; set; }

/// <summary>
/// The subscription frequency of the customer.
/// </summary>
[JsonProperty("subscription_frequency", NullValueHandling = NullValueHandling.Ignore)]
public string? SubscriptionFrequency { get; set; }

/// <summary>
/// The subscription price of the customer.
/// </summary>
[JsonProperty("subscription_price", NullValueHandling = NullValueHandling.Ignore)]
public int? SubscriptionPrice { get; set; }

/// <summary>
/// The subscription status of the customer.
/// </summary>
[JsonProperty("subscription_status", NullValueHandling = NullValueHandling.Ignore)]
public string SubscriptionStatus { get; set; }

/// <summary>
/// The customers notification settings.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions AITSYS.Vimeo.Ott/Entities/OttWebhook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using AITSYS.Vimeo.Ott.Entities.EmbeddedData;

using Newtonsoft.Json;

namespace AITSYS.Vimeo.Ott.Entities;

/// <summary>
/// Represents a webhook event from Vimeo OTT.
/// </summary>
public sealed class OttWebhook
{
/// <summary>
/// Gets the embedded data related to the webhook event.
/// </summary>
[JsonProperty("_embedded", NullValueHandling = NullValueHandling.Ignore)]
public OttCustomerProductEmbeddedData Embedded { get; set; }

/// <summary>
/// Gets the topic of the webhook event.
/// </summary>
[JsonProperty("topic", NullValueHandling = NullValueHandling.Ignore)]
public string Topic { get; set; }

/// <summary>
/// Gets the date and time when the webhook event was created.
/// </summary>
[JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
public DateTime? CreatedAt { get; set; }
}
44 changes: 43 additions & 1 deletion AITSYS.Vimeo.Ott/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// 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.Logging;

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

namespace AITSYS.Vimeo.Ott;

/// <summary>
/// Represents various extension methods.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
Expand All @@ -29,4 +36,39 @@ public static VimeoOttClient GetClientForCustomer(this VimeoOttClient baseClient
baseClient.Logger.LogDebug(LoggerEvents.Library, "Creating a new client bound to {customer}", customerHref);
return new(shadowConfig);
}

/// <summary>
/// Maps methods marked with the <see cref="VimeoOttWebhookAttribute" /> to a specified pattern and applies the
/// provided authentication scheme.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder" /> to add the route to.</param>
/// <param name="pattern">The URL pattern of the webhook endpoint.</param>
/// <param name="authenticationScheme">The authentication scheme to apply to the endpoint.</param>
/// <returns>The <see cref="IEndpointRouteBuilder" /> with the mapped webhook endpoint.</returns>
public static IEndpointRouteBuilder MapVimeoOttWebhook(this IEndpointRouteBuilder endpoints, string pattern, string authenticationScheme)
{
var methods = Assembly.GetExecutingAssembly()
.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(VimeoOttWebhookAttribute), false).Length > 0)
.ToArray();

foreach (var method in methods)
{
var attribute = method.GetCustomAttribute<VimeoOttWebhookAttribute>();
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);
})
.RequireAuthorization(new AuthorizeAttribute
{
AuthenticationSchemes = authenticationScheme
});
}

return endpoints;
}
}

0 comments on commit 354d763

Please sign in to comment.