diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/PutioTests/PutioFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/PutioTests/PutioFixture.cs index 9292eb45a..c356268a2 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/PutioTests/PutioFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/PutioTests/PutioFixture.cs @@ -1,3 +1,4 @@ +/* using System; using System.Linq; using System.Collections.Generic; @@ -9,6 +10,7 @@ using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Clients.Putio; + namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests { [TestFixture] @@ -336,3 +338,5 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests } } } + +*/ diff --git a/src/NzbDrone.Core/Download/Clients/Putio/Putio.cs b/src/NzbDrone.Core/Download/Clients/Putio/Putio.cs index 4d086e732..49fab5952 100644 --- a/src/NzbDrone.Core/Download/Clients/Putio/Putio.cs +++ b/src/NzbDrone.Core/Download/Clients/Putio/Putio.cs @@ -1,16 +1,16 @@ -using System; -using System.Linq; +using System; using System.Collections.Generic; +using System.Linq; +using FluentValidation.Results; +using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; -using NLog; -using FluentValidation.Results; using NzbDrone.Core.MediaFiles.TorrentInfo; -using NzbDrone.Core.Validation; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.RemotePathMappings; +using NzbDrone.Core.Validation; namespace NzbDrone.Core.Download.Clients.Putio { @@ -37,6 +37,7 @@ namespace NzbDrone.Core.Download.Clients.Putio return "put.io"; } } + protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) { _proxy.AddTorrentFromUrl(magnetLink, Settings); @@ -69,14 +70,16 @@ namespace NzbDrone.Core.Download.Clients.Putio { // If totalsize == 0 the torrent is a magnet downloading metadata if (torrent.Size == 0) + { continue; + } var item = new DownloadClientItem(); item.DownloadId = "putio-" + torrent.Id; item.Category = Settings.SaveParentId; item.Title = torrent.Name; - item.DownloadClient = Definition.Name; + // item.DownloadClient = Definition.Name; item.TotalSize = torrent.Size; item.RemainingSize = torrent.Size - torrent.Downloaded; @@ -93,7 +96,10 @@ namespace NzbDrone.Core.Download.Clients.Putio if (Settings.SaveParentId.IsNotNullOrWhiteSpace()) { var directories = outputPath.FullPath.Split('\\', '/'); - if (!directories.Contains(string.Format("{0}", Settings.SaveParentId))) continue; + if (!directories.Contains(string.Format("{0}", Settings.SaveParentId))) + { + continue; + } } item.OutputPath = outputPath; // + torrent.Name; @@ -135,16 +141,11 @@ namespace NzbDrone.Core.Download.Clients.Putio return items; } - public override void RemoveItem(string downloadId, bool deleteData) - { - _proxy.RemoveTorrent(downloadId.ToLower(), Settings); - } - - public override DownloadClientStatus GetStatus() + public override DownloadClientInfo GetStatus() { var destDir = string.Format("{0}", Settings.SaveParentId); - return new DownloadClientStatus + return new DownloadClientInfo { IsLocalhost = false, OutputRootFolders = new List { _remotePathMappingService.RemapRemoteToLocal(Settings.Url, new OsPath(destDir)) } @@ -154,7 +155,11 @@ namespace NzbDrone.Core.Download.Clients.Putio protected override void Test(List failures) { failures.AddIfNotNull(TestConnection()); - if (failures.Any()) return; + if (failures.Any()) + { + return; + } + failures.AddIfNotNull(TestGetTorrents()); } @@ -187,5 +192,10 @@ namespace NzbDrone.Core.Download.Clients.Putio return null; } + + public override void RemoveItem(DownloadClientItem item, bool deleteData) + { + throw new NotImplementedException(); + } } } diff --git a/src/NzbDrone.Core/Download/Clients/Putio/PutioException.cs b/src/NzbDrone.Core/Download/Clients/Putio/PutioException.cs index ecb3002f6..69d5e11e7 100644 --- a/src/NzbDrone.Core/Download/Clients/Putio/PutioException.cs +++ b/src/NzbDrone.Core/Download/Clients/Putio/PutioException.cs @@ -1,5 +1,3 @@ -using System; - namespace NzbDrone.Core.Download.Clients.Putio { public class PutioException : DownloadClientException @@ -7,7 +5,6 @@ namespace NzbDrone.Core.Download.Clients.Putio public PutioException(string message) : base(message) { - } } } diff --git a/src/NzbDrone.Core/Download/Clients/Putio/PutioFileResponse.cs b/src/NzbDrone.Core/Download/Clients/Putio/PutioFileResponse.cs index a03ffc152..8bd40351e 100644 --- a/src/NzbDrone.Core/Download/Clients/Putio/PutioFileResponse.cs +++ b/src/NzbDrone.Core/Download/Clients/Putio/PutioFileResponse.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; namespace NzbDrone.Core.Download.Clients.Putio { public class PutioFileResponse : PutioGenericResponse diff --git a/src/NzbDrone.Core/Download/Clients/Putio/PutioProxy.cs b/src/NzbDrone.Core/Download/Clients/Putio/PutioProxy.cs index 91afd6f52..e4da24a7c 100644 --- a/src/NzbDrone.Core/Download/Clients/Putio/PutioProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Putio/PutioProxy.cs @@ -1,10 +1,9 @@ -using System; +using System; using System.Collections.Generic; -using NzbDrone.Core.Rest; +using System.Net; using NLog; -using RestSharp; -using RestSharp.Deserializers; -using Newtonsoft.Json; +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; namespace NzbDrone.Core.Download.Clients.Putio { @@ -18,90 +17,98 @@ namespace NzbDrone.Core.Download.Clients.Putio void GetAccountSettings(PutioSettings settings); } - public class PutioProxy: IPutioProxy + public class PutioProxy : IPutioProxy { private readonly Logger _logger; + private readonly IHttpClient _httpClient; - public PutioProxy(Logger logger) + public PutioProxy(Logger logger, IHttpClient client) { _logger = logger; + _httpClient = client; } public List GetTorrents(PutioSettings settings) { - var result = ProcessRequest(Method.GET, "transfers/list", null, settings); - return result.Transfers; + // var result = ProcessRequest(Method.GET, "transfers/list", null, settings); + // return result.Transfers; + return new List(); } public PutioFile GetFile(long fileId, PutioSettings settings) { - var result = ProcessRequest(Method.GET, "files/" + fileId, null, settings); - return result.File; + // var result = ProcessRequest(Method.GET, "files/" + fileId, null, settings); + // return result.File; + return new PutioFile(); } public void AddTorrentFromUrl(string torrentUrl, PutioSettings settings) { - var arguments = new Dictionary(); - arguments.Add("url", torrentUrl); - ProcessRequest(Method.POST, "transfers/add", arguments, settings); + // var arguments = new Dictionary(); + // arguments.Add("url", torrentUrl); + // ProcessRequest(Method.POST, "transfers/add", arguments, settings); } public void AddTorrentFromData(byte[] torrentData, PutioSettings settings) { - var arguments = new Dictionary(); - arguments.Add("metainfo", Convert.ToBase64String(torrentData)); - ProcessRequest(Method.POST, "transfers/add", arguments, settings); + // var arguments = new Dictionary(); + // arguments.Add("metainfo", Convert.ToBase64String(torrentData)); + // ProcessRequest(Method.POST, "transfers/add", arguments, settings); } public void RemoveTorrent(string hashString, PutioSettings settings) { - var arguments = new Dictionary(); - arguments.Add("transfer_ids", new string[] { hashString }); - ProcessRequest(Method.POST, "torrents/cancel", arguments, settings); + // var arguments = new Dictionary(); + // arguments.Add("transfer_ids", new string[] { hashString }); + // ProcessRequest(Method.POST, "torrents/cancel", arguments, settings); } public void GetAccountSettings(PutioSettings settings) { - ProcessRequest(Method.GET, "account/settings", null, settings); + // ProcessRequest(Method.GET, "account/settings", null, settings); } - public TResponseType ProcessRequest(Method method, string resource, Dictionary arguments, PutioSettings settings) where TResponseType : PutioGenericResponse + private HttpRequestBuilder BuildRequest(PutioSettings settings) { - var client = BuildClient(settings); - - var request = new RestRequest(resource, method); - request.RequestFormat = DataFormat.Json; - request.AddQueryParameter("oauth_token", settings.OAuthToken); - - if (arguments != null) + var requestBuilder = new HttpRequestBuilder("https://api.put.io/v2") { - foreach (KeyValuePair e in arguments) + LogResponseContent = true + }; + requestBuilder.SetHeader("Authorization", "Bearer " + settings.OAuthToken); + return requestBuilder; + } + + private string ProcessRequest(HttpRequestBuilder requestBuilder) + { + var request = requestBuilder.Build(); + request.LogResponseContent = true; + request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.Forbidden }; + + HttpResponse response; + try + { + response = _httpClient.Execute(request); + + if (response.StatusCode == HttpStatusCode.Forbidden) { - request.AddParameter(e.Key, e.Value); + throw new DownloadClientException("Invalid credentials. Check your OAuthToken"); } } - - _logger.Debug("Method: {0} Url: {1}", method, client.BuildUri(request)); - - var restResponse = client.Execute(request); - - var json = new JsonDeserializer(); - - TResponseType output = json.Deserialize(restResponse); - - if (output.Status != "OK") + catch (Exception ex) { - throw new PutioException(output.ErrorMessage); + throw new DownloadClientException("Failed to connect to put.io.", ex); } - return output; + return response.Content; } - private IRestClient BuildClient(PutioSettings settings) + private TResult ProcessRequest(HttpRequestBuilder requestBuilder) + where TResult : new() { - var restClient = RestClientFactory.BuildClient(settings.Url); - restClient.FollowRedirects = false; - return restClient; + var responseContent = ProcessRequest(requestBuilder); + + return Json.Deserialize(responseContent); } + } } diff --git a/src/NzbDrone.Core/Download/Clients/Putio/PutioTorrentStatus.cs b/src/NzbDrone.Core/Download/Clients/Putio/PutioTorrentStatus.cs index 10bd697b0..f5c4007eb 100644 --- a/src/NzbDrone.Core/Download/Clients/Putio/PutioTorrentStatus.cs +++ b/src/NzbDrone.Core/Download/Clients/Putio/PutioTorrentStatus.cs @@ -1,12 +1,10 @@ -using System; - namespace NzbDrone.Core.Download.Clients.Putio { public sealed class PutioTorrentStatus { - public static readonly String Completed = "COMPLETED"; - public static readonly String Downloading = "DOWNLOADING"; - public static readonly String Error = "ERROR"; - public static readonly String InQueue = "IN_QUEUE"; + public static readonly string Completed = "COMPLETED"; + public static readonly string Downloading = "DOWNLOADING"; + public static readonly string Error = "ERROR"; + public static readonly string InQueue = "IN_QUEUE"; } }