Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Added api to test arr connections.

nzbdav-dev9208c82f1f11
diff --git a/backend/Api/Controllers/TestArrConnection/TestArrConnectionController.cs b/backend/Api/Controllers/TestArrConnection/TestArrConnectionController.cs
new file mode 100644
index 00000000..1994178b
--- /dev/null
+++ b/backend/Api/Controllers/TestArrConnection/TestArrConnectionController.cs
@@ -0,0 +1,41 @@
+using Microsoft.AspNetCore.Mvc;
+using NzbWebDAV.Clients.RadarrSonarr;
+using NzbWebDAV.Clients.Usenet;
+using NzbWebDAV.Exceptions;
+
+namespace NzbWebDAV.Api.Controllers.TestArrConnection;
+
+[ApiController]
+[Route("api/test-arr-connection")]
+public class TestArrConnectionController() : BaseApiController
+{
+ private static async Task<TestArrConnectionResponse> TestArrConnection(TestArrConnectionRequest request)
+ {
+ try
+ {
+ var client = new ArrClient(request.Host, request.ApiKey);
+ var apiInfo = await client.GetApiInfo();
+ return new TestArrConnectionResponse
+ {
+ Status = true,
+ Connected = apiInfo.Current?.Length > 0
+ };
+ }
+ catch (Exception e)
+ {
+ return new TestArrConnectionResponse
+ {
+ Status = true,
+ Connected = false,
+ Error = e.Message
+ };
+ }
+ }
+
+ protected override async Task<IActionResult> HandleRequest()
+ {
+ var request = new TestArrConnectionRequest(HttpContext);
+ var response = await TestArrConnection(request);
+ return Ok(response);
+ }
+}
\ No newline at end of file
diff --git a/backend/Api/Controllers/TestArrConnection/TestArrConnectionRequest.cs b/backend/Api/Controllers/TestArrConnection/TestArrConnectionRequest.cs
new file mode 100644
index 00000000..18b368b3
--- /dev/null
+++ b/backend/Api/Controllers/TestArrConnection/TestArrConnectionRequest.cs
@@ -0,0 +1,18 @@
+using Microsoft.AspNetCore.Http;
+
+namespace NzbWebDAV.Api.Controllers.TestArrConnection;
+
+public class TestArrConnectionRequest
+{
+ public string Host { get; init; }
+ public string ApiKey { get; init; }
+
+ public TestArrConnectionRequest(HttpContext context)
+ {
+ Host = context.Request.Form["host"].FirstOrDefault()
+ ?? throw new BadHttpRequestException("Arr host is required");
+
+ ApiKey = context.Request.Form["apiKey"].FirstOrDefault()
+ ?? throw new BadHttpRequestException("Arr apiKey is required");
+ }
+}
\ No newline at end of file
diff --git a/backend/Api/Controllers/TestArrConnection/TestArrConnectionResponse.cs b/backend/Api/Controllers/TestArrConnection/TestArrConnectionResponse.cs
new file mode 100644
index 00000000..aad6f542
--- /dev/null
+++ b/backend/Api/Controllers/TestArrConnection/TestArrConnectionResponse.cs
@@ -0,0 +1,6 @@
+namespace NzbWebDAV.Api.Controllers.TestArrConnection;
+
+public class TestArrConnectionResponse : BaseApiResponse
+{
+ public bool Connected { get; set; }
+}
\ No newline at end of file
diff --git a/backend/Clients/RadarrSonarr/ArrClient.cs b/backend/Clients/RadarrSonarr/ArrClient.cs
index 426f290a..2e4f07b0 100644
--- a/backend/Clients/RadarrSonarr/ArrClient.cs
+++ b/backend/Clients/RadarrSonarr/ArrClient.cs
@@ -6,13 +6,17 @@ using NzbWebDAV.Config;
namespace NzbWebDAV.Clients.RadarrSonarr;
-public abstract class ArrClient(string host, string apiKey)
+public class ArrClient(string host, string apiKey)
{
public string Host { get; } = host;
private string ApiKey { get; } = apiKey;
private const string BasePath = "/api/v3";
- public abstract Task<bool> RemoveAndSearch(string symlinkPath);
+ public Task<ArrApiInfoResponse> GetApiInfo() =>
+ GetRoot<ArrApiInfoResponse>($"/api");
+
+ public virtual Task<bool> RemoveAndSearch(string symlinkPath) =>
+ throw new InvalidOperationException();
public Task<List<ArrRootFolder>> GetRootFolders() =>
Get<List<ArrRootFolder>>($"/rootfolder");
@@ -37,10 +41,13 @@ public abstract class ArrClient(string host, string apiKey)
public Task<ArrCommand> CommandAsync(object command) =>
Post<ArrCommand>($"/command", command);
- protected async Task<T> Get<T>(string path)
+ protected Task<T> Get<T>(string path) =>
+ GetRoot<T>($"{BasePath}{path}");
+
+ protected async Task<T> GetRoot<T>(string rootPath)
{
using var httpClient = GetHttpClient();
- await using var response = await httpClient.GetStreamAsync($"{Host}{BasePath}{path}");
+ await using var response = await httpClient.GetStreamAsync($"{Host}{rootPath}");
return await JsonSerializer.DeserializeAsync<T>(response) ?? throw new NullReferenceException();
}
diff --git a/backend/Clients/RadarrSonarr/BaseModels/ArrApiInfoResponse.cs b/backend/Clients/RadarrSonarr/BaseModels/ArrApiInfoResponse.cs
new file mode 100644
index 00000000..7ff1ad14
--- /dev/null
+++ b/backend/Clients/RadarrSonarr/BaseModels/ArrApiInfoResponse.cs
@@ -0,0 +1,9 @@
+using System.Text.Json.Serialization;
+
+namespace NzbWebDAV.Clients.RadarrSonarr.BaseModels;
+
+public class ArrApiInfoResponse
+{
+ [JsonPropertyName("current")]
+ public string? Current { get; set; }
+}
\ No newline at end of file