Skip to content

Commit

Permalink
New calls (#2)
Browse files Browse the repository at this point in the history
* NEW! Add ListConfigs()

- Model
- Call
- Test
- Readme

* NEW! Add RecheckTorrents()

- Call
- Test
- Readme

* NEW! Add AddTorrentByUrl()

- DelugeResponse
- Call
- Test
- Constants
- Readme

* NEW! Add AddTorrentByUrl()

- TorrentExtended
- Calls
- Test
- Readme

---------

Co-authored-by: Rafael Calafell Cladera <[email protected]>
  • Loading branch information
rafacc87 and rafaelkan authored Oct 29, 2024
1 parent c39efc6 commit 3c62189
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 1 deletion.
29 changes: 29 additions & 0 deletions DelugeRPCClient.Net.Tests/ConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using DelugeRPCClient.Net.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DelugeRPCClient.Net.Tests
{
[TestClass]
public class ConfigTests
{
[TestMethod]
public async Task ListConfigs()
{
DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword);

bool loginResult = await client.Login();
Assert.IsTrue(loginResult);

Config configs = await client.ListConfigs();
Assert.IsNotNull(configs);

bool logoutResult = await client.Logout();
Assert.IsTrue(logoutResult);
}
}
}
1 change: 1 addition & 0 deletions DelugeRPCClient.Net.Tests/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ internal class Constants
internal const string TestLabelName = "testlabel";
internal const string TorrentMagnet = "magnet:?xt=urn:btih:30987c19cf0eae3cf47766f387c621fa78a58ab9&dn=debian-9.2.1-amd64-netinst.iso";
internal const string TestTorrentFilename = "test.torrent";
internal const string TestTorrentUrl = "https://archive.org/download/die_siedler_2_151/die_siedler_2_151_archive.torrent";
}
}
61 changes: 61 additions & 0 deletions DelugeRPCClient.Net.Tests/TorrentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -32,6 +33,25 @@ public async Task ListAndGetTorrent()
Assert.IsTrue(logoutResult);
}

[TestMethod]
public async Task ListAndGetTorrentExtended()
{
DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword);

bool loginResult = await client.Login();
Assert.IsTrue(loginResult);

List<TorrentExtended> torrents = await client.ListTorrentsExtended();
Assert.IsNotNull(torrents);
Assert.AreNotEqual(0, torrents.Count);

TorrentExtended torrent = await client.GetTorrentExtended(torrents[0].Hash);
Assert.IsNotNull(torrent);

bool logoutResult = await client.Logout();
Assert.IsTrue(logoutResult);
}

[TestMethod]
public async Task AddRemoveTorrentByMagnet()
{
Expand Down Expand Up @@ -72,6 +92,26 @@ public async Task AddRemoveTorrentByFile()
Assert.IsTrue(logoutResult);
}

[TestMethod]
public async Task AddRemoveTorrentByUrl()
{
DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword);

bool loginResult = await client.Login();
Assert.IsTrue(loginResult);

Torrent torrent = await client.AddTorrentByUrl(Constants.TestTorrentUrl);
Assert.IsNotNull(torrent);

Thread.Sleep(1000);

bool removeTorrentResult = await client.RemoveTorrent(torrent.Hash);
Assert.IsTrue(removeTorrentResult);

bool logoutResult = await client.Logout();
Assert.IsTrue(logoutResult);
}

[TestMethod]
public async Task PauseResumeTorrent()
{
Expand Down Expand Up @@ -104,5 +144,26 @@ public async Task PauseResumeTorrent()
bool logoutResult = await client.Logout();
Assert.IsTrue(logoutResult);
}

[TestMethod]
public async Task RecheckTorrents()
{
DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword);

bool loginResult = await client.Login();
Assert.IsTrue(loginResult);

List<Torrent> torrents = await client.ListTorrents();
Assert.IsNotNull(torrents);
Assert.AreNotEqual(0, torrents.Count);

Torrent torrent = torrents[0];

bool? recheckResult = await client.RecheckTorrents(torrent.Hash.Split(",").ToList());
Assert.IsNull(recheckResult);

bool logoutResult = await client.Logout();
Assert.IsTrue(logoutResult);
}
}
}
7 changes: 7 additions & 0 deletions DelugeRPCClient.Net/Core/DelugeResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ internal class DelugeResponsee<T>
[JsonProperty(PropertyName = "error")]
public DelugeError Error { get; set; }
}

internal class ResultEntry
{
public bool Success { get; set; }
public string Hash { get; set; }

}
}
75 changes: 74 additions & 1 deletion DelugeRPCClient.Net/DelugeClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DelugeRPCClient.Net.Models;
using DelugeRPCClient.Net.Core;
using DelugeRPCClient.Net.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -68,6 +69,19 @@ public async Task<List<Torrent>> ListTorrents(Dictionary<string, string> filters
return result.Values.ToList();
}

/// <summary>
/// List all torrents with details optionnaly filtered
/// </summary>
/// <param name="filters">optional filters</param>
/// <returns>List of torrents</returns>
public async Task<List<TorrentExtended>> ListTorrentsExtended(Dictionary<string, string> filters = null)
{
filters = filters ?? new Dictionary<string, string>();
var keys = typeof(TorrentExtended).GetAllJsonPropertyFromType();
Dictionary<string, TorrentExtended> result = await SendRequest<Dictionary<string, TorrentExtended>>("core.get_torrents_status", filters, keys);
return result.Values.ToList();
}

/// <summary>
/// Get torrent informations by torrent hash
/// </summary>
Expand All @@ -79,6 +93,17 @@ public async Task<Torrent> GetTorrent(string hash)
return torrents.Count > 0 ? torrents[0] : null;
}

/// <summary>
/// Get torrent informations with details by torrent hash
/// </summary>
/// <param name="hash">The requested torrent hash</param>
/// <returns>the torrent object</returns>
public async Task<TorrentExtended> GetTorrentExtended(string hash)
{
List<TorrentExtended> torrents = await ListTorrentsExtended(new Dictionary<string, string>() { { "hash", hash } });
return torrents.Count > 0 ? torrents[0] : null;
}

/// <summary>
/// Add a new torrent by magnet information
/// </summary>
Expand Down Expand Up @@ -114,6 +139,31 @@ public async Task<Torrent> AddTorrentByFile(string file, TorrentOptions options
return await GetTorrent(hash);
}

/// <summary>
/// Add a new torrent by .torrent url
/// </summary>
/// <param name="file">url of the .torrent file</param>
/// <param name="options">Optional torrent options</param>
/// <returns>the torrent object</returns>
/// <exception cref="ArgumentException"></exception>
public async Task<Torrent> AddTorrentByUrl(string url, TorrentOptions options = null)
{
if (String.IsNullOrWhiteSpace(url)) throw new ArgumentException(nameof(url));
var request = CreateRequest("web.download_torrent_from_url", url, options);
request.NullValueHandling = NullValueHandling.Ignore;
string pathTemp = await SendRequest<string>(request);

request = CreateRequest("web.add_torrents", new object[] { new object[] { new { options = new object { }, path = pathTemp } } });
var resultAdd = await SendRequest<List<List<Object>>>(request);
return await GetTorrent(resultAdd.Select(item =>
new ResultEntry
{
Success = item[0] is bool success && success,
Hash = item[1] as string
}
).FirstOrDefault().Hash);
}

/// <summary>
/// Remove a torrent from deluge
/// </summary>
Expand Down Expand Up @@ -149,6 +199,29 @@ public async Task<bool> ResumeTorrent(string hash)
return result == null;
}

/// <summary>
/// Recheck torrents
/// </summary>
/// <param name="hash">Hash of the target torrents</param>
/// <returns>true if the action is successfull</returns>
public async Task<bool?> RecheckTorrents(List<string> hash)
{
return await SendRequest<bool?>("core.force_recheck", hash);
}

#endregion

#region Config

/// <summary>
/// List all existing labels
/// </summary>
/// <returns>list of labels</returns>
public async Task<Config> ListConfigs()
{
return await SendRequest<Config>("core.get_config");
}

#endregion

#region Labels
Expand Down
Loading

0 comments on commit 3c62189

Please sign in to comment.