Throw exception with error message return by diskstation (#1672)

pull/4/head
Marcelo Castagna 8 years ago committed by Taloth
parent d7aa23388e
commit cf306f4aba

@ -332,12 +332,10 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
Mocker.GetMock<IDownloadStationProxy>() Mocker.GetMock<IDownloadStationProxy>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>())) .Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Returns(true)
.Callback(PrepareClientToReturnQueuedItem); .Callback(PrepareClientToReturnQueuedItem);
Mocker.GetMock<IDownloadStationProxy>() Mocker.GetMock<IDownloadStationProxy>()
.Setup(s => s.AddTorrentFromData(It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>())) .Setup(s => s.AddTorrentFromData(It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Returns(true)
.Callback(PrepareClientToReturnQueuedItem); .Callback(PrepareClientToReturnQueuedItem);
} }
@ -467,7 +465,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
} }
[Test] [Test]
public void GetItems_should_set_outputhPath_to_base_folder_when_single_file_non_finished_torrent() public void GetItems_should_set_outputPath_to_base_folder_when_single_file_non_finished_torrent()
{ {
GivenSerialNumber(); GivenSerialNumber();
GivenSharedFolder(); GivenSharedFolder();
@ -481,7 +479,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
} }
[Test] [Test]
public void GetItems_should_set_outputhPath_to_torrent_folder_when_multiple_files_non_finished_torrent() public void GetItems_should_set_outputPath_to_torrent_folder_when_multiple_files_non_finished_torrent()
{ {
GivenSerialNumber(); GivenSerialNumber();
GivenSharedFolder(); GivenSharedFolder();
@ -495,7 +493,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
} }
[Test] [Test]
public void GetItems_should_set_outputhPath_to_base_folder_when_single_file_finished_torrent() public void GetItems_should_set_outputPath_to_base_folder_when_single_file_finished_torrent()
{ {
GivenSerialNumber(); GivenSerialNumber();
GivenSharedFolder(); GivenSharedFolder();
@ -509,7 +507,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
} }
[Test] [Test]
public void GetItems_should_set_outputhPath_to_torrent_folder_when_multiple_files_finished_torrent() public void GetItems_should_set_outputPath_to_torrent_folder_when_multiple_files_finished_torrent()
{ {
GivenSerialNumber(); GivenSerialNumber();
GivenSharedFolder(); GivenSharedFolder();

@ -117,13 +117,16 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
public override void RemoveItem(string downloadId, bool deleteData) public override void RemoveItem(string downloadId, bool deleteData)
{ {
if (_proxy.RemoveTorrent(ParseDownloadId(downloadId), deleteData, Settings)) try
{ {
_proxy.RemoveTorrent(ParseDownloadId(downloadId), deleteData, Settings);
_logger.Debug("{0} removed correctly", downloadId); _logger.Debug("{0} removed correctly", downloadId);
return; return;
} }
catch (DownloadClientException e)
_logger.Error("Failed to remove {0}", downloadId); {
_logger.Error(e);
}
} }
protected OsPath GetOutputPath(OsPath outputPath, DownloadStationTorrent torrent, string serialNumber) protected OsPath GetOutputPath(OsPath outputPath, DownloadStationTorrent torrent, string serialNumber)
@ -141,9 +144,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
{ {
var hashedSerialNumber = _serialNumberProvider.GetSerialNumber(Settings); var hashedSerialNumber = _serialNumberProvider.GetSerialNumber(Settings);
if (_proxy.AddTorrentFromUrl(magnetLink, GetDownloadDirectory(), Settings)) _proxy.AddTorrentFromUrl(magnetLink, GetDownloadDirectory(), Settings);
{
var item = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == magnetLink).SingleOrDefault(); var item = _proxy.GetTorrents(Settings).SingleOrDefault(t => t.Additional.Detail["uri"] == magnetLink);
if (item != null) if (item != null)
{ {
@ -152,7 +155,6 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
} }
_logger.Debug("No such task {0} in Download Station", magnetLink); _logger.Debug("No such task {0} in Download Station", magnetLink);
}
throw new DownloadClientException("Failed to add magnet task to Download Station"); throw new DownloadClientException("Failed to add magnet task to Download Station");
} }
@ -161,8 +163,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
{ {
var hashedSerialNumber = _serialNumberProvider.GetSerialNumber(Settings); var hashedSerialNumber = _serialNumberProvider.GetSerialNumber(Settings);
if (_proxy.AddTorrentFromData(fileContent, filename, GetDownloadDirectory(), Settings)) _proxy.AddTorrentFromData(fileContent, filename, GetDownloadDirectory(), Settings);
{
var items = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == Path.GetFileNameWithoutExtension(filename)); var items = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == Path.GetFileNameWithoutExtension(filename));
var item = items.SingleOrDefault(); var item = items.SingleOrDefault();
@ -174,7 +176,6 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
} }
_logger.Debug("No such task {0} in Download Station", filename); _logger.Debug("No such task {0} in Download Station", filename);
}
throw new DownloadClientException("Failed to add torrent task to Download Station"); throw new DownloadClientException("Failed to add torrent task to Download Station");
} }

@ -26,14 +26,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "method", "getinfo" } { "method", "getinfo" }
}; };
var response = ProcessRequest<DSMInfoResponse>(DiskStationApi.DSMInfo, arguments, settings); var response = ProcessRequest<DSMInfoResponse>(DiskStationApi.DSMInfo, arguments, settings, "get serial number");
if (response.Success == true)
{
return response.Data.SerialNumber; return response.Data.SerialNumber;
} }
_logger.Debug("Failed to get Download Station serial number");
throw new DownloadClientException("Failed to get Download Station serial number");
}
} }
} }

@ -34,14 +34,16 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
protected DiskStationResponse<object> ProcessRequest(DiskStationApi api, protected DiskStationResponse<object> ProcessRequest(DiskStationApi api,
Dictionary<string, object> arguments, Dictionary<string, object> arguments,
DownloadStationSettings settings, DownloadStationSettings settings,
string operation,
HttpMethod method = HttpMethod.GET) HttpMethod method = HttpMethod.GET)
{ {
return ProcessRequest<object>(api, arguments, settings, method); return ProcessRequest<object>(api, arguments, settings, operation, method);
} }
protected DiskStationResponse<T> ProcessRequest<T>(DiskStationApi api, protected DiskStationResponse<T> ProcessRequest<T>(DiskStationApi api,
Dictionary<string, object> arguments, Dictionary<string, object> arguments,
DownloadStationSettings settings, DownloadStationSettings settings,
string operation,
HttpMethod method = HttpMethod.GET, HttpMethod method = HttpMethod.GET,
int retries = 0) where T : new() int retries = 0) where T : new()
{ {
@ -58,18 +60,28 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
var request = BuildRequest(settings, api, arguments, method); var request = BuildRequest(settings, api, arguments, method);
var response = _httpClient.Execute(request); var response = _httpClient.Execute(request);
_logger.Debug("Trying to {0}", operation);
if (response.StatusCode == HttpStatusCode.OK) if (response.StatusCode == HttpStatusCode.OK)
{ {
var responseContent = Json.Deserialize<DiskStationResponse<T>>(response.Content); var responseContent = Json.Deserialize<DiskStationResponse<T>>(response.Content);
if (!responseContent.Success && responseContent.Error.SessionError) if (responseContent.Success)
{ {
_authenticated = false; return responseContent;
return ProcessRequest<T>(api, arguments, settings, method, retries++);
} }
else else
{ {
return responseContent; if (responseContent.Error.SessionError)
{
_authenticated = false;
return ProcessRequest<T>(api, arguments, settings, operation, method, retries++);
}
var msg = $"Failed to {operation}. Reason: {responseContent.Error.GetMessage(api)}";
_logger.Error(msg);
throw new DownloadClientException(msg);
} }
} }
else else
@ -166,10 +178,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "query", "SYNO.API.Auth, SYNO.DownloadStation.Info, SYNO.DownloadStation.Task, SYNO.FileStation.List, SYNO.DSM.Info" }, { "query", "SYNO.API.Auth, SYNO.DownloadStation.Info, SYNO.DownloadStation.Task, SYNO.FileStation.List, SYNO.DSM.Info" },
}; };
var infoResponse = ProcessRequest<DiskStationApiInfoResponse>(DiskStationApi.Info, arguments, settings); var infoResponse = ProcessRequest<DiskStationApiInfoResponse>(DiskStationApi.Info, arguments, settings, "Get api version");
if (infoResponse.Success == true)
{
//TODO: Refactor this into more elegant code //TODO: Refactor this into more elegant code
var infoResponeDSAuth = infoResponse.Data["SYNO.API.Auth"]; var infoResponeDSAuth = infoResponse.Data["SYNO.API.Auth"];
var infoResponeDSInfo = infoResponse.Data["SYNO.DownloadStation.Info"]; var infoResponeDSInfo = infoResponse.Data["SYNO.DownloadStation.Info"];
@ -199,10 +209,5 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
throw new DownloadClientException("Api not implemented"); throw new DownloadClientException("Api not implemented");
} }
} }
else
{
throw new DownloadClientException(infoResponse.Error.GetMessage(DiskStationApi.Info));
}
}
} }
} }

@ -12,9 +12,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ {
IEnumerable<DownloadStationTorrent> GetTorrents(DownloadStationSettings settings); IEnumerable<DownloadStationTorrent> GetTorrents(DownloadStationSettings settings);
Dictionary<string, object> GetConfig(DownloadStationSettings settings); Dictionary<string, object> GetConfig(DownloadStationSettings settings);
bool RemoveTorrent(string downloadId, bool deleteData, DownloadStationSettings settings); void RemoveTorrent(string downloadId, bool deleteData, DownloadStationSettings settings);
bool AddTorrentFromUrl(string url, string downloadDirectory, DownloadStationSettings settings); void AddTorrentFromUrl(string url, string downloadDirectory, DownloadStationSettings settings);
bool AddTorrentFromData(byte[] torrentData, string filename, string downloadDirectory, DownloadStationSettings settings); void AddTorrentFromData(byte[] torrentData, string filename, string downloadDirectory, DownloadStationSettings settings);
IEnumerable<int> GetApiVersion(DownloadStationSettings settings); IEnumerable<int> GetApiVersion(DownloadStationSettings settings);
} }
@ -25,7 +25,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ {
} }
public bool AddTorrentFromData(byte[] torrentData, string filename, string downloadDirectory, DownloadStationSettings settings) public void AddTorrentFromData(byte[] torrentData, string filename, string downloadDirectory, DownloadStationSettings settings)
{ {
var arguments = new Dictionary<string, object> var arguments = new Dictionary<string, object>
{ {
@ -41,12 +41,10 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
arguments.Add("file", new Dictionary<string, object>() { { "name", filename }, { "data", torrentData } }); arguments.Add("file", new Dictionary<string, object>() { { "name", filename }, { "data", torrentData } });
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, HttpMethod.POST); var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"add torrent from data {filename}", HttpMethod.POST);
return response.Success;
} }
public bool AddTorrentFromUrl(string torrentUrl, string downloadDirectory, DownloadStationSettings settings) public void AddTorrentFromUrl(string torrentUrl, string downloadDirectory, DownloadStationSettings settings)
{ {
var arguments = new Dictionary<string, object> var arguments = new Dictionary<string, object>
{ {
@ -61,9 +59,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
arguments.Add("destination", downloadDirectory); arguments.Add("destination", downloadDirectory);
} }
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, HttpMethod.GET); var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"add torrent from url {torrentUrl}", HttpMethod.GET);
return response.Success;
} }
public IEnumerable<DownloadStationTorrent> GetTorrents(DownloadStationSettings settings) public IEnumerable<DownloadStationTorrent> GetTorrents(DownloadStationSettings settings)
@ -76,15 +72,18 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "additional", "detail,transfer" } { "additional", "detail,transfer" }
}; };
var response = ProcessRequest<DownloadStationTaskInfoResponse>(DiskStationApi.DownloadStationTask, arguments, settings); try
if (response.Success)
{ {
var response = ProcessRequest<DownloadStationTaskInfoResponse>(DiskStationApi.DownloadStationTask, arguments, settings, "get torrents");
return response.Data.Tasks.Where(t => t.Type == DownloadStationTaskType.BT); return response.Data.Tasks.Where(t => t.Type == DownloadStationTaskType.BT);
} }
catch (DownloadClientException e)
{
_logger.Error(e);
return new List<DownloadStationTorrent>(); return new List<DownloadStationTorrent>();
} }
}
public Dictionary<string, object> GetConfig(DownloadStationSettings settings) public Dictionary<string, object> GetConfig(DownloadStationSettings settings)
{ {
@ -95,20 +94,12 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "method", "getconfig" } { "method", "getconfig" }
}; };
try var response = ProcessRequest<Dictionary<string, object>>(DiskStationApi.DownloadStationInfo, arguments, settings, "get config");
{
var response = ProcessRequest<Dictionary<string, object>>(DiskStationApi.DownloadStationInfo, arguments, settings);
return response.Data;
}
catch (Exception ex)
{
_logger.Error(ex, "Failed to get config from Download Station");
throw; return response.Data;
}
} }
public bool RemoveTorrent(string downloadId, bool deleteData, DownloadStationSettings settings) public void RemoveTorrent(string downloadId, bool deleteData, DownloadStationSettings settings)
{ {
var arguments = new Dictionary<string, object> var arguments = new Dictionary<string, object>
{ {
@ -119,25 +110,10 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "force_complete", false } { "force_complete", false }
}; };
try var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"remove item {downloadId}");
{
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings);
if (response.Success)
{
_logger.Trace("Item {0} removed from Download Station", downloadId); _logger.Trace("Item {0} removed from Download Station", downloadId);
} }
return response.Success;
}
catch (DownloadClientException e)
{
_logger.Debug(e, "Failed to remove item {0} from Download Station", downloadId);
throw;
}
}
public IEnumerable<int> GetApiVersion(DownloadStationSettings settings) public IEnumerable<int> GetApiVersion(DownloadStationSettings settings)
{ {
return base.GetApiVersion(settings, DiskStationApi.DownloadStationInfo); return base.GetApiVersion(settings, DiskStationApi.DownloadStationInfo);

@ -47,14 +47,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "additional", $"[\"real_path\"]" } { "additional", $"[\"real_path\"]" }
}; };
var response = ProcessRequest<FileStationListResponse>(DiskStationApi.FileStationList, arguments, settings); var response = ProcessRequest<FileStationListResponse>(DiskStationApi.FileStationList, arguments, settings, $"get info of {path}");
if (response.Success == true)
{
return response.Data.Files.First(); return response.Data.Files.First();
} }
throw new DownloadClientException($"Failed to get info of {0}", path);
}
} }
} }

Loading…
Cancel
Save