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

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

@ -331,13 +331,11 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), new byte[1000]));
Mocker.GetMock<IDownloadStationProxy>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Returns(true)
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Callback(PrepareClientToReturnQueuedItem);
Mocker.GetMock<IDownloadStationProxy>()
.Setup(s => s.AddTorrentFromData(It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.Returns(true)
.Setup(s => s.AddTorrentFromData(It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DownloadStationSettings>()))
.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,19 +144,18 @@ 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);
if (item != null)
{
_logger.Debug("{0} added correctly", remoteEpisode);
return CreateDownloadId(item.Id, hashedSerialNumber);
}
var item = _proxy.GetTorrents(Settings).SingleOrDefault(t => t.Additional.Detail["uri"] == magnetLink);
_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");
}
@ -161,21 +163,20 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
{
var hashedSerialNumber = _serialNumberProvider.GetSerialNumber(Settings);
if (_proxy.AddTorrentFromData(fileContent, filename, GetDownloadDirectory(), Settings))
{
var items = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == Path.GetFileNameWithoutExtension(filename));
_proxy.AddTorrentFromData(fileContent, filename, GetDownloadDirectory(), Settings);
var item = items.SingleOrDefault();
var items = _proxy.GetTorrents(Settings).Where(t => t.Additional.Detail["uri"] == Path.GetFileNameWithoutExtension(filename));
if (item != null)
{
_logger.Debug("{0} added correctly", remoteEpisode);
return CreateDownloadId(item.Id, hashedSerialNumber);
}
var item = items.SingleOrDefault();
_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");
}

@ -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)
{
return response.Data.SerialNumber;
}
_logger.Debug("Failed to get Download Station serial number");
throw new DownloadClientException("Failed to get Download Station serial number");
var response = ProcessRequest<DSMInfoResponse>(DiskStationApi.DSMInfo, arguments, settings, "get serial number");
return response.Data.SerialNumber;
}
}
}

@ -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,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" },
};
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"];
var infoResponeDSTask = infoResponse.Data["SYNO.DownloadStation.Task"];
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.DownloadStationTask] = infoResponeDSTask.Path;
Resources[DiskStationApi.FileStationList] = infoResponseFSList.Path;
Resources[DiskStationApi.DSMInfo] = infoResponseDSMInfo.Path;
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
//TODO: Refactor this into more elegant code
var infoResponeDSAuth = infoResponse.Data["SYNO.API.Auth"];
var infoResponeDSInfo = infoResponse.Data["SYNO.DownloadStation.Info"];
var infoResponeDSTask = infoResponse.Data["SYNO.DownloadStation.Task"];
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.DownloadStationTask] = infoResponeDSTask.Path;
Resources[DiskStationApi.FileStationList] = infoResponseFSList.Path;
Resources[DiskStationApi.DSMInfo] = infoResponseDSMInfo.Path;
switch (api)
{
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);
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>
{
@ -40,13 +40,11 @@ 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,14 +72,17 @@ 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);
}
return new List<DownloadStationTorrent>();
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,23 +110,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ "force_complete", false }
};
try
{
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;
}
var response = ProcessRequest(DiskStationApi.DownloadStationTask, arguments, settings, $"remove item {downloadId}");
_logger.Trace("Item {0} removed from Download Station", downloadId);
}
public IEnumerable<int> GetApiVersion(DownloadStationSettings settings)

@ -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);
return response.Data.Files.First();
}
}
}

Loading…
Cancel
Save