From 1b9480275fbebf2e295a4f2e5404383748550556 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 22 Oct 2012 23:57:01 -0700 Subject: [PATCH] Get json response when adding item to the queue --- .../SabProviderTests/SabProviderFixture.cs | 14 +++++++------- NzbDrone.Core/Model/Sabnzbd/SabAddResponse.cs | 16 ++++++++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Providers/DownloadClients/SabProvider.cs | 12 +++++------- 4 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 NzbDrone.Core/Model/Sabnzbd/SabAddResponse.cs diff --git a/NzbDrone.Core.Test/ProviderTests/DownloadClientTests/SabProviderTests/SabProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/DownloadClientTests/SabProviderTests/SabProviderFixture.cs index dd3feeb22..50d6e939d 100644 --- a/NzbDrone.Core.Test/ProviderTests/DownloadClientTests/SabProviderTests/SabProviderFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/DownloadClientTests/SabProviderTests/SabProviderFixture.cs @@ -43,15 +43,15 @@ namespace NzbDrone.Core.Test.ProviderTests.DownloadClientTests.SabProviderTests private void WithFailResponse() { Mocker.GetMock() - .Setup(s => s.DownloadString(It.IsAny())).Returns("failed"); + .Setup(s => s.DownloadString(It.IsAny())).Returns("{ \"status\": false, \"error\": \"API Key Required\" }"); } [Test] public void add_url_should_format_request_properly() { Mocker.GetMock(MockBehavior.Strict) - .Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) - .Returns("ok"); + .Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) + .Returns("{ \"status\": true }"); //Act Mocker.Resolve().DownloadNzb(url, title).Should().BeTrue(); @@ -61,8 +61,8 @@ namespace NzbDrone.Core.Test.ProviderTests.DownloadClientTests.SabProviderTests public void newzbin_add_url_should_format_request_properly() { Mocker.GetMock(MockBehavior.Strict) - .Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=addid&name=6107863&priority=0&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) - .Returns("ok"); + .Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=addid&name=6107863&priority=0&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) + .Returns("{ \"status\": true }"); //Act @@ -78,8 +78,8 @@ namespace NzbDrone.Core.Test.ProviderTests.DownloadClientTests.SabProviderTests WithFailResponse(); //Act - Mocker.Resolve().DownloadNzb(url, title).Should().BeFalse(); - ExceptionVerification.ExpectedWarns(1); + Assert.Throws(() => Mocker.Resolve().DownloadNzb(url, title).Should().BeFalse()); + //ExceptionVerification.ExpectedErrors(1); } [Test] diff --git a/NzbDrone.Core/Model/Sabnzbd/SabAddResponse.cs b/NzbDrone.Core/Model/Sabnzbd/SabAddResponse.cs new file mode 100644 index 000000000..43e0a89a1 --- /dev/null +++ b/NzbDrone.Core/Model/Sabnzbd/SabAddResponse.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Newtonsoft.Json; + +namespace NzbDrone.Core.Model.Sabnzbd +{ + public class SabAddResponse + { + public bool Status { get; set; } + + [JsonProperty(PropertyName = "nzo_ids")] + public List Ids { get; set; } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 275cbbb70..8d3bead02 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -277,6 +277,7 @@ + diff --git a/NzbDrone.Core/Providers/DownloadClients/SabProvider.cs b/NzbDrone.Core/Providers/DownloadClients/SabProvider.cs index 91684cb12..252b7b2b3 100644 --- a/NzbDrone.Core/Providers/DownloadClients/SabProvider.cs +++ b/NzbDrone.Core/Providers/DownloadClients/SabProvider.cs @@ -87,7 +87,7 @@ namespace NzbDrone.Core.Providers.DownloadClients string name = GetNzbName(url); string nzbName = HttpUtility.UrlEncode(title); - string action = string.Format("mode=addurl&name={0}&priority={1}&pp=3&cat={2}&nzbname={3}", + string action = string.Format("mode=addurl&name={0}&priority={1}&pp=3&cat={2}&nzbname={3}&output=json", name, priority, cat, nzbName); if (url.ToLower().Contains("newzbin")) @@ -98,19 +98,17 @@ namespace NzbDrone.Core.Providers.DownloadClients string request = GetSabRequest(action); logger.Info("Adding report [{0}] to the queue.", title); - var response = _httpProvider.DownloadString(request).Replace("\n", String.Empty); + var response = _httpProvider.DownloadString(request); logger.Debug("Queue Response: [{0}]", response); - if (response == "ok") - return true; - - logger.Warn("SAB returned unexpected response '{0}'", response); + CheckForError(response); + return true; } catch (WebException ex) { - logger.Error("Error communicating with SAB"); + logger.Error("Error communicating with SAB: " + ex.Message); } return false;