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>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Returns(true)
.Callback(PrepareClientToReturnQueuedItem);
Mocker.GetMock<IDownloadStationProxy>()
.Setup(s => s.AddTorrentFromData(It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Returns(true)
.Callback(PrepareClientToReturnQueuedItem);
}
@ -467,7 +465,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
}
[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();
GivenSharedFolder();
@ -481,7 +479,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
}
[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();
GivenSharedFolder();
@ -495,7 +493,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
}
[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();
GivenSharedFolder();
@ -509,7 +507,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
}
[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();
GivenSharedFolder();

@ -117,13 +117,16 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
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);
return;
}
_logger.Error("Failed to remove {0}", downloadId);
catch (DownloadClientException e)
{
_logger.Error(e);
}
}
protected OsPath GetOutputPath(OsPath outputPath, DownloadStationTorrent torrent, string serialNumber)
@ -141,9 +144,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
{
var hashedSerialNumber = _serialNumberProvider.GetSerialNumber(Settings);
if (_proxy.AddTorrentFromUrl(magnetLink, GetDownloadDirectory(), Settings))
{
var item = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == magnetLink).SingleOrDefault();
_proxy.AddTorrentFromUrl(magnetLink, GetDownloadDirectory(), Settings);
var item = _proxy.GetTorrents(Settings).SingleOrDefault(t => t.Additional.Detail["uri"] == magnetLink);
if (item != null)
{
@ -152,7 +155,6 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
}
_logger.Debug("No such task {0} in Download Station", magnetLink);
}
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);
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();
@ -174,7 +176,6 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
}
_logger.Debug("No such task {0} in Download Station", filename);
}
throw new DownloadClientException("Failed to add torrent task to Download Station");
}

@ -26,14 +26,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "method", "getinfo" }
};
var response = ProcessRequest<DSMInfoResponse>(DiskStationApi.DSMInfo, arguments, settings);
if (response.Success == true)
{
var response = ProcessRequest<DSMInfoResponse>(DiskStationApi.DSMInfo, arguments, settings, "get serial number");
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,
Dictionary<string, object> arguments,
DownloadStationSettings settings,
string operation,
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,
Dictionary<string, object> arguments,
DownloadStationSettings settings,
string operation,
HttpMethod method = HttpMethod.GET,
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 response = _httpClient.Execute(request);
_logger.Debug("Trying to {0}", operation);
if (response.StatusCode == HttpStatusCode.OK)
{
var responseContent = Json.Deserialize<DiskStationResponse<T>>(response.Content);
if (!responseContent.Success && responseContent.Error.SessionError)
if (responseContent.Success)
{
_authenticated = false;
return ProcessRequest<T>(api, arguments, settings, method, retries++);
return responseContent;
}
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
@ -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" },
};
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"];
var infoResponeDSInfo = infoResponse.Data["SYNO.DownloadStation.Info"];
@ -199,10 +209,5 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
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);
Dictionary<string, object> GetConfig(DownloadStationSettings settings);
bool RemoveTorrent(string downloadId, bool deleteData, DownloadStationSettings settings);
bool AddTorrentFromUrl(string url, string downloadDirectory, DownloadStationSettings settings);
bool AddTorrentFromData(byte[] torrentData, string filename, string downloadDirectory, DownloadStationSettings settings);
void RemoveTorrent(string downloadId, bool deleteData, DownloadStationSettings settings);
void AddTorrentFromUrl(string url, string downloadDirectory, DownloadStationSettings settings);
void AddTorrentFromData(byte[] torrentData, string filename, string downloadDirectory, 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>
{
@ -41,12 +41,10 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
arguments.Add("file", new Dictionary<string, object>() { { "name", filename }, { "data", torrentData } });
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, HttpMethod.POST);
return response.Success;
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"add torrent from data {filename}", HttpMethod.POST);
}
public bool AddTorrentFromUrl(string torrentUrl, string downloadDirectory, DownloadStationSettings settings)
public void AddTorrentFromUrl(string torrentUrl, string downloadDirectory, DownloadStationSettings settings)
{
var arguments = new Dictionary<string, object>
{
@ -61,9 +59,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
arguments.Add("destination", downloadDirectory);
}
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, HttpMethod.GET);
return response.Success;
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"add torrent from url {torrentUrl}", HttpMethod.GET);
}
public IEnumerable<DownloadStationTorrent> GetTorrents(DownloadStationSettings settings)
@ -76,15 +72,18 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "additional", "detail,transfer" }
};
var response = ProcessRequest<DownloadStationTaskInfoResponse>(DiskStationApi.DownloadStationTask, arguments, settings);
if (response.Success)
try
{
var response = ProcessRequest<DownloadStationTaskInfoResponse>(DiskStationApi.DownloadStationTask, arguments, settings, "get torrents");
return response.Data.Tasks.Where(t => t.Type == DownloadStationTaskType.BT);
}
catch (DownloadClientException e)
{
_logger.Error(e);
return new List<DownloadStationTorrent>();
}
}
public Dictionary<string, object> GetConfig(DownloadStationSettings settings)
{
@ -95,20 +94,12 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "method", "getconfig" }
};
try
{
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");
var response = ProcessRequest<Dictionary<string, object>>(DiskStationApi.DownloadStationInfo, arguments, settings, "get config");
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>
{
@ -119,25 +110,10 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "force_complete", false }
};
try
{
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings);
if (response.Success)
{
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"remove item {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)
{
return base.GetApiVersion(settings, DiskStationApi.DownloadStationInfo);

@ -47,14 +47,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "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();
}
throw new DownloadClientException($"Failed to get info of {0}", path);
}
}
}

Loading…
Cancel
Save