2
0

Implemented /v2/NotificationConnector/*

This commit is contained in:
Glax 2024-04-23 00:58:19 +02:00
parent 0ced3a7dd9
commit 2651a0c53b
2 changed files with 104 additions and 7 deletions

View File

@ -1,5 +1,6 @@
using System.Net;
using System.Text.RegularExpressions;
using Tranga.NotificationConnectors;
namespace Tranga.Server;
@ -7,31 +8,125 @@ public partial class Server
{
private ValueTuple<HttpStatusCode, object?> GetV2NotificationConnector(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, notificationConnectors);
}
private ValueTuple<HttpStatusCode, object?> GetV2NotificationConnectorTypes(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK,
Enum.GetValues<NotificationConnectors.NotificationConnector.NotificationConnectorType>().ToDictionary(b => (byte)b, b => Enum.GetName(b)));
}
private ValueTuple<HttpStatusCode, object?> GetV2NotificationConnectorType(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (groups.Count < 1 ||
!Enum.TryParse(groups[1].Value, true, out NotificationConnector.NotificationConnectorType notificationConnectorType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"NotificationType {groups[1].Value} does not exist.");
}
if(notificationConnectors.All(nc => nc.notificationConnectorType != notificationConnectorType))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"NotificationType {Enum.GetName(notificationConnectorType)} not configured.");
else
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, notificationConnectors.First(nc => nc.notificationConnectorType != notificationConnectorType));
}
private ValueTuple<HttpStatusCode, object?> PostV2NotificationConnectorType(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (groups.Count < 1 ||
!Enum.TryParse(groups[1].Value, true, out NotificationConnector.NotificationConnectorType notificationConnectorType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"NotificationType {groups[1].Value} does not exist.");
}
string? url;
switch (notificationConnectorType)
{
case NotificationConnector.NotificationConnectorType.Gotify:
if(!requestParameters.TryGetValue("url", out url))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'url' missing.");
if(!requestParameters.TryGetValue("appToken", out string? appToken))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'appToken' missing.");
Gotify gotify = new (this, url, appToken);
this.notificationConnectors.RemoveWhere(nc =>
nc.notificationConnectorType == NotificationConnector.NotificationConnectorType.Gotify);
this.notificationConnectors.Add(gotify);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, gotify);
case NotificationConnector.NotificationConnectorType.LunaSea:
if(!requestParameters.TryGetValue("webhook", out string? webhook))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'webhook' missing.");
LunaSea lunaSea = new (this, webhook);
this.notificationConnectors.RemoveWhere(nc =>
nc.notificationConnectorType == NotificationConnector.NotificationConnectorType.LunaSea);
this.notificationConnectors.Add(lunaSea);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, lunaSea);
case NotificationConnector.NotificationConnectorType.Ntfy:
if(!requestParameters.TryGetValue("url", out url))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'url' missing.");
if(!requestParameters.TryGetValue("auth", out string? auth))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'auth' missing.");
Ntfy ntfy = new(this, url, auth);
this.notificationConnectors.RemoveWhere(nc =>
nc.notificationConnectorType == NotificationConnector.NotificationConnectorType.Ntfy);
this.notificationConnectors.Add(ntfy);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, ntfy);
default:
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.MethodNotAllowed, $"NotificationType {Enum.GetName(notificationConnectorType)} is not supported.");
}
}
private ValueTuple<HttpStatusCode, object?> PostV2NotificationConnectorTypeTest(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (groups.Count < 1 ||
!Enum.TryParse(groups[1].Value, true, out NotificationConnector.NotificationConnectorType notificationConnectorType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"NotificationType {groups[1].Value} does not exist.");
}
string? url;
switch (notificationConnectorType)
{
case NotificationConnector.NotificationConnectorType.Gotify:
if(!requestParameters.TryGetValue("url", out url))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'url' missing.");
if(!requestParameters.TryGetValue("appToken", out string? appToken))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'appToken' missing.");
Gotify gotify = new (this, url, appToken);
gotify.SendNotification("Tranga Test", "It was successful :3");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, gotify);
case NotificationConnector.NotificationConnectorType.LunaSea:
if(!requestParameters.TryGetValue("webhook", out string? webhook))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'webhook' missing.");
LunaSea lunaSea = new (this, webhook);
lunaSea.SendNotification("Tranga Test", "It was successful :3");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, lunaSea);
case NotificationConnector.NotificationConnectorType.Ntfy:
if(!requestParameters.TryGetValue("url", out url))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'url' missing.");
if(!requestParameters.TryGetValue("auth", out string? auth))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'auth' missing.");
Ntfy ntfy = new(this, url, auth);
ntfy.SendNotification("Tranga Test", "It was successful :3");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, ntfy);
default:
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.MethodNotAllowed, $"NotificationType {Enum.GetName(notificationConnectorType)} is not supported.");
}
}
private ValueTuple<HttpStatusCode, object?> DeleteV2NotificationConnectorType(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (groups.Count < 1 ||
!Enum.TryParse(groups[1].Value, true, out NotificationConnector.NotificationConnectorType notificationConnectorType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"NotificationType {groups[1].Value} does not exist.");
}
if(notificationConnectors.All(nc => nc.notificationConnectorType != notificationConnectorType))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"NotificationType {Enum.GetName(notificationConnectorType)} not configured.");
else
{
notificationConnectors.Remove(notificationConnectors.First(nc => nc.notificationConnectorType != notificationConnectorType));
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}
}
}

View File

@ -784,6 +784,7 @@ Returns the configured Notification-Connector of the specified Type.
| StatusCode | Meaning |
|------------|---------------------------------------|
| 404 | Library Connector Type does not exist |
| 500 | Parsing Error |
</details>
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/NotificationConnector/<Type>`
@ -825,6 +826,7 @@ Creates a Notification-Connector of the specified Type.
| StatusCode | Meaning |
|------------|--------------------------------------------|
| 404 | Notification Connector Type does not exist |
| 406 | Missing Parameter |
| 500 | Parsing Error |
</details>
@ -866,7 +868,7 @@ Tests a Notification-Connector of the specified Type.
|------------|--------------------------------------------|
| 200 | Test successful |
| 404 | Notification Connector Type does not exist |
| 408 | Test failed |
| 406 | Missing Parameter |
| 500 | Parsing Error |
</details>