-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
ArmaForces.ArmaServerManager/Features/Webhooks/DTOs/DiscordWebhook.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ArmaForces.ArmaServerManager.Features.Webhooks.DTOs; | ||
|
||
internal record DiscordWebhook | ||
{ | ||
[JsonPropertyName("username")] | ||
public string UserName { get; init; } | ||
|
||
public string Content { get; init; } | ||
} |
8 changes: 8 additions & 0 deletions
8
ArmaForces.ArmaServerManager/Features/Webhooks/IWebhookClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace ArmaForces.ArmaServerManager.Features.Webhooks; | ||
|
||
public interface IWebhookClient | ||
{ | ||
Task Send(string content); | ||
} |
22 changes: 22 additions & 0 deletions
22
ArmaForces.ArmaServerManager/Features/Webhooks/WebhookClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using ArmaForces.ArmaServerManager.Common; | ||
using ArmaForces.ArmaServerManager.Features.Webhooks.DTOs; | ||
|
||
namespace ArmaForces.ArmaServerManager.Features.Webhooks; | ||
|
||
internal class WebhookClient : HttpClientBase, IWebhookClient | ||
{ | ||
public WebhookClient(HttpClient httpClient) : base(httpClient) | ||
{ | ||
} | ||
|
||
public async Task Send(string content) | ||
{ | ||
await HttpPostAsync(requestUrl: null, content: new DiscordWebhook | ||
{ | ||
UserName = "Arma Server", | ||
Content = content | ||
}); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ArmaForces.ArmaServerManager/Features/Webhooks/WebhookServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Net.Http; | ||
using ArmaForces.Arma.Server.Config; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace ArmaForces.ArmaServerManager.Features.Webhooks; | ||
|
||
internal static class WebhookServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddWebhookClient(this IServiceCollection services) | ||
=> services.AddHttpClientForWebhookClient(); | ||
|
||
private static IServiceCollection AddHttpClientForWebhookClient(this IServiceCollection services) | ||
{ | ||
services | ||
.AddHttpClient<IWebhookClient, WebhookClient>() | ||
.ConfigureHttpClient(SetBaseAddress()); | ||
|
||
return services; | ||
} | ||
|
||
private static Action<IServiceProvider, HttpClient> SetBaseAddress() | ||
=> (services, client) => client.BaseAddress = new Uri( | ||
services.GetRequiredService<ISettings>().WebhookUrl ?? | ||
throw new NotSupportedException("Missions API is required, provide valid url address.")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace ArmaForces.ArmaServerManager.Services; | ||
|
||
public interface IWebhookService | ||
{ | ||
Task AnnounceStart(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Threading.Tasks; | ||
using ArmaForces.Arma.Server.Config; | ||
using ArmaForces.ArmaServerManager.Features.Webhooks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace ArmaForces.ArmaServerManager.Services; | ||
|
||
public class WebhookService : IWebhookService | ||
{ | ||
private readonly IWebhookClient _webhookClient; | ||
private readonly ILogger<WebhookService> _logger; | ||
private readonly string? _webhookUrl; | ||
|
||
public WebhookService(IWebhookClient webhookClient, ISettings settings, ILogger<WebhookService> logger) | ||
{ | ||
_webhookClient = webhookClient; | ||
_logger = logger; | ||
_webhookUrl = settings.WebhookUrl; | ||
} | ||
|
||
public async Task AnnounceStart() | ||
{ | ||
_logger.LogInformation("Announce start"); | ||
if (_webhookUrl == null) return; | ||
_logger.LogInformation("Webhook url: {WebhookUrl}", _webhookUrl); | ||
|
||
await Task.Delay(10000); | ||
await _webhookClient.Send("Manager started"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters