/// <summary>
 /// Toggles the sequential download.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task ToggleSequentialDownloadAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     CancellationToken token = default)
 {
     ValidateHash(hash);
     return(client.ToggleSequentialDownloadAsync(new[] { hash }, token));
 }
 /// <summary>
 /// Toggles the first and last piece priority.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task ToggleFirstLastPiecePrioritizedAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     CancellationToken token = default)
 {
     ValidateHash(hash);
     return(client.ToggleFirstLastPiecePrioritizedAsync(new[] { hash }, token));
 }
 /// <summary>
 /// Sets the torrent download speed limit.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="limit">The limit.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task SetTorrentDownloadLimitAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     long limit,
     CancellationToken token = default)
 {
     ValidateHash(hash);
     return(client.SetTorrentDownloadLimitAsync(new[] { hash }, limit, token));
 }
 /// <summary>
 /// Sets the torrent category.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="category">The category.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task SetTorrentCategoryAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     [NotNull] string category,
     CancellationToken token = default)
 {
     ValidateHash(hash);
     return(client.SetTorrentCategoryAsync(new[] { hash }, category, token));
 }
 /// <summary>
 /// Sets the super seeding.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="enabled"></param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task SetSuperSeedingAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     bool enabled,
     CancellationToken token = default)
 {
     ValidateHash(hash);
     return(client.SetSuperSeedingAsync(new[] { hash }, enabled, token));
 }
 /// <summary>
 /// Deletes the torrent.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="deleteDownloadedData"><see langword="true"/> to delete the downloaded data.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task DeleteAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     bool deleteDownloadedData = false,
     CancellationToken token   = default)
 {
     ValidateHash(hash);
     return(client.DeleteAsync(new[] { hash }, deleteDownloadedData, token));
 }
 /// <summary>
 /// Changes the torrent priority.
 /// </summary>
 /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
 /// <param name="hash">The torrent hash.</param>
 /// <param name="change">The priority change.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns></returns>
 public static Task ChangeTorrentPriorityAsync(
     [NotNull] this IQBittorrentClient client,
     [NotNull] string hash,
     TorrentPriorityChange change,
     CancellationToken token = default)
 {
     ValidateHash(hash);
     return(client.ChangeTorrentPriorityAsync(new[] { hash }, change, token));
 }
 protected async Task WarnAutomaticTorrentManagement(IQBittorrentClient client, IConsole console)
 {
     if (AutomaticTorrentManagement != null &&
         await client.GetApiVersionAsync() < new ApiVersion(2, 2))
     {
         console.WriteLineColored(
             "automatic-torrent-management option is ignored by qBittorrent versions earlier than 4.1.5.",
             ColorScheme.Current.Warning);
     }
 }
        /// <summary>
        /// Gets the torrent download speed limit.
        /// </summary>
        /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
        /// <param name="hash">The torrent hash.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns></returns>
        public static Task <long?> GetTorrentDownloadLimitAsync(
            [NotNull] this IQBittorrentClient client,
            [NotNull] string hash,
            CancellationToken token = default)
        {
            ValidateHash(hash);
            return(ExecuteAsync());

            async Task <long?> ExecuteAsync()
            {
                var dict = await client.GetTorrentDownloadLimitAsync(new[] { hash }, token).ConfigureAwait(false);

                return(dict?.Values?.SingleOrDefault());
            }
        }
        /// <summary>
        /// Deletes the category.
        /// </summary>
        /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
        /// <param name="category">The category name.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns></returns>
        public static Task DeleteCategoryAsync(
            [NotNull] this IQBittorrentClient client,
            [NotNull] string category,
            CancellationToken token = default)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }
            if (string.IsNullOrWhiteSpace(category))
            {
                throw new ArgumentException("The category cannot be empty.", nameof(category));
            }

            return(client.DeleteCategoriesAsync(new[] { category }, token));
        }
        /// <summary>
        /// Adds the tracker to the torrent.
        /// </summary>
        /// <param name="client">An <see cref="IQBittorrentClient"/> instance.</param>
        /// <param name="hash">The torrent hash.</param>
        /// <param name="tracker">The tracker.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns></returns>
        public static Task AddTrackerAsync(
            [NotNull] this IQBittorrentClient client,
            [NotNull] string hash,
            [NotNull] Uri tracker,
            CancellationToken token = default)
        {
            if (tracker == null)
            {
                throw new ArgumentNullException(nameof(tracker));
            }
            if (!tracker.IsAbsoluteUri)
            {
                throw new ArgumentException("The URI must be absolute.", nameof(tracker));
            }

            return(client.AddTrackersAsync(hash, new[] { tracker }, token));
        }