Lidarr/src/NzbDrone.Core/Update/UpdatePackageProvider.cs

82 lines
3.6 KiB
C#
Raw Normal View History

2013-04-13 23:57:10 +00:00
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
2014-09-11 23:49:41 +00:00
using NzbDrone.Common.Cloud;
2014-03-08 03:00:18 +00:00
using NzbDrone.Common.EnvironmentInfo;
2014-09-11 23:49:41 +00:00
using NzbDrone.Common.Http;
using NzbDrone.Core.Analytics;
2013-04-13 23:57:10 +00:00
namespace NzbDrone.Core.Update
{
public interface IUpdatePackageProvider
2013-04-13 23:57:10 +00:00
{
2013-09-11 06:33:47 +00:00
UpdatePackage GetLatestUpdate(string branch, Version currentVersion);
List<UpdatePackage> GetRecentUpdates(string branch, Version currentVersion);
2013-04-13 23:57:10 +00:00
}
public class UpdatePackageProvider : IUpdatePackageProvider
2013-04-13 23:57:10 +00:00
{
2014-09-11 23:49:41 +00:00
private readonly IHttpClient _httpClient;
private readonly IHttpRequestBuilderFactory _requestBuilder;
private readonly IPlatformInfo _platformInfo;
private readonly IAnalyticsService _analyticsService;
2013-04-13 23:57:10 +00:00
public UpdatePackageProvider(IHttpClient httpClient, ILidarrCloudRequestBuilder requestBuilder, IAnalyticsService analyticsService, IPlatformInfo platformInfo)
2014-09-11 23:49:41 +00:00
{
_platformInfo = platformInfo;
_analyticsService = analyticsService;
_requestBuilder = requestBuilder.Services;
_httpClient = httpClient;
2014-09-11 23:49:41 +00:00
}
2013-09-11 06:33:47 +00:00
2014-09-11 23:49:41 +00:00
public UpdatePackage GetLatestUpdate(string branch, Version currentVersion)
{
var request = _requestBuilder.Create()
.Resource("/update/{branch}")
.AddQueryParam("version", currentVersion)
.AddQueryParam("os", OsInfo.Os.ToString().ToLowerInvariant())
.AddQueryParam("arch", RuntimeInformation.OSArchitecture)
.AddQueryParam("runtime", PlatformInfo.Platform.ToString().ToLowerInvariant())
.AddQueryParam("runtimeVer", _platformInfo.Version)
.SetSegment("branch", branch);
if (_analyticsService.IsEnabled)
{
// Send if the system is active so we know which versions to deprecate/ignore
request.AddQueryParam("active", _analyticsService.InstallIsActive.ToString().ToLower());
}
2013-09-11 06:33:47 +00:00
var update = _httpClient.Get<UpdatePackageAvailable>(request.Build()).Resource;
2013-04-13 23:57:10 +00:00
if (!update.Available)
{
return null;
}
2013-04-13 23:57:10 +00:00
2013-08-24 02:21:12 +00:00
return update.UpdatePackage;
}
public List<UpdatePackage> GetRecentUpdates(string branch, Version currentVersion)
{
var request = _requestBuilder.Create()
.Resource("/update/{branch}/changes")
.AddQueryParam("version", currentVersion)
.AddQueryParam("os", OsInfo.Os.ToString().ToLowerInvariant())
.AddQueryParam("arch", RuntimeInformation.OSArchitecture)
.AddQueryParam("runtime", PlatformInfo.Platform.ToString().ToLowerInvariant())
.AddQueryParam("runtimeVer", _platformInfo.Version)
.SetSegment("branch", branch);
if (_analyticsService.IsEnabled)
{
// Send if the system is active so we know which versions to deprecate/ignore
request.AddQueryParam("active", _analyticsService.InstallIsActive.ToString().ToLower());
}
var updates = _httpClient.Get<List<UpdatePackage>>(request.Build());
2014-09-11 23:49:41 +00:00
return updates.Resource;
}
2013-04-13 23:57:10 +00:00
}
}