From f21f7388b73864f7ca0674fccc7f2e34a51e4e3d Mon Sep 17 00:00:00 2001 From: Flaminel Date: Thu, 20 Mar 2025 00:09:24 +0200 Subject: [PATCH] Add download client customizable url base (#43) --- README.md | 3 +++ .../DownloadClient/DelugeConfig.cs | 6 +++++- .../Configuration/DownloadClient/QBitConfig.cs | 6 +++++- .../DownloadClient/TransmissionConfig.cs | 6 +++++- code/Executable/appsettings.Development.json | 3 +++ code/Executable/appsettings.json | 3 +++ .../DownloadClient/Deluge/DelugeClient.cs | 9 +++++++-- .../DownloadClient/QBittorrent/QBitService.cs | 6 +++++- .../Transmission/TransmissionService.cs | 6 +++++- variables.md | 18 ++++++++++++++++++ 10 files changed, 59 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c76f9411..f5125375 100644 --- a/README.md +++ b/README.md @@ -214,15 +214,18 @@ services: # OR # - DOWNLOAD_CLIENT=qBittorrent # - QBITTORRENT__URL=http://localhost:8080 + # - QBITTORRENT__URL_BASE=myCustomPath # - QBITTORRENT__USERNAME=user # - QBITTORRENT__PASSWORD=pass # OR # - DOWNLOAD_CLIENT=deluge + # - DELUGE__URL_BASE=myCustomPath # - DELUGE__URL=http://localhost:8112 # - DELUGE__PASSWORD=testing # OR # - DOWNLOAD_CLIENT=transmission # - TRANSMISSION__URL=http://localhost:9091 + # - TRANSMISSION__URL_BASE=myCustomPath # - TRANSMISSION__USERNAME=test # - TRANSMISSION__PASSWORD=testing diff --git a/code/Common/Configuration/DownloadClient/DelugeConfig.cs b/code/Common/Configuration/DownloadClient/DelugeConfig.cs index be16ac02..59033a28 100644 --- a/code/Common/Configuration/DownloadClient/DelugeConfig.cs +++ b/code/Common/Configuration/DownloadClient/DelugeConfig.cs @@ -1,4 +1,5 @@ -using Common.Exceptions; +using Common.Exceptions; +using Microsoft.Extensions.Configuration; namespace Common.Configuration.DownloadClient; @@ -8,6 +9,9 @@ public sealed record DelugeConfig : IConfig public Uri? Url { get; init; } + [ConfigurationKeyName("URL_BASE")] + public string UrlBase { get; init; } = string.Empty; + public string? Password { get; init; } public void Validate() diff --git a/code/Common/Configuration/DownloadClient/QBitConfig.cs b/code/Common/Configuration/DownloadClient/QBitConfig.cs index afdbc1ef..7de0fafb 100644 --- a/code/Common/Configuration/DownloadClient/QBitConfig.cs +++ b/code/Common/Configuration/DownloadClient/QBitConfig.cs @@ -1,4 +1,5 @@ -using Common.Exceptions; +using Common.Exceptions; +using Microsoft.Extensions.Configuration; namespace Common.Configuration.DownloadClient; @@ -8,6 +9,9 @@ public sealed class QBitConfig : IConfig public Uri? Url { get; init; } + [ConfigurationKeyName("URL_BASE")] + public string UrlBase { get; init; } = string.Empty; + public string? Username { get; init; } public string? Password { get; init; } diff --git a/code/Common/Configuration/DownloadClient/TransmissionConfig.cs b/code/Common/Configuration/DownloadClient/TransmissionConfig.cs index c029b10d..4d30b626 100644 --- a/code/Common/Configuration/DownloadClient/TransmissionConfig.cs +++ b/code/Common/Configuration/DownloadClient/TransmissionConfig.cs @@ -1,4 +1,5 @@ -using Common.Exceptions; +using Common.Exceptions; +using Microsoft.Extensions.Configuration; namespace Common.Configuration.DownloadClient; @@ -8,6 +9,9 @@ public record TransmissionConfig : IConfig public Uri? Url { get; init; } + [ConfigurationKeyName("URL_BASE")] + public string UrlBase { get; init; } = "transmission"; + public string? Username { get; init; } public string? Password { get; init; } diff --git a/code/Executable/appsettings.Development.json b/code/Executable/appsettings.Development.json index 26a69d1f..f5d87202 100644 --- a/code/Executable/appsettings.Development.json +++ b/code/Executable/appsettings.Development.json @@ -52,15 +52,18 @@ "DOWNLOAD_CLIENT": "qbittorrent", "qBittorrent": { "Url": "http://localhost:8080", + "URL_BASE": "", "Username": "test", "Password": "testing" }, "Deluge": { "Url": "http://localhost:8112", + "URL_BASE": "", "Password": "testing" }, "Transmission": { "Url": "http://localhost:9091", + "URL_BASE": "transmission", "Username": "test", "Password": "testing" }, diff --git a/code/Executable/appsettings.json b/code/Executable/appsettings.json index 3b901e87..5fa1fc78 100644 --- a/code/Executable/appsettings.json +++ b/code/Executable/appsettings.json @@ -42,15 +42,18 @@ "DOWNLOAD_CLIENT": "none", "qBittorrent": { "Url": "http://localhost:8080", + "URL_BASE": "", "Username": "", "Password": "" }, "Deluge": { "Url": "http://localhost:8112", + "URL_BASE": "", "Password": "testing" }, "Transmission": { "Url": "http://localhost:9091", + "URL_BASE": "transmission", "Username": "test", "Password": "testing" }, diff --git a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs index e7eac073..63048cb4 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeClient.cs @@ -33,6 +33,7 @@ public sealed class DelugeClient public DelugeClient(IOptions config, IHttpClientFactory httpClientFactory) { _config = config.Value; + _config.Validate(); _httpClient = httpClientFactory.CreateClient(nameof(DelugeService)); } @@ -121,8 +122,12 @@ public sealed class DelugeClient { StringContent content = new StringContent(json); content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json"); - - var responseMessage = await _httpClient.PostAsync(new Uri(_config.Url, "/json"), content); + + UriBuilder uriBuilder = new(_config.Url); + uriBuilder.Path = string.IsNullOrEmpty(_config.UrlBase) + ? $"{uriBuilder.Path.TrimEnd('/')}/json" + : $"{uriBuilder.Path.TrimEnd('/')}/{_config.UrlBase.TrimStart('/').TrimEnd('/')}/json"; + var responseMessage = await _httpClient.PostAsync(uriBuilder.Uri, content); responseMessage.EnsureSuccessStatusCode(); var responseJson = await responseMessage.Content.ReadAsStringAsync(); diff --git a/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs b/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs index 8061fe2b..4d55d7c5 100644 --- a/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs @@ -45,7 +45,11 @@ public class QBitService : DownloadService, IQBitService { _config = config.Value; _config.Validate(); - _client = new(httpClientFactory.CreateClient(Constants.HttpClientWithRetryName), _config.Url); + UriBuilder uriBuilder = new(_config.Url); + uriBuilder.Path = string.IsNullOrEmpty(_config.UrlBase) + ? uriBuilder.Path + : $"{uriBuilder.Path.TrimEnd('/')}/{_config.UrlBase.TrimStart('/')}"; + _client = new(httpClientFactory.CreateClient(Constants.HttpClientWithRetryName), uriBuilder.Uri); } public override async Task LoginAsync() diff --git a/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs b/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs index 51605691..7348959e 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs @@ -64,9 +64,13 @@ public class TransmissionService : DownloadService, ITransmissionService { _config = config.Value; _config.Validate(); + UriBuilder uriBuilder = new(_config.Url); + uriBuilder.Path = string.IsNullOrEmpty(_config.UrlBase) + ? $"{uriBuilder.Path.TrimEnd('/')}/rpc" + : $"{uriBuilder.Path.TrimEnd('/')}/{_config.UrlBase.TrimStart('/').TrimEnd('/')}/rpc"; _client = new( httpClientFactory.CreateClient(Constants.HttpClientWithRetryName), - new Uri(_config.Url, "/transmission/rpc").ToString(), + uriBuilder.Uri.ToString(), login: _config.Username, password: _config.Password ); diff --git a/variables.md b/variables.md index 6c64d315..e5567056 100644 --- a/variables.md +++ b/variables.md @@ -378,6 +378,12 @@ - Default: `http://localhost:8080`. - Required: No. +#### **`QBITTORRENT__URL_BASE`** +- Adds a prefix to the qBittorrent url, such as `[QBITTORRENT__URL]/[QBITTORRENT__URL_BASE]/api`. +- Type: String. +- Default: Empty. +- Required: No. + #### **`QBITTORRENT__USERNAME`** - Username for qBittorrent authentication. - Type: String. @@ -396,6 +402,12 @@ - Default: `http://localhost:8112`. - Required: No. +#### **`DELUGE__URL_BASE`** +- Adds a prefix to the deluge json url, such as `[DELUGE__URL]/[DELUGE__URL_BASE]/json`. +- Type: String. +- Default: Empty. +- Required: No. + #### **`DELUGE__PASSWORD`** - Password for Deluge authentication. - Type: String. @@ -408,6 +420,12 @@ - Default: `http://localhost:9091`. - Required: No. +#### **`TRANSMISSION__URL_BASE`** +- Adds a prefix to the Transmission rpc url, such as `[TRANSMISSION__URL]/[TRANSMISSION__URL_BASE]/rpc`. +- Type: String. +- Default: `transmission`. +- Required: No. + #### **`TRANSMISSION__USERNAME`** - Username for Transmission authentication. - Type: String.