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

pull/1678/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,19 +144,18 @@ 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();
if (item != null) var item = _proxy.GetTorrents(Settings).SingleOrDefault(t => t.Additional.Detail["uri"] == magnetLink);
{
_logger.Debug("{0} added correctly", remoteEpisode);
return CreateDownloadId(item.Id, hashedSerialNumber);
}
_logger.Debug("No such task {0} in Download Station", magnetLink); if (item != null)
{
_logger.Debug("{0} added correctly", remoteEpisode);
return CreateDownloadId(item.Id, hashedSerialNumber);
} }
_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,21 +163,20 @@ 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 item = items.SingleOrDefault(); var items = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == Path.GetFileNameWithoutExtension(filename));
if (item != null) var item = items.SingleOrDefault();
{
_logger.Debug("{0} added correctly", remoteEpisode);
return CreateDownloadId(item.Id, hashedSerialNumber);
}
_logger.Debug("No such task {0} in Download Station", filename); if (item != null)
{
_logger.Debug("{0} added correctly", remoteEpisode);
return CreateDownloadId(item.Id, hashedSerialNumber);
} }
_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");
return response.Data.SerialNumber;
if (response.Success == true)
{
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,42 +178,35 @@ 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
{ var infoResponeDSAuth = infoResponse.Data["SYNO.API.Auth"];
//TODO: Refactor this into more elegant code var infoResponeDSInfo = infoResponse.Data["SYNO.DownloadStation.Info"];
var infoResponeDSAuth = infoResponse.Data["SYNO.API.Auth"]; var infoResponeDSTask = infoResponse.Data["SYNO.DownloadStation.Task"];
var infoResponeDSInfo = infoResponse.Data["SYNO.DownloadStation.Info"]; var infoResponseFSList = infoResponse.Data["SYNO.FileStation.List"];
var infoResponeDSTask = infoResponse.Data["SYNO.DownloadStation.Task"]; var infoResponseDSMInfo = infoResponse.Data["SYNO.DSM.Info"];
var infoResponseFSList = infoResponse.Data["SYNO.FileStation.List"];
var infoResponseDSMInfo = infoResponse.Data["SYNO.DSM.Info"]; Resources[DiskStationApi.Auth] = infoResponeDSAuth.Path;
Resources[DiskStationApi.DownloadStationInfo] = infoResponeDSInfo.Path;
Resources[DiskStationApi.Auth] = infoResponeDSAuth.Path; Resources[DiskStationApi.DownloadStationTask] = infoResponeDSTask.Path;
Resources[DiskStationApi.DownloadStationInfo] = infoResponeDSInfo.Path; Resources[DiskStationApi.FileStationList] = infoResponseFSList.Path;
Resources[DiskStationApi.DownloadStationTask] = infoResponeDSTask.Path; Resources[DiskStationApi.DSMInfo] = infoResponseDSMInfo.Path;
Resources[DiskStationApi.FileStationList] = infoResponseFSList.Path;
Resources[DiskStationApi.DSMInfo] = infoResponseDSMInfo.Path; switch (api)
switch (api)
{
case DiskStationApi.Auth:
return Enumerable.Range(infoResponeDSAuth.MinVersion, infoResponeDSAuth.MaxVersion - infoResponeDSAuth.MinVersion + 1);
case DiskStationApi.DownloadStationInfo:
return Enumerable.Range(infoResponeDSInfo.MinVersion, infoResponeDSInfo.MaxVersion - infoResponeDSInfo.MinVersion + 1);
case DiskStationApi.DownloadStationTask:
return Enumerable.Range(infoResponeDSTask.MinVersion, infoResponeDSTask.MaxVersion - infoResponeDSTask.MinVersion + 1);
case DiskStationApi.FileStationList:
return Enumerable.Range(infoResponseFSList.MinVersion, infoResponseFSList.MaxVersion - infoResponseFSList.MinVersion + 1);
case DiskStationApi.DSMInfo:
return Enumerable.Range(infoResponseDSMInfo.MinVersion, infoResponseDSMInfo.MaxVersion - infoResponseDSMInfo.MinVersion + 1);
default:
throw new DownloadClientException("Api not implemented");
}
}
else
{ {
throw new DownloadClientException(infoResponse.Error.GetMessage(DiskStationApi.Info)); case DiskStationApi.Auth:
return Enumerable.Range(infoResponeDSAuth.MinVersion, infoResponeDSAuth.MaxVersion - infoResponeDSAuth.MinVersion + 1);
case DiskStationApi.DownloadStationInfo:
return Enumerable.Range(infoResponeDSInfo.MinVersion, infoResponeDSInfo.MaxVersion - infoResponeDSInfo.MinVersion + 1);
case DiskStationApi.DownloadStationTask:
return Enumerable.Range(infoResponeDSTask.MinVersion, infoResponeDSTask.MaxVersion - infoResponeDSTask.MinVersion + 1);
case DiskStationApi.FileStationList:
return Enumerable.Range(infoResponseFSList.MinVersion, infoResponseFSList.MaxVersion - infoResponseFSList.MinVersion + 1);
case DiskStationApi.DSMInfo:
return Enumerable.Range(infoResponseDSMInfo.MinVersion, infoResponseDSMInfo.MaxVersion - infoResponseDSMInfo.MinVersion + 1);
default:
throw new DownloadClientException("Api not implemented");
} }
} }
} }

@ -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,14 +72,17 @@ 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)
return new List<DownloadStationTorrent>(); {
_logger.Error(e);
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,23 +110,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "force_complete", false } { "force_complete", false }
}; };
try var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"remove item {downloadId}");
{ _logger.Trace("Item {0} removed from Download Station", downloadId);
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings);
if (response.Success)
{
_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)

@ -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