Skip to content

Commit 88a33a8

Browse files
committed
moved webhook stuff into the namespace
1 parent 4b80a7c commit 88a33a8

File tree

7 files changed

+48
-45
lines changed

7 files changed

+48
-45
lines changed
Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
11
using Microsoft.AspNetCore.Mvc;
22
using RandomAPI.Models;
3-
using RandomAPI.Services.Webhooks;
4-
5-
public interface IWebhookService
3+
namespace RandomAPI.Services.Webhooks
64
{
7-
// Basel class methods
8-
/// <summary>
9-
/// Sends a webhook notification to all registered listeners.
10-
/// </summary>
11-
Task BroadcastAsync<T>(T payload) where T : class;
5+
public interface IWebhookService
6+
{
7+
// Basel class methods
8+
/// <summary>
9+
/// Sends a webhook notification to all registered listeners.
10+
/// </summary>
11+
Task BroadcastAsync<T>(T payload) where T : class;
1212

13-
/// <summary>
14-
/// Registers a new webhook listener URL.
15-
/// </summary>
16-
/// <returns>True if added, false if it already existed.</returns>
17-
Task AddListenerAsync(string url, WebhookType type = default);
13+
/// <summary>
14+
/// Registers a new webhook listener URL.
15+
/// </summary>
16+
/// <returns>True if added, false if it already existed.</returns>
17+
Task AddListenerAsync(string url, WebhookType type = default);
1818

19-
/// <summary>
20-
/// Removes a webhook listener URL.
21-
/// </summary>
22-
/// <returns>True if removed, false if not found.</returns>
23-
Task<bool> RemoveListenerAsync(string url);
19+
/// <summary>
20+
/// Removes a webhook listener URL.
21+
/// </summary>
22+
/// <returns>True if removed, false if not found.</returns>
23+
Task<bool> RemoveListenerAsync(string url);
2424

25-
/// <summary>
26-
/// Returns a snapshot of all registered listener URLs.
27-
/// </summary>
28-
Task<IEnumerable<string>> GetListenersAsync();
25+
/// <summary>
26+
/// Returns a snapshot of all registered listener URLs.
27+
/// </summary>
28+
Task<IEnumerable<string>> GetListenersAsync();
2929

30-
/// <summary>
31-
/// returns a snapshot of all registered listenrs of a given type
32-
/// </summary>
33-
/// <param name="type"> the type of url</param>
34-
Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default);
30+
/// <summary>
31+
/// returns a snapshot of all registered listenrs of a given type
32+
/// </summary>
33+
/// <param name="type"> the type of url</param>
34+
Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default);
3535

36-
// Controller Logic Methods (Implemented in the derived class)
37-
public Task<IActionResult> HandleGetListenersActionAsync();
38-
public Task<IActionResult> HandleGetListenersOfTypeAsync(WebhookType type);
39-
public Task<IActionResult> HandleRegisterActionAsync(string url, WebhookType type = default);
40-
public Task<IActionResult> HandleUnregisterActionAsync(string url);
41-
public Task<IActionResult> HandleBroadcastActionAsync(IWebHookPayload payload);
36+
// Controller Logic Methods (Implemented in the derived class)
37+
public Task<IActionResult> HandleGetListenersActionAsync();
38+
public Task<IActionResult> HandleGetListenersOfTypeAsync(WebhookType type);
39+
public Task<IActionResult> HandleRegisterActionAsync(string url, WebhookType type = default);
40+
public Task<IActionResult> HandleUnregisterActionAsync(string url);
41+
public Task<IActionResult> HandleBroadcastActionAsync(IWebHookPayload payload);
4242

43-
public enum WebhookType
44-
{
45-
Default = 0,
46-
Discord = 1,
47-
Logging = 2,
43+
public enum WebhookType
44+
{
45+
Default = 0,
46+
Discord = 1,
47+
Logging = 2,
48+
}
4849
}
4950
}
51+

src/RandomAPI/APIServices/Services/BaseWebhookService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using RandomAPI.Repository;
2-
using static IWebhookService;
32

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

24-
public async Task<IEnumerable<string>> GetListenersAsync(WebhookType type = WebhookType.Default)
23+
public async Task<IEnumerable<string>> GetListenersAsync(IWebhookService.WebhookType type = IWebhookService.WebhookType.Default)
2524
{
2625
var urls = await _repo.GetUrlsOfTypeAsync(type);
2726
return urls.Select(u => u.Url);
2827
}
2928

30-
public async Task AddListenerAsync(string url, WebhookType type = default)
29+
public async Task AddListenerAsync(string url, IWebhookService.WebhookType type = default)
3130
{
3231
await _repo.AddUrlAsync(url, type);
3332
}

src/RandomAPI/APIServices/Services/WebhookActionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public async Task<IActionResult> HandleBroadcastActionAsync([FromBody] IWebHookP
8989
private static string SanitizeURL(ref string url)
9090
{
9191
url = url.Trim();
92-
var safeUrlForLog = url.Replace("\r", "").Replace("\n", "");
92+
string? safeUrlForLog = url.Replace("\r", "").Replace("\n", "");
9393
return safeUrlForLog;
9494
}
9595
}

src/RandomAPI/Controllers/WebhookController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22
using RandomAPI.Services.Webhooks;
3-
using static IWebhookService;
3+
44

55
namespace RandomAPI.Controllers
66
{
@@ -43,7 +43,7 @@ public async Task<IActionResult> RegisterUrl([FromBody] string url)
4343
[HttpPost("register-discord")]
4444
public async Task<IActionResult> RegisterDiscordUrl([FromBody] string url)
4545
{
46-
return await _webhookService.HandleRegisterActionAsync(url, WebhookType.Discord);
46+
return await _webhookService.HandleRegisterActionAsync(url, IWebhookService.WebhookType.Discord);
4747
}
4848

4949
/// <summary>

src/RandomAPI/Models/WebhookUrl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace RandomAPI.Models
2+
namespace RandomAPI.Services.Webhooks
33
{
44
/// <summary>
55
/// Represents a registered webhook listener URL stored in the database.

src/RandomAPI/Repository/IWebhookRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RandomAPI.Models;
2+
using RandomAPI.Services.Webhooks;
23

34
namespace RandomAPI.Repository
45
{

src/RandomAPI/Repository/WebhookRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Data.Sqlite;
33
using RandomAPI.Models;
44
using System.Data;
5+
using RandomAPI.Services.Webhooks;
56

67
namespace RandomAPI.Repository
78
{

0 commit comments

Comments
 (0)