From 564a40de140eabfa58f837af56e631f47b8de188 Mon Sep 17 00:00:00 2001 From: Daniel Silvestre Date: Mon, 4 Nov 2024 22:51:28 +0100 Subject: [PATCH] Add DelugeClientConfig to set timeout and other future stuff (#5) * Added docker-compose.yml to help testing * Added DelugeClientConfig class to store timeout and other future stuff * Set defaut timeout to 3 seconds * Update README.md --- .gitignore | 1 + .../AuthentificationTests.cs | 6 +- DelugeRPCClient.Net.Tests/DelugeClientTest.cs | 64 +++++++++++++++++ DelugeRPCClient.Net.Tests/LabelTests.cs | 59 +++++++--------- DelugeRPCClient.Net.Tests/TorrentsTests.cs | 68 ++++++++----------- .../Core/CoreDelugeWebClient.cs | 27 +++++--- DelugeRPCClient.Net/DelugeClient.cs | 2 +- DelugeRPCClient.Net/DelugeClientConfig.cs | 15 ++++ README.md | 11 +++ docker-compose.yml | 16 +++++ 10 files changed, 179 insertions(+), 90 deletions(-) create mode 100644 DelugeRPCClient.Net.Tests/DelugeClientTest.cs create mode 100644 DelugeRPCClient.Net/DelugeClientConfig.cs create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index dfcfd56..1e08def 100644 --- a/.gitignore +++ b/.gitignore @@ -348,3 +348,4 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ +docker/ \ No newline at end of file diff --git a/DelugeRPCClient.Net.Tests/AuthentificationTests.cs b/DelugeRPCClient.Net.Tests/AuthentificationTests.cs index 478aa6a..23d141a 100644 --- a/DelugeRPCClient.Net.Tests/AuthentificationTests.cs +++ b/DelugeRPCClient.Net.Tests/AuthentificationTests.cs @@ -4,14 +4,12 @@ namespace DelugeRPCClient.Net.Tests { [TestClass] - public class AuthentificationTests + public class AuthentificationTests : DelugeClientTest { [TestMethod] public async Task LoginLogout() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + DelugeClient client = await Login(); bool logoutResult = await client.Logout(); Assert.IsTrue(logoutResult); } diff --git a/DelugeRPCClient.Net.Tests/DelugeClientTest.cs b/DelugeRPCClient.Net.Tests/DelugeClientTest.cs new file mode 100644 index 0000000..1fdeb8a --- /dev/null +++ b/DelugeRPCClient.Net.Tests/DelugeClientTest.cs @@ -0,0 +1,64 @@ +using DelugeRPCClient.Net.Models; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace DelugeRPCClient.Net.Tests +{ + public class DelugeClientTest + { + protected async Task Login() + { + DelugeClientConfig config = new DelugeClientConfig() + { + IgnoreSslErrors = true, + Timeout = new TimeSpan(0, 0, 30) + }; + DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword, config); + bool loginResult = await client.Login(); + Assert.IsTrue(loginResult); + + return client; + } + + protected async Task Logout(DelugeClient client) + { + bool logoutResult = await client.Logout(); + Assert.IsTrue(logoutResult); + } + + protected async Task AddTestTorrent(DelugeClient client) + { + Torrent torrent = await client.AddTorrentByFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Constants.TestTorrentFilename)); + Assert.IsNotNull(torrent); + Thread.Sleep(1000); + return torrent; + } + + protected async Task RemoveTestTorrent(DelugeClient client, Torrent torrent) + { + bool removeTorrentResult = await client.RemoveTorrent(torrent.Hash); + Assert.IsTrue(removeTorrentResult); + Thread.Sleep(1000); + } + + protected async Task AddTestLabel(DelugeClient client) + { + bool addLabelResult = await client.AddLabel(Constants.TestLabelName); + Assert.IsTrue(addLabelResult); + Thread.Sleep(1000); + } + + protected async Task RemoveTestLabel(DelugeClient client) + { + bool removeLabelResult = await client.RemoveLabel(Constants.TestLabelName); + Assert.IsTrue(removeLabelResult); + Thread.Sleep(1000); + } + } +} diff --git a/DelugeRPCClient.Net.Tests/LabelTests.cs b/DelugeRPCClient.Net.Tests/LabelTests.cs index 3cfdeb8..3fff10d 100644 --- a/DelugeRPCClient.Net.Tests/LabelTests.cs +++ b/DelugeRPCClient.Net.Tests/LabelTests.cs @@ -2,6 +2,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -9,72 +10,58 @@ namespace DelugeRPCClient.Net.Tests { [TestClass] - public class LabelTests + public class LabelTests : DelugeClientTest { [TestMethod] public async Task ListLabels() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); - - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); - + DelugeClient client = await Login(); + + await AddTestLabel(client); + List labels = await client.ListLabels(); Assert.IsNotNull(labels); Assert.AreNotEqual(0, labels.Count); + + await RemoveTestLabel(client); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await Logout(client); } [TestMethod] - public async Task AddRemoveLabel() + public async Task AddAndRemoveLabel() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + DelugeClient client = await Login(); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + await AddTestLabel(client); - bool addLabelResult = await client.AddLabel(Constants.TestLabelName); - Assert.IsTrue(addLabelResult); + await RemoveTestLabel(client); - Thread.Sleep(1000); - - bool removeLabelResult = await client.RemoveLabel(Constants.TestLabelName); - Assert.IsTrue(removeLabelResult); - - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await Logout(client); } [TestMethod] public async Task AssignLabel() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + DelugeClient client = await Login(); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + await AddTestLabel(client); - List torrents = await client.ListTorrents(new Dictionary() { { "label", "" } }); - Assert.IsNotNull(torrents); - Assert.AreNotEqual(0, torrents.Count); - - Torrent torrent = torrents[0]; + Torrent testTorrent = await AddTestTorrent(client); - bool assignResult = await client.SetTorrentLabel(torrent.Hash, Constants.TestLabelName); + bool assignResult = await client.SetTorrentLabel(testTorrent.Hash, Constants.TestLabelName); Assert.IsTrue(assignResult); Thread.Sleep(1000); - bool unsertLabelResult = await client.SetTorrentLabel(torrent.Hash, null); + bool unsertLabelResult = await client.SetTorrentLabel(testTorrent.Hash, null); Assert.IsTrue(unsertLabelResult); Thread.Sleep(1000); - bool removeLabelResult = await client.RemoveLabel(Constants.TestLabelName); - Assert.IsTrue(removeLabelResult); - Thread.Sleep(1000); + await RemoveTestLabel(client); + + await RemoveTestTorrent(client, testTorrent); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await Logout(client); } } } diff --git a/DelugeRPCClient.Net.Tests/TorrentsTests.cs b/DelugeRPCClient.Net.Tests/TorrentsTests.cs index 7c65c28..83673df 100644 --- a/DelugeRPCClient.Net.Tests/TorrentsTests.cs +++ b/DelugeRPCClient.Net.Tests/TorrentsTests.cs @@ -12,15 +12,14 @@ namespace DelugeRPCClient.Net.Tests { [TestClass] - public class TorrentsTests + public class TorrentsTests : DelugeClientTest { [TestMethod] public async Task ListAndGetTorrent() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + DelugeClient client = await Login(); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + Torrent testTorrent = await AddTestTorrent(client); List torrents = await client.ListTorrents(); Assert.IsNotNull(torrents); @@ -29,17 +28,17 @@ public async Task ListAndGetTorrent() Torrent torrent = await client.GetTorrent(torrents[0].Hash); Assert.IsNotNull(torrent); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await RemoveTestTorrent(client, testTorrent); + + await Logout(client); } [TestMethod] public async Task ListAndGetTorrentExtended() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + DelugeClient client = await Login(); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + Torrent testTorrent = await AddTestTorrent(client); List torrents = await client.ListTorrentsExtended(); Assert.IsNotNull(torrents); @@ -48,17 +47,15 @@ public async Task ListAndGetTorrentExtended() TorrentExtended torrent = await client.GetTorrentExtended(torrents[0].Hash); Assert.IsNotNull(torrent); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await RemoveTestTorrent(client, testTorrent); + + await Logout(client); } [TestMethod] public async Task AddRemoveTorrentByMagnet() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); - - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + DelugeClient client = await Login(); Torrent torrent = await client.AddTorrentByMagnet(Constants.TorrentMagnet); Assert.IsNotNull(torrent); @@ -68,17 +65,13 @@ public async Task AddRemoveTorrentByMagnet() bool removeTorrentResult = await client.RemoveTorrent(torrent.Hash); Assert.IsTrue(removeTorrentResult); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await Logout(client); } [TestMethod] public async Task AddRemoveTorrentByFile() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); - - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + DelugeClient client = await Login(); Torrent torrent = await client.AddTorrentByFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Constants.TestTorrentFilename)); Assert.IsNotNull(torrent); @@ -88,17 +81,13 @@ public async Task AddRemoveTorrentByFile() bool removeTorrentResult = await client.RemoveTorrent(torrent.Hash); Assert.IsTrue(removeTorrentResult); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await Logout(client); } [TestMethod] public async Task AddRemoveTorrentByUrl() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); - - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + DelugeClient client = await Login(); Torrent torrent = await client.AddTorrentByUrl(Constants.TestTorrentUrl); Assert.IsNotNull(torrent); @@ -108,17 +97,15 @@ public async Task AddRemoveTorrentByUrl() bool removeTorrentResult = await client.RemoveTorrent(torrent.Hash); Assert.IsTrue(removeTorrentResult); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await Logout(client); } [TestMethod] public async Task PauseResumeTorrent() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + DelugeClient client = await Login(); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + Torrent testTorrent = await AddTestTorrent(client); List torrents = await client.ListTorrents(); Assert.IsNotNull(torrents); @@ -139,19 +126,19 @@ public async Task PauseResumeTorrent() Assert.IsTrue(pauseResult); bool resumeResult = await client.ResumeTorrent(torrent.Hash); Assert.IsTrue(resumeResult); - } + } - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await RemoveTestTorrent(client, testTorrent); + + await Logout(client); } [TestMethod] public async Task RecheckTorrents() { - DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + DelugeClient client = await Login(); - bool loginResult = await client.Login(); - Assert.IsTrue(loginResult); + Torrent testTorrent = await AddTestTorrent(client); List torrents = await client.ListTorrents(); Assert.IsNotNull(torrents); @@ -162,8 +149,9 @@ public async Task RecheckTorrents() bool? recheckResult = await client.RecheckTorrents(torrent.Hash.Split(",").ToList()); Assert.IsNull(recheckResult); - bool logoutResult = await client.Logout(); - Assert.IsTrue(logoutResult); + await RemoveTestTorrent(client, testTorrent); + + await Logout(client); } } } diff --git a/DelugeRPCClient.Net/Core/CoreDelugeWebClient.cs b/DelugeRPCClient.Net/Core/CoreDelugeWebClient.cs index 8d96f06..863abf9 100644 --- a/DelugeRPCClient.Net/Core/CoreDelugeWebClient.cs +++ b/DelugeRPCClient.Net/Core/CoreDelugeWebClient.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace DelugeRPCClient.Net.Core @@ -16,9 +17,12 @@ public class CoreDelugeWebClient private HttpClientHandler HttpClientHandler { get; set; } private HttpClient HttpClient { get; set; } private int RequestId { get; set; } + private DelugeClientConfig DelugeClientConfig { get; set; } - public CoreDelugeWebClient(string url) + public CoreDelugeWebClient(string url, DelugeClientConfig config = null) { + DelugeClientConfig = config ?? new DelugeClientConfig(); + HttpClientHandler = new HttpClientHandler { AllowAutoRedirect = true, @@ -26,14 +30,19 @@ public CoreDelugeWebClient(string url) CookieContainer = new CookieContainer(), AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; - HttpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; - + + if (DelugeClientConfig.IgnoreSslErrors) + { + HttpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; + } + HttpClient = new HttpClient(HttpClientHandler, true); - + HttpClient.Timeout = DelugeClientConfig.Timeout; + RequestId = 1; Url = url; - } + } protected async Task SendRequest(string method, params object[] parameters) { @@ -47,7 +56,9 @@ protected async Task SendRequest(DelugeRequest webRequest) NullValueHandling = webRequest.NullValueHandling }); + var responseJson = await PostJson(requestJson); + var webResponse = JsonConvert.DeserializeObject>(responseJson); if (webResponse.Error != null) throw new DelugeClientException(webResponse.Error.Message); @@ -60,10 +71,8 @@ private async Task PostJson(String json) { StringContent content = new StringContent(json); content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json"); - - var httpClient = new HttpClient(); - httpClient.Timeout = TimeSpan.FromMilliseconds(30); - var responseMessage = await httpClient.PostAsync(Url, content); + + var responseMessage = await HttpClient.PostAsync(Url, content); responseMessage.EnsureSuccessStatusCode(); var responseJson = await responseMessage.Content.ReadAsStringAsync(); diff --git a/DelugeRPCClient.Net/DelugeClient.cs b/DelugeRPCClient.Net/DelugeClient.cs index 2d46ce7..30dd21e 100644 --- a/DelugeRPCClient.Net/DelugeClient.cs +++ b/DelugeRPCClient.Net/DelugeClient.cs @@ -25,7 +25,7 @@ public class DelugeClient : Core.CoreDelugeWebClient /// /// url of delugeweb (like http://localhost:8112/json) /// delugeweb password - public DelugeClient(string url, string password) : base(url) + public DelugeClient(string url, string password,DelugeClientConfig config = null) : base(url, config) { Password = password; } diff --git a/DelugeRPCClient.Net/DelugeClientConfig.cs b/DelugeRPCClient.Net/DelugeClientConfig.cs new file mode 100644 index 0000000..a7e7f4c --- /dev/null +++ b/DelugeRPCClient.Net/DelugeClientConfig.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DelugeRPCClient.Net +{ + public class DelugeClientConfig + { + public DelugeClientConfig() { } + + public bool IgnoreSslErrors { get; set; } = true; + + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(3); + } +} diff --git a/README.md b/README.md index 8b87e72..a74a98c 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,17 @@ PM> Install-Package DelugeRPCClient.Net -Version x.x.x DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); ``` +## Create a DelugeClient Object with advanced options +```C# +DelugeClientConfig config = new DelugeClientConfig() +{ + IgnoreSslErrors = true, + Timeout = new TimeSpan(0, 0, 30) +}; + +DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword, config); +``` + ## Authentification #### Login diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ad701cd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +services: + deluge: + image: lscr.io/linuxserver/deluge:latest + container_name: deluge + volumes: + - ./docker/config:/config + - ./docker/downloads:/downloads + environment: + - PUID=1000 + - PGID=1000 + - TZ=Etc/UTC + - DELUGE_LOGLEVEL=error #optional + ports: + - 8112:8112 + - 6881:6881 + - 6881:6881/udp \ No newline at end of file