Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletions src/RandomAPI/APIServices/ServiceInterfaces/IWebhookService.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
using Microsoft.AspNetCore.Mvc;
using RandomAPI.Models;
using RandomAPI.Services.Webhooks;

public interface IWebhookService
namespace RandomAPI.Services.Webhooks
{
// Basel class methods
/// <summary>
/// Sends a webhook notification to all registered listeners.
/// </summary>
Task BroadcastAsync<T>(T payload) where T : class;
public interface IWebhookService
{
// Basel class methods
/// <summary>
/// Sends a webhook notification to all registered listeners.
/// </summary>
Task BroadcastAsync<T>(T payload) where T : class;

/// <summary>
/// Registers a new webhook listener URL.
/// </summary>
/// <returns>True if added, false if it already existed.</returns>
Task AddListenerAsync(string url, WebhookType type = default);
/// <summary>
/// Registers a new webhook listener URL.
/// </summary>
/// <returns>True if added, false if it already existed.</returns>
Task AddListenerAsync(string url, WebhookType type = default);

/// <summary>
/// Removes a webhook listener URL.
/// </summary>
/// <returns>True if removed, false if not found.</returns>
Task<bool> RemoveListenerAsync(string url);
/// <summary>
/// Removes a webhook listener URL.
/// </summary>
/// <returns>True if removed, false if not found.</returns>
Task<bool> RemoveListenerAsync(string url);

/// <summary>
/// Returns a snapshot of all registered listener URLs.
/// </summary>
Task<IEnumerable<string>> GetListenersAsync();
/// <summary>
/// Returns a snapshot of all registered listener URLs.
/// </summary>
Task<IEnumerable<string>> GetListenersAsync();

/// <summary>
/// returns a snapshot of all registered listenrs of a given type
/// </summary>
/// <param name="type"> the type of url</param>
Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default);
/// <summary>
/// returns a snapshot of all registered listenrs of a given type
/// </summary>
/// <param name="type"> the type of url</param>
Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default);

// Controller Logic Methods (Implemented in the derived class)
public Task<IActionResult> HandleGetListenersActionAsync();
public Task<IActionResult> HandleGetListenersOfTypeAsync(WebhookType type);
public Task<IActionResult> HandleRegisterActionAsync(string url, WebhookType type = default);
public Task<IActionResult> HandleUnregisterActionAsync(string url);
public Task<IActionResult> HandleBroadcastActionAsync(IWebHookPayload payload);
// Controller Logic Methods (Implemented in the derived class)
public Task<IActionResult> HandleGetListenersActionAsync();
public Task<IActionResult> HandleGetListenersOfTypeAsync(WebhookType type);
public Task<IActionResult> HandleRegisterActionAsync(string url, WebhookType type = default);
public Task<IActionResult> HandleUnregisterActionAsync(string url);
public Task<IActionResult> HandleBroadcastActionAsync(IWebHookPayload payload);

public enum WebhookType
{
Default = 0,
Discord = 1,
Logging = 2,
public enum WebhookType
{
Default = 0,
Discord = 1,
Logging = 2,
}
}
}

5 changes: 2 additions & 3 deletions src/RandomAPI/APIServices/Services/BaseWebhookService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RandomAPI.Repository;
using static IWebhookService;

namespace RandomAPI.Services.Webhooks
{
Expand All @@ -21,13 +20,13 @@ public async Task<IEnumerable<string>> GetListenersAsync()
return urls.Select(u => u.Url);
}

public async Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default)
public async Task<IEnumerable<string>> GetListenersAsync(IWebhookService.WebhookType type = IWebhookService.WebhookType.Default)
{
var urls = await _repo.GetUrlsOfTypeAsync(type);
return urls.Select(u => u.Url);
}

public async Task AddListenerAsync(string url, WebhookType type = default)
public async Task AddListenerAsync(string url, IWebhookService.WebhookType type = default)
{
await _repo.AddUrlAsync(url, type);
}
Expand Down
7 changes: 3 additions & 4 deletions src/RandomAPI/APIServices/Services/WebhookActionService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using RandomAPI.Models;
using RandomAPI.Repository;
using static IWebhookService;

namespace RandomAPI.Services.Webhooks
{
Expand All @@ -16,7 +15,7 @@ public async Task<IActionResult> HandleGetListenersActionAsync()
return new OkObjectResult(urls);
}

public async Task<IActionResult> HandleGetListenersOfTypeAsync(WebhookType type)
public async Task<IActionResult> HandleGetListenersOfTypeAsync(IWebhookService.WebhookType type)
{
var urls = await base.GetListenersAsync(type);
return new OkObjectResult(urls);
Expand All @@ -26,7 +25,7 @@ public async Task<IActionResult> HandleRegisterActionAsync([FromBody] string url
{
if (string.IsNullOrWhiteSpace(url))
return new BadRequestObjectResult("URL cannot be empty.");
//neede both on regisdter and deregister
//needed both on regisdter and deregister
string safeUrlForLog = SanitizeURL(ref url);

await base.AddListenerAsync(url, type);
Expand Down Expand Up @@ -89,7 +88,7 @@ public async Task<IActionResult> HandleBroadcastActionAsync([FromBody] IWebHookP
private static string SanitizeURL(ref string url)
{
url = url.Trim();
var safeUrlForLog = url.Replace("\r", "").Replace("\n", "");
string? safeUrlForLog = url.Replace("\r", "").Replace("\n", "");
return safeUrlForLog;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/RandomAPI/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using RandomAPI.Services.Webhooks;
using static IWebhookService;


namespace RandomAPI.Controllers
{
Expand Down Expand Up @@ -43,7 +43,7 @@ public async Task<IActionResult> RegisterUrl([FromBody] string url)
[HttpPost("register-discord")]
public async Task<IActionResult> RegisterDiscordUrl([FromBody] string url)
{
return await _webhookService.HandleRegisterActionAsync(url, WebhookType.Discord);
return await _webhookService.HandleRegisterActionAsync(url, IWebhookService.WebhookType.Discord);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/RandomAPI/Models/WebhookUrl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

namespace RandomAPI.Models
namespace RandomAPI.Services.Webhooks
{
/// <summary>
/// Represents a registered webhook listener URL stored in the database.
Expand Down
9 changes: 9 additions & 0 deletions src/RandomAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
// TODO:
// - good logging service. rabapp has an event table, i bet i could do something worse

// date idea generator based on a list of ideas and an energy rating. should have a table of all ideas, endpoints to review what exists, and display the most recent selection from both parties

// idea - email checker that notifies me when a meeting time is moved or canceled for rabapp

//

// - AlertGatewayService
// API Endpoint Goal: POST / alert / ingest
// Brief Description(Project Scope): Centralized Notification Hub with Discord Integration.
Expand All @@ -97,3 +103,6 @@ public class HealthCheckService { }
// A CRUD API to save and retrieve frequently forgotten code snippets, complex CLI commands, and database queries.
// Supports robust searching by language (python, sql) and customizable tags (regex, lambda, auth).
public class UniversalSnippetService { }
//list of strings as languages
//list of strings as tags
//snippet should probably the language(s), list of tags, the code snippet, and a description?
1 change: 1 addition & 0 deletions src/RandomAPI/Repository/IWebhookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RandomAPI.Models;
using RandomAPI.Services.Webhooks;

namespace RandomAPI.Repository
{
Expand Down
1 change: 1 addition & 0 deletions src/RandomAPI/Repository/WebhookRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Data.Sqlite;
using RandomAPI.Models;
using System.Data;
using RandomAPI.Services.Webhooks;

namespace RandomAPI.Repository
{
Expand Down
Loading