Skip to content

Commit

Permalink
added download client customizable url base
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaminel committed Jan 18, 2025
1 parent 7786776 commit 243de69
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
7 changes: 6 additions & 1 deletion code/Common/Configuration/DownloadClient/DelugeConfig.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
namespace Common.Configuration.DownloadClient;
using Microsoft.Extensions.Configuration;

namespace Common.Configuration.DownloadClient;

public sealed record DelugeConfig : IConfig
{
public const string SectionName = "Deluge";

public Uri? Url { get; init; }

[ConfigurationKeyName("URL_BASE")]
public string UrlBase { get; init; } = string.Empty;

public string? Password { get; init; }

public void Validate()
Expand Down
7 changes: 6 additions & 1 deletion code/Common/Configuration/DownloadClient/QBitConfig.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
namespace Common.Configuration.DownloadClient;
using Microsoft.Extensions.Configuration;

namespace Common.Configuration.DownloadClient;

public sealed class QBitConfig : IConfig
{
public const string SectionName = "qBittorrent";

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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
namespace Common.Configuration.DownloadClient;
using Microsoft.Extensions.Configuration;

namespace Common.Configuration.DownloadClient;

public record TransmissionConfig : IConfig
{
public const string SectionName = "Transmission";

public Uri? Url { get; init; }

[ConfigurationKeyName("URL_BASE")]
public string UrlBase { get; init; } = "transmission";

public string? Username { get; init; }

public string? Password { get; init; }
Expand Down
3 changes: 3 additions & 0 deletions code/Executable/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,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"
},
Expand Down
3 changes: 3 additions & 0 deletions code/Executable/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class DelugeClient
public DelugeClient(IOptions<DelugeConfig> config, IHttpClientFactory httpClientFactory)
{
_config = config.Value;
_config.Validate();
_httpClient = httpClientFactory.CreateClient(nameof(DelugeService));
}

Expand Down Expand Up @@ -87,8 +88,12 @@ private async Task<String> PostJson(String json)
{
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('/')}/json";
var responseMessage = await _httpClient.PostAsync(uriBuilder.Uri, content);
responseMessage.EnsureSuccessStatusCode();

var responseJson = await responseMessage.Content.ReadAsStringAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ Striker striker
{
_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('/')}/json";
_client = new(httpClientFactory.CreateClient(Constants.HttpClientWithRetryName), uriBuilder.Uri);
}

public override async Task LoginAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ Striker striker
{
_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('/')}/rpc";
_client = new(
httpClientFactory.CreateClient(Constants.HttpClientWithRetryName),
new Uri(_config.Url, "/transmission/rpc").ToString(),
uriBuilder.Uri.ToString(),
login: _config.Username,
password: _config.Password
);
Expand Down

0 comments on commit 243de69

Please sign in to comment.