Пример #1
0
        /// <summary>
        /// Add torrent (API: torrent-add)
        /// </summary>
        /// <returns>Torrent info (ID, Name and HashString)</returns>
        public NewTorrentInfo AddTorrent(NewTorrent torrent)
        {
            if (String.IsNullOrWhiteSpace(torrent.Metainfo) && String.IsNullOrWhiteSpace(torrent.Filename))
            {
                throw new Exception("Either \"filename\" or \"metainfo\" must be included.");
            }

            var request  = new TransmissionRequest("torrent-add", torrent.ToDictionary());
            var response = SendRequest(request);
            var jObject  = response.Deserialize <JObject>();

            if (jObject == null || jObject.First == null)
            {
                return(null);
            }

            NewTorrentInfo result = null;
            JToken         value  = null;

            if (jObject.TryGetValue("torrent-duplicate", out value))
            {
                result = JsonConvert.DeserializeObject <NewTorrentInfo>(value.ToString());
            }
            else if (jObject.TryGetValue("torrent-added", out value))
            {
                result = JsonConvert.DeserializeObject <NewTorrentInfo>(value.ToString());
            }

            return(result);
        }
        /// <summary>
        /// Add torrent (API: torrent-add)
        /// </summary>
        /// <param name="torrent"></param>
        /// <returns>Torrent info (ID, Name and HashString)</returns>
        public async Task <CreatedTorrent> TorrentAddAsync(NewTorrent torrent)
        {
            if (string.IsNullOrWhiteSpace(torrent.MetaInfo) && string.IsNullOrWhiteSpace(torrent.Filename))
            {
                throw new Exception("Either \"filename\" or \"metainfo\" must be included.");
            }

            var request = new TransmissionRequest("torrent-add", torrent.ToDictionary());

            TransmissionResponse response = await SendRequestAsync(request);

            JObject jObject = response.Deserialize <JObject>();

            if (jObject?.First == null)
            {
                return(null);
            }

            if (jObject.TryGetValue("torrent-duplicate", out JToken value))
            {
                return(JsonConvert.DeserializeObject <CreatedTorrent>(value.ToString()));
            }

            if (jObject.TryGetValue("torrent-added", out value))
            {
                return(JsonConvert.DeserializeObject <CreatedTorrent>(value.ToString()));
            }

            return(null);
        }