From 11c49c9260c38b2c61eeee25348e14b9cf66bfe2 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 23 Aug 2013 19:21:12 -0700 Subject: [PATCH 1/3] Using services to get updates now --- NzbDrone.Common/NzbDrone.Common.csproj | 1 + NzbDrone.Common/Serializer/Json.cs | 3 -- NzbDrone.Common/Services.cs | 18 ++++++++ .../Scene/SceneMappingProxyFixture.cs | 8 ---- .../UpdatePackageProviderFixture.cs | 8 +--- NzbDrone.Core/Configuration/ConfigService.cs | 5 --- NzbDrone.Core/Configuration/IConfigService.cs | 1 - .../DailySeries/DailySeriesDataProxy.cs | 8 ++-- .../Scene/SceneMappingProxy.cs | 8 +--- NzbDrone.Core/NzbDrone.Core.csproj | 1 + NzbDrone.Core/Update/UpdateCheckService.cs | 2 +- NzbDrone.Core/Update/UpdatePackage.cs | 11 ++++- .../Update/UpdatePackageAvailable.cs | 13 ++++++ NzbDrone.Core/Update/UpdatePackageProvider.cs | 43 ++++--------------- 14 files changed, 58 insertions(+), 72 deletions(-) create mode 100644 NzbDrone.Common/Services.cs create mode 100644 NzbDrone.Core/Update/UpdatePackageAvailable.cs diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 42d7d0903..c1b17236f 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -93,6 +93,7 @@ + diff --git a/NzbDrone.Common/Serializer/Json.cs b/NzbDrone.Common/Serializer/Json.cs index 59c58946a..866776ec9 100644 --- a/NzbDrone.Common/Serializer/Json.cs +++ b/NzbDrone.Common/Serializer/Json.cs @@ -40,7 +40,6 @@ namespace NzbDrone.Common.Serializer return JsonConvert.SerializeObject(obj); } - public static void Serialize(TModel model, TextWriter outputStream) { var jsonTextWriter = new JsonTextWriter(outputStream); @@ -52,7 +51,5 @@ namespace NzbDrone.Common.Serializer { Serialize(model, new StreamWriter(outputStream)); } - - } } \ No newline at end of file diff --git a/NzbDrone.Common/Services.cs b/NzbDrone.Common/Services.cs new file mode 100644 index 000000000..bfa537901 --- /dev/null +++ b/NzbDrone.Common/Services.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Common +{ + public class Services + { + public static String RootUrl + { + get + { + return "http://services.nzbdrone.com"; + } + } + } +} diff --git a/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingProxyFixture.cs b/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingProxyFixture.cs index 64c5cbc79..98ad8b504 100644 --- a/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingProxyFixture.cs +++ b/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingProxyFixture.cs @@ -15,14 +15,6 @@ namespace NzbDrone.Core.Test.DataAugmentationFixture.Scene { private const string SCENE_MAPPING_URL = "http://services.nzbdrone.com/SceneMapping/Active"; - [SetUp] - public void Setup() - { - Mocker.GetMock().SetupGet(s => s.ServiceRootUrl) - .Returns("http://services.nzbdrone.com"); - - } - [Test] public void fetch_should_return_list_of_mappings() { diff --git a/NzbDrone.Core.Test/UpdateTests/UpdatePackageProviderFixture.cs b/NzbDrone.Core.Test/UpdateTests/UpdatePackageProviderFixture.cs index bbf0c5b9c..138e3b404 100644 --- a/NzbDrone.Core.Test/UpdateTests/UpdatePackageProviderFixture.cs +++ b/NzbDrone.Core.Test/UpdateTests/UpdatePackageProviderFixture.cs @@ -16,13 +16,7 @@ namespace NzbDrone.Core.Test.UpdateTests Mocker.GetMock().SetupGet(c => c.Branch).Returns("master"); - var updates = Subject.GetAvailablePackages().ToList(); - - updates.Should().NotBeEmpty(); - updates.Should().OnlyContain(c => !string.IsNullOrWhiteSpace(c.FileName)); - updates.Should().OnlyContain(c => !string.IsNullOrWhiteSpace(c.Url)); - updates.Should().OnlyContain(c => c.Version != null); - updates.Should().OnlyContain(c => c.Version.Major == 2); + Subject.GetLatestUpdate().Should().BeNull(); } } } diff --git a/NzbDrone.Core/Configuration/ConfigService.cs b/NzbDrone.Core/Configuration/ConfigService.cs index 0d9189fc6..e3ca0317b 100644 --- a/NzbDrone.Core/Configuration/ConfigService.cs +++ b/NzbDrone.Core/Configuration/ConfigService.cs @@ -172,11 +172,6 @@ namespace NzbDrone.Core.Configuration set { SetValue("BlackholeFolder", value); } } - public string ServiceRootUrl - { - get { return "http://services.nzbdrone.com"; } - } - public string PneumaticFolder { get { return GetValue("PneumaticFolder", String.Empty); } diff --git a/NzbDrone.Core/Configuration/IConfigService.cs b/NzbDrone.Core/Configuration/IConfigService.cs index 07e70d25b..d84ed272b 100644 --- a/NzbDrone.Core/Configuration/IConfigService.cs +++ b/NzbDrone.Core/Configuration/IConfigService.cs @@ -25,7 +25,6 @@ namespace NzbDrone.Core.Configuration int Retention { get; set; } DownloadClientType DownloadClient { get; set; } string BlackholeFolder { get; set; } - string ServiceRootUrl { get; } string PneumaticFolder { get; set; } string RecycleBin { get; set; } String NzbgetUsername { get; set; } diff --git a/NzbDrone.Core/DataAugmentation/DailySeries/DailySeriesDataProxy.cs b/NzbDrone.Core/DataAugmentation/DailySeries/DailySeriesDataProxy.cs index fc2a591a2..239afecc8 100644 --- a/NzbDrone.Core/DataAugmentation/DailySeries/DailySeriesDataProxy.cs +++ b/NzbDrone.Core/DataAugmentation/DailySeries/DailySeriesDataProxy.cs @@ -17,13 +17,11 @@ namespace NzbDrone.Core.DataAugmentation.DailySeries public class DailySeriesDataProxy : IDailySeriesDataProxy { private readonly IHttpProvider _httpProvider; - private readonly IConfigService _configService; private readonly Logger _logger; - public DailySeriesDataProxy(IHttpProvider httpProvider, IConfigService configService, Logger logger) + public DailySeriesDataProxy(IHttpProvider httpProvider, Logger logger) { _httpProvider = httpProvider; - _configService = configService; _logger = logger; } @@ -31,7 +29,7 @@ namespace NzbDrone.Core.DataAugmentation.DailySeries { try { - var dailySeriesIds = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/DailySeries/AllIds"); + var dailySeriesIds = _httpProvider.DownloadString(Services.RootUrl + "/DailySeries/AllIds"); var seriesIds = Json.Deserialize>(dailySeriesIds); @@ -49,7 +47,7 @@ namespace NzbDrone.Core.DataAugmentation.DailySeries { try { - var result = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/DailySeries/Check?seriesId=" + tvdbid); + var result = _httpProvider.DownloadString(Services.RootUrl + "/DailySeries/Check?seriesId=" + tvdbid); return Convert.ToBoolean(result); } catch (Exception ex) diff --git a/NzbDrone.Core/DataAugmentation/Scene/SceneMappingProxy.cs b/NzbDrone.Core/DataAugmentation/Scene/SceneMappingProxy.cs index c38cb4a58..2447a96ec 100644 --- a/NzbDrone.Core/DataAugmentation/Scene/SceneMappingProxy.cs +++ b/NzbDrone.Core/DataAugmentation/Scene/SceneMappingProxy.cs @@ -13,19 +13,15 @@ namespace NzbDrone.Core.DataAugmentation.Scene public class SceneMappingProxy : ISceneMappingProxy { private readonly IHttpProvider _httpProvider; - private readonly IConfigService _configService; - - public SceneMappingProxy(IHttpProvider httpProvider, IConfigService configService) + public SceneMappingProxy(IHttpProvider httpProvider) { _httpProvider = httpProvider; - _configService = configService; - } public List Fetch() { - var mappingsJson = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/SceneMapping/Active"); + var mappingsJson = _httpProvider.DownloadString(Services.RootUrl + "/SceneMapping/Active"); return Json.Deserialize>(mappingsJson); } } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 7adf989f7..9234720af 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -492,6 +492,7 @@ + diff --git a/NzbDrone.Core/Update/UpdateCheckService.cs b/NzbDrone.Core/Update/UpdateCheckService.cs index 36d43ea44..313f1ef58 100644 --- a/NzbDrone.Core/Update/UpdateCheckService.cs +++ b/NzbDrone.Core/Update/UpdateCheckService.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Core.Update { var latestAvailable = _updatePackageProvider.GetLatestUpdate(); - if (latestAvailable == null || latestAvailable.Version <= BuildInfo.Version) + if (latestAvailable == null) { _logger.Debug("No update available."); return null; diff --git a/NzbDrone.Core/Update/UpdatePackage.cs b/NzbDrone.Core/Update/UpdatePackage.cs index e600e2a83..ce038ac99 100644 --- a/NzbDrone.Core/Update/UpdatePackage.cs +++ b/NzbDrone.Core/Update/UpdatePackage.cs @@ -1,11 +1,18 @@ using System; +using Newtonsoft.Json; namespace NzbDrone.Core.Update { public class UpdatePackage { - public string Url { get; set; } - public string FileName { get; set; } + public String Id { get; set; } + + [JsonConverter(typeof(Newtonsoft.Json.Converters.VersionConverter))] public Version Version { get; set; } + + public String Branch { get; set; } + public DateTime ReleaseDate { get; set; } + public String FileName { get; set; } + public String Url { get; set; } } } diff --git a/NzbDrone.Core/Update/UpdatePackageAvailable.cs b/NzbDrone.Core/Update/UpdatePackageAvailable.cs new file mode 100644 index 000000000..1d76e22d7 --- /dev/null +++ b/NzbDrone.Core/Update/UpdatePackageAvailable.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Core.Update +{ + public class UpdatePackageAvailable + { + public Boolean Available { get; set; } + public UpdatePackage UpdatePackage { get; set; } + } +} diff --git a/NzbDrone.Core/Update/UpdatePackageProvider.cs b/NzbDrone.Core/Update/UpdatePackageProvider.cs index f85dc6e66..2456b5511 100644 --- a/NzbDrone.Core/Update/UpdatePackageProvider.cs +++ b/NzbDrone.Core/Update/UpdatePackageProvider.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using Newtonsoft.Json; using NLog; using NzbDrone.Common; using NzbDrone.Common.EnvironmentInfo; @@ -11,56 +12,30 @@ namespace NzbDrone.Core.Update { public interface IUpdatePackageProvider { - IEnumerable GetAvailablePackages(); UpdatePackage GetLatestUpdate(); } public class UpdatePackageProvider : IUpdatePackageProvider { - private readonly IConfigFileProvider _configService; + private readonly IConfigFileProvider _configFileProvider; private readonly IHttpProvider _httpProvider; private readonly Logger _logger; - private static readonly Regex ParseRegex = new Regex(@"(?:\>)(?NzbDrone.+?(?(?<=\.)\d+\.\d+\.\d+\.\d+).+?)(?:\<\/a\>)", - RegexOptions.IgnoreCase); - - public UpdatePackageProvider(IConfigFileProvider configService, IHttpProvider httpProvider, Logger logger) + public UpdatePackageProvider(IConfigFileProvider configFileProvider, IHttpProvider httpProvider, Logger logger) { - _configService = configService; + _configFileProvider = configFileProvider; _httpProvider = httpProvider; _logger = logger; } - public IEnumerable GetAvailablePackages() + public UpdatePackage GetLatestUpdate() { - var updateList = new List(); - - var branch = _configService.Branch; - var version = BuildInfo.Version; - var updateUrl = String.Format("http://update.nzbdrone.com/v{0}/{1}/", version.Major, branch); - - _logger.Debug("Getting a list of updates from {0}", updateUrl); - - var rawUpdateList = _httpProvider.DownloadString(updateUrl); - var matches = ParseRegex.Matches(rawUpdateList); + var url = String.Format("{0}/v1/update/{1}?{2}", Services.RootUrl, _configFileProvider.Branch, BuildInfo.Version); + var update = JsonConvert.DeserializeObject(_httpProvider.DownloadString(url)); - foreach (Match match in matches) - { - var updatePackage = new UpdatePackage(); - updatePackage.FileName = match.Groups["filename"].Value; - updatePackage.Url = updateUrl + updatePackage.FileName; - updatePackage.Version = new Version(match.Groups["version"].Value); - updateList.Add(updatePackage); - } + if (!update.Available) return null; - _logger.Debug("Found {0} update packages", updateUrl.Length); - - return updateList; - } - - public UpdatePackage GetLatestUpdate() - { - return GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault(); + return update.UpdatePackage; } } } \ No newline at end of file From c6bbc52631cf807682fa74362310005fb8582630 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 23 Aug 2013 19:42:45 -0700 Subject: [PATCH 2/3] Fixed UpdateResource --- NzbDrone.Api/Update/UpdateModule.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/NzbDrone.Api/Update/UpdateModule.cs b/NzbDrone.Api/Update/UpdateModule.cs index 6fdb1ca50..8a95efa4b 100644 --- a/NzbDrone.Api/Update/UpdateModule.cs +++ b/NzbDrone.Api/Update/UpdateModule.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Newtonsoft.Json; using NzbDrone.Api.REST; using NzbDrone.Core.Update; using NzbDrone.Api.Mapping; @@ -32,7 +33,13 @@ namespace NzbDrone.Api.Update public class UpdateResource : RestResource { + public String Id { get; set; } + + [JsonConverter(typeof(Newtonsoft.Json.Converters.VersionConverter))] public Version Version { get; set; } + + public String Branch { get; set; } + public DateTime ReleaseDate { get; set; } public String FileName { get; set; } public String Url { get; set; } } From eefe44caa50b9ffb83b4f0e67a3e479316023204 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 23 Aug 2013 19:45:59 -0700 Subject: [PATCH 3/3] added version to query string --- NzbDrone.Core/Update/UpdatePackageProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NzbDrone.Core/Update/UpdatePackageProvider.cs b/NzbDrone.Core/Update/UpdatePackageProvider.cs index 2456b5511..e045a89fd 100644 --- a/NzbDrone.Core/Update/UpdatePackageProvider.cs +++ b/NzbDrone.Core/Update/UpdatePackageProvider.cs @@ -30,7 +30,7 @@ namespace NzbDrone.Core.Update public UpdatePackage GetLatestUpdate() { - var url = String.Format("{0}/v1/update/{1}?{2}", Services.RootUrl, _configFileProvider.Branch, BuildInfo.Version); + var url = String.Format("{0}/v1/update/{1}?version={2}", Services.RootUrl, _configFileProvider.Branch, BuildInfo.Version); var update = JsonConvert.DeserializeObject(_httpProvider.DownloadString(url)); if (!update.Available) return null;