Skip to content

Commit 6c1b798

Browse files
committed
migrated from type to string for basic functionality
1 parent 5da14ad commit 6c1b798

9 files changed

Lines changed: 54 additions & 101 deletions

File tree

src/RandomAPI/APIServices/ServiceInterfaces/IWebhookService.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IWebhookService
1414
/// Registers a new webhook listener URL.
1515
/// </summary>
1616
/// <returns>True if added, false if it already existed.</returns>
17-
Task AddListenerAsync(string url, WebhookType type = default);
17+
Task AddListenerAsync(string url, string source);
1818

1919
/// <summary>
2020
/// Removes a webhook listener URL.
@@ -31,21 +31,14 @@ public interface IWebhookService
3131
/// returns a snapshot of all registered listenrs of a given type
3232
/// </summary>
3333
/// <param name="type"> the type of url</param>
34-
Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default);
34+
Task<IEnumerable<string>> GetListenersAsync(string source);
3535

3636
// Controller Logic Methods (Implemented in the derived class)
3737
public Task<IActionResult> HandleGetListenersActionAsync();
38-
public Task<IActionResult> HandleGetListenersOfTypeAsync(WebhookType type);
39-
public Task<IActionResult> HandleRegisterActionAsync(string url, WebhookType type = default);
38+
public Task<IActionResult> HandleGetListenersOfSourceAsync(string source);
39+
public Task<IActionResult> HandleRegisterActionAsync(string url, string source = "");
4040
public Task<IActionResult> HandleUnregisterActionAsync(string url);
41-
public Task<IActionResult> HandleBroadcastActionAsync(IWebHookPayload payload);
42-
43-
public enum WebhookType
44-
{
45-
Default = 0,
46-
Discord = 1,
47-
Logging = 2,
48-
}
41+
public Task<IActionResult> HandleBroadcastActionAsync(IWebHookPayload payload, string source);
4942
}
5043
}
5144

src/RandomAPI/APIServices/Services/BaseWebhookService.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public async Task<IEnumerable<string>> GetListenersAsync()
2020
return urls.Select(u => u.Url);
2121
}
2222

23-
public async Task<IEnumerable<string>> GetListenersAsync(IWebhookService.WebhookType type = IWebhookService.WebhookType.Default)
23+
public async Task<IEnumerable<string>> GetListenersAsync(string source)
2424
{
25-
var urls = await _repo.GetUrlsOfTypeAsync(type);
25+
var urls = await _repo.GetUrlsOfSourceAsync(source);
2626
return urls.Select(u => u.Url);
2727
}
2828

29-
public async Task AddListenerAsync(string url, IWebhookService.WebhookType type = default)
29+
public async Task AddListenerAsync(string url, string source)
3030
{
31-
await _repo.AddUrlAsync(url, type);
31+
await _repo.AddUrlAsync(url, source);
3232
}
3333

3434
public async Task<bool> RemoveListenerAsync(string url)
@@ -43,6 +43,14 @@ public async Task BroadcastAsync<T>(T payload) where T : class
4343
IEnumerable<string> urls = await GetListenersAsync();
4444
await BroadcastAsync(payload, urls);
4545
}
46+
47+
//broadcast for all of source
48+
public async Task BroadcastAsync<T>(T payload, string source) where T : class
49+
{
50+
IEnumerable<string> urls = await GetListenersAsync(source);
51+
await BroadcastAsync(payload, urls);
52+
}
53+
4654
//derived for the payloads
4755
public async Task BroadcastAsync<T>(T payload, IEnumerable<string> urls) where T : class
4856
{

src/RandomAPI/APIServices/Services/WebhookActionService.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ public async Task<IActionResult> HandleGetListenersActionAsync()
1515
return new OkObjectResult(urls);
1616
}
1717

18-
public async Task<IActionResult> HandleGetListenersOfTypeAsync(IWebhookService.WebhookType type)
18+
public async Task<IActionResult> HandleGetListenersOfSourceAsync(string source)
1919
{
20-
var urls = await base.GetListenersAsync(type);
20+
var urls = await base.GetListenersAsync(source);
2121
return new OkObjectResult(urls);
2222
}
2323

24-
public async Task<IActionResult> HandleRegisterActionAsync([FromBody] string url, IWebhookService.WebhookType type = default)
24+
public async Task<IActionResult> HandleRegisterActionAsync([FromBody] string url, string source)
2525
{
2626
if (string.IsNullOrWhiteSpace(url))
2727
return new BadRequestObjectResult("URL cannot be empty.");
2828
//needed both on regisdter and deregister
2929
string safeUrlForLog = SanitizeURL(ref url);
3030

31-
await base.AddListenerAsync(url, type);
31+
await base.AddListenerAsync(url, source);
3232

3333
_logger.LogInformation("Registered new webhook listener: {Url}", safeUrlForLog);
3434

@@ -54,33 +54,20 @@ public async Task<IActionResult> HandleUnregisterActionAsync([FromBody] string u
5454
return new OkObjectResult(new { Message = $"Listener removed: {url}" });
5555
}
5656

57-
public async Task<IActionResult> HandleBroadcastActionAsync([FromBody] IWebHookPayload payload)
57+
public async Task<IActionResult> HandleBroadcastActionAsync([FromBody] IWebHookPayload payload, string source)
5858
{
59-
var listeners = await base.GetListenersAsync();
59+
var listeners = await GetListenersAsync(source);
6060

6161
if (!listeners.Any())
6262
return new BadRequestObjectResult("No listeners registered to broadcast to.");
63+
payload.Timestamp = DateTime.UtcNow;
6364

64-
switch (payload)
65-
{
66-
case WebhookPayload p:
67-
p.Timestamp = DateTime.UtcNow;
68-
69-
break;
70-
71-
case DiscordWebhookPayload p:
72-
break;
73-
74-
default:
75-
_logger.LogWarning("Received unsupported payload type: {Type}", payload.GetType().Name);
76-
return new BadRequestObjectResult(new { Message = "Unsupported webhook payload type." });
77-
}
7865

79-
_logger.LogInformation("Broadcasting test payload: {Message}", payload.content);
80-
await base.BroadcastAsync(payload);
66+
_logger.LogInformation("Broadcasting test payload: {Message}", payload.Content);
67+
await BroadcastAsync(payload);
8168
return new OkObjectResult(new
8269
{
83-
Message = $"Broadcast sent for message: '{payload.content}'. Check logs for delivery status."
70+
Message = $"Broadcast sent for message: '{payload.Content}'. Check logs for delivery status."
8471
});
8572
}
8673

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1+
using Microsoft.AspNetCore.Mvc;
12
using RandomAPI.Models;
23
using RandomAPI.Services.Webhooks;
34

45
namespace RandomAPI.Services.Webhooks
56
{
6-
public class WebhookPayload : ICustomWebhookPayload
7+
public class WebhookPayload : IWebHookPayload
78
{
8-
public string content { get; set; } = "";
99
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
10-
}
11-
12-
public class DiscordWebhookPayload : IWebHookPayload
13-
{
14-
public string content { get; set; } = "";
10+
public string Content { get; set; } = "";
1511
}
1612
}

src/RandomAPI/Controllers/WebhookController.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public async Task<IActionResult> RegisterUrl([FromBody] string url)
4040
/// <summary>
4141
/// Registers a new URL to receive webhook payloads.
4242
/// </summary>
43-
[HttpPost("register-discord")]
44-
public async Task<IActionResult> RegisterDiscordUrl([FromBody] string url)
43+
[HttpPost("register-source")]
44+
public async Task<IActionResult> RegisterUrlWithSource([FromBody] string url, string source = "")
4545
{
46-
return await _webhookService.HandleRegisterActionAsync(url, IWebhookService.WebhookType.Discord);
46+
return await _webhookService.HandleRegisterActionAsync(url, source);
4747
}
4848

4949
/// <summary>
@@ -68,31 +68,13 @@ public async Task<IActionResult> BroadcastTest([FromBody] WebhookPayload payload
6868

6969
payload.Timestamp = DateTime.UtcNow;
7070

71-
_logger.LogInformation("Broadcasting test payload: {Message}", payload.content);
71+
_logger.LogInformation("Broadcasting test payload: {Message}", payload.Content);
7272

7373
await _webhookService.BroadcastAsync(payload);
7474

7575
return Ok(new
7676
{
77-
Message = $"Broadcast sent for message: '{payload.content}'. Check logs for delivery status."
78-
});
79-
}
80-
81-
[HttpPost("debug/discord-broadcast-test")]
82-
public async Task<IActionResult> BroadcastDiscordTest([FromBody] DiscordWebhookPayload payload)
83-
{
84-
var listeners = await _webhookService.GetListenersAsync();
85-
86-
if (!listeners.Any())
87-
return BadRequest("No listeners registered to broadcast to.");
88-
89-
_logger.LogInformation("Broadcasting Discord payload: {Message}", payload.content?.Replace("\r", "").Replace("\n", ""));
90-
91-
await _webhookService.BroadcastAsync(payload);
92-
93-
return Ok(new
94-
{
95-
Message = $"Broadcast sent for message: '{payload.content}'. Check logs for delivery status."
77+
Message = $"Broadcast sent for message: '{payload.Content}'. Check logs for delivery status."
9678
});
9779
}
9880
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
13
namespace RandomAPI.Models
24
{
3-
public interface ICustomWebhookPayload : IWebHookPayload
4-
{
5-
DateTime Timestamp { get; set; }
6-
}
7-
85
public interface IWebHookPayload
96
{
10-
string content { get; set; }
7+
DateTime Timestamp { get; set; }
8+
string Content { get; set; }
119
}
12-
}
10+
}

src/RandomAPI/Models/WebhookUrl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace RandomAPI.Services.Webhooks
66
/// </summary>
77
public class WebhookUrl
88
{
9-
public int Id { get; set; }
9+
public required int Id { get; set; }
1010
public required string Url { get; set; }
11-
public IWebhookService.WebhookType Type { get; set; }
11+
public required string Source { get; set; }
1212
}
1313
}

src/RandomAPI/Repository/IWebhookRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace RandomAPI.Repository
99
public interface IWebhookRepository
1010
{
1111
Task<IEnumerable<WebhookUrl>> GetAllUrlsAsync();
12-
Task<IEnumerable<WebhookUrl>> GetUrlsOfTypeAsync(IWebhookService.WebhookType type);
13-
Task AddUrlAsync(string url, IWebhookService.WebhookType type);
12+
Task<IEnumerable<WebhookUrl>> GetUrlsOfSourceAsync(string source);
13+
Task AddUrlAsync(string url, string source);
1414
Task<int> DeleteUrlAsync(string url);
1515
}
1616
}

src/RandomAPI/Repository/WebhookRepository.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,14 @@ private IDbConnection CreateConnection()
3232
public async Task InitializeAsync()
3333
{
3434
using var db = CreateConnection();
35-
3635
const string sql = @"
3736
CREATE TABLE IF NOT EXISTS WebhookUrls (
38-
Id INTEGER PRIMARY KEY AUTOINCREMENT,
37+
Id TEXT PRIMARY KEY AUTOINCREMENT,
3938
Url TEXT NOT NULL UNIQUE,
40-
Type INTEGER NOT NULL DEFAULT 0
39+
Source TEXT NOT NULL
4140
);";
4241

4342
await db.ExecuteAsync(sql);
44-
try
45-
{
46-
const string alterTableSql = "ALTER TABLE WebhookUrls ADD COLUMN Type INTEGER NOT NULL DEFAULT 0;";
47-
await db.ExecuteAsync(alterTableSql);
48-
}
49-
catch (SqliteException ex) when (ex.SqliteErrorCode == 1) { }
50-
catch (Exception)
51-
{
52-
//re-throw any critical exceptions
53-
throw;
54-
}
5543
}
5644

5745
/// <summary>
@@ -61,15 +49,15 @@ Type INTEGER NOT NULL DEFAULT 0
6149
public async Task<IEnumerable<WebhookUrl>> GetAllUrlsAsync()
6250
{
6351
using var db = CreateConnection();
64-
const string sql = "SELECT Id, Url FROM WebhookUrls ORDER BY Id;";
52+
const string sql = "SELECT Id, Url, Source FROM WebhookUrls ORDER BY Id;";
6553
return await db.QueryAsync<WebhookUrl>(sql);
6654
}
6755

68-
public async Task<IEnumerable<WebhookUrl>> GetUrlsOfTypeAsync(IWebhookService.WebhookType type)
56+
public async Task<IEnumerable<WebhookUrl>> GetUrlsOfSourceAsync(string source)
6957
{
7058
using var db = CreateConnection();
71-
const string sql = "SELECT Id, Url, Type FROM WebhookUrls WHERE Type = @Type;";
72-
var parameters = new { Type = (int)type };
59+
const string sql = "SELECT Id, Url, Source FROM WebhookUrls WHERE Source = @Source;";
60+
var parameters = new { Source = source };
7361

7462
return await db.QueryAsync<WebhookUrl>(sql, parameters);
7563
}
@@ -78,14 +66,15 @@ public async Task<IEnumerable<WebhookUrl>> GetUrlsOfTypeAsync(IWebhookService.We
7866
/// Adds a new URL to the database. Uses INSERT OR IGNORE to handle duplicates gracefully.
7967
/// </summary>
8068
/// <param name="url">The URL string to add.</param>
81-
public async Task AddUrlAsync(string url, IWebhookService.WebhookType type)
69+
/// <param name="source">The source string to categorize the URL.</param>
70+
public async Task AddUrlAsync(string url, string source)
8271
{
8372
using var db = CreateConnection();
84-
const string sql = "INSERT OR IGNORE INTO WebhookUrls (Url, Type) VALUES (@Url, @Type);";
73+
const string sql = "INSERT OR IGNORE INTO WebhookUrls (Url, Source) VALUES (@Url, @Source);";
8574
var parameters = new
8675
{
8776
Url = url,
88-
Type = (int)type
77+
Source = source
8978
};
9079

9180
await db.ExecuteAsync(sql, parameters);

0 commit comments

Comments
 (0)