From 9a649cf58ef1611e861ac852070d4d88d0582661 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 20 Jul 2014 13:36:31 +0200 Subject: [PATCH] Added UserAgent to RestClient. --- src/NzbDrone.Common/NzbDrone.Common.csproj | 4 ++++ src/NzbDrone.Common/packages.config | 1 + .../DataAugmentation/Xem/XemProxy.cs | 6 ++--- .../Download/Clients/Nzbget/NzbgetProxy.cs | 2 +- .../Download/Clients/Sabnzbd/SabnzbdProxy.cs | 3 ++- .../MetadataSource/TraktProxy.cs | 2 +- .../MetadataSource/Tvdb/TvdbProxy.cs | 3 ++- .../NotifyMyAndroid/NotifyMyAndroidProxy.cs | 4 ++-- .../Notifications/Plex/PlexServerProxy.cs | 5 ++-- .../PushBullet/PushBulletProxy.cs | 2 +- .../Notifications/Pushover/PushoverService.cs | 2 +- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + src/NzbDrone.Core/Rest/RestClientFactory.cs | 24 +++++++++++++++++++ .../Update/UpdatePackageProvider.cs | 4 ++-- 14 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 src/NzbDrone.Core/Rest/RestClientFactory.cs diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index e9bdd86f0..c5ebd8d90 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -43,6 +43,10 @@ ..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll + + False + ..\packages\RestSharp.104.4.0\lib\net4\RestSharp.dll + diff --git a/src/NzbDrone.Common/packages.config b/src/NzbDrone.Common/packages.config index 4e193d0ba..67af54535 100644 --- a/src/NzbDrone.Common/packages.config +++ b/src/NzbDrone.Common/packages.config @@ -3,5 +3,6 @@ + \ No newline at end of file diff --git a/src/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs b/src/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs index f371d20b9..009fc8146 100644 --- a/src/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs +++ b/src/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs @@ -43,7 +43,7 @@ namespace NzbDrone.Core.DataAugmentation.Xem { _logger.Debug("Fetching Series IDs from"); - var restClient = new RestClient(XEM_BASE_URL); + var restClient = RestClientFactory.BuildClient(XEM_BASE_URL); var request = BuildRequest("havemap"); @@ -57,7 +57,7 @@ namespace NzbDrone.Core.DataAugmentation.Xem { _logger.Debug("Fetching Mappings for: {0}", id); - var restClient = new RestClient(XEM_BASE_URL); + var restClient = RestClientFactory.BuildClient(XEM_BASE_URL); var request = BuildRequest("all"); request.AddParameter("id", id); @@ -71,7 +71,7 @@ namespace NzbDrone.Core.DataAugmentation.Xem public List GetSceneTvdbNames() { _logger.Debug("Fetching alternate names"); - var restClient = new RestClient(XEM_BASE_URL); + var restClient = RestClientFactory.BuildClient(XEM_BASE_URL); var request = BuildRequest("allNames"); request.AddParameter("origin", "tvdb"); diff --git a/src/NzbDrone.Core/Download/Clients/Nzbget/NzbgetProxy.cs b/src/NzbDrone.Core/Download/Clients/Nzbget/NzbgetProxy.cs index 66c400192..343250598 100644 --- a/src/NzbDrone.Core/Download/Clients/Nzbget/NzbgetProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Nzbget/NzbgetProxy.cs @@ -172,7 +172,7 @@ namespace NzbDrone.Core.Download.Clients.Nzbget _logger.Debug("Url: " + url); - var client = new RestClient(url); + var client = RestClientFactory.BuildClient(url); client.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password); return client; diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs index 8f7a40e5d..fb54f969c 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs @@ -7,6 +7,7 @@ using NLog; using NzbDrone.Common; using NzbDrone.Common.Extensions; using NzbDrone.Common.Serializer; +using NzbDrone.Core.Rest; using NzbDrone.Core.Download.Clients.Sabnzbd.Responses; using RestSharp; @@ -138,7 +139,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd _logger.Debug("Url: " + url); - return new RestClient(url); + return RestClientFactory.BuildClient(url); } private void CheckForError(IRestResponse response) diff --git a/src/NzbDrone.Core/MetadataSource/TraktProxy.cs b/src/NzbDrone.Core/MetadataSource/TraktProxy.cs index be5b694fa..aafd62b85 100644 --- a/src/NzbDrone.Core/MetadataSource/TraktProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/TraktProxy.cs @@ -106,7 +106,7 @@ namespace NzbDrone.Core.MetadataSource private static IRestClient BuildClient(string resource, string method) { - return new RestClient(string.Format("http://api.trakt.tv/{0}/{1}.json/bc3c2c460f22cbb01c264022b540e191", resource, method)); + return RestClientFactory.BuildClient(string.Format("http://api.trakt.tv/{0}/{1}.json/bc3c2c460f22cbb01c264022b540e191", resource, method)); } private static Series MapSeries(Show show) diff --git a/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs b/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs index 6be24fc18..88af8bad4 100644 --- a/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/Tvdb/TvdbProxy.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Linq; +using NzbDrone.Core.Rest; using NzbDrone.Core.Indexers; using NzbDrone.Core.Tv; using RestSharp; @@ -38,7 +39,7 @@ namespace NzbDrone.Core.MetadataSource.Tvdb private static IRestClient BuildClient(string resource) { - return new RestClient(String.Format("http://thetvdb.com/data/{0}", resource)); + return RestClientFactory.BuildClient(String.Format("http://thetvdb.com/data/{0}", resource)); } private static Series MapSeries(XElement item) diff --git a/src/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidProxy.cs b/src/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidProxy.cs index f51c03062..3aca9b420 100644 --- a/src/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidProxy.cs +++ b/src/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidProxy.cs @@ -28,7 +28,7 @@ namespace NzbDrone.Core.Notifications.NotifyMyAndroid public void SendNotification(string title, string message, string apiKey, NotifyMyAndroidPriority priority) { - var client = new RestClient(URL); + var client = RestClientFactory.BuildClient(URL); var request = new RestRequest("notify", Method.POST); request.RequestFormat = DataFormat.Xml; request.AddParameter("apikey", apiKey); @@ -43,7 +43,7 @@ namespace NzbDrone.Core.Notifications.NotifyMyAndroid private void Verify(string apiKey) { - var client = new RestClient(URL); + var client = RestClientFactory.BuildClient(URL); var request = new RestRequest("verify", Method.GET); request.RequestFormat = DataFormat.Xml; request.AddParameter("apikey", apiKey, ParameterType.GetOrPost); diff --git a/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs b/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs index d0355835f..9ed970171 100644 --- a/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs +++ b/src/NzbDrone.Core/Notifications/Plex/PlexServerProxy.cs @@ -7,6 +7,7 @@ using NLog; using NzbDrone.Common; using NzbDrone.Common.Cache; using NzbDrone.Common.Serializer; +using NzbDrone.Core.Rest; using RestSharp; namespace NzbDrone.Core.Notifications.Plex @@ -75,7 +76,7 @@ namespace NzbDrone.Core.Notifications.Plex private RestClient GetMyPlexClient(string username, string password) { - var client = new RestClient("https://my.plexapp.com"); + var client = RestClientFactory.BuildClient("https://my.plexapp.com"); client.Authenticator = new HttpBasicAuthenticator(username, password); return client; @@ -96,7 +97,7 @@ namespace NzbDrone.Core.Notifications.Plex private RestClient GetPlexServerClient(PlexServerSettings settings) { - return new RestClient(String.Format("http://{0}:{1}", settings.Host, settings.Port)); + return RestClientFactory.BuildClient(String.Format("http://{0}:{1}", settings.Host, settings.Port)); } private RestRequest GetPlexServerRequest(string resource, Method method, PlexServerSettings settings) diff --git a/src/NzbDrone.Core/Notifications/PushBullet/PushBulletProxy.cs b/src/NzbDrone.Core/Notifications/PushBullet/PushBulletProxy.cs index 5f4ddf5ab..6071177fd 100644 --- a/src/NzbDrone.Core/Notifications/PushBullet/PushBulletProxy.cs +++ b/src/NzbDrone.Core/Notifications/PushBullet/PushBulletProxy.cs @@ -25,7 +25,7 @@ namespace NzbDrone.Core.Notifications.PushBullet public void SendNotification(string title, string message, string apiKey, string deviceId) { - var client = new RestClient(URL); + var client = RestClientFactory.BuildClient(URL); var request = BuildRequest(deviceId); request.AddParameter("type", "note"); diff --git a/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs b/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs index 484fb1146..97739702b 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs @@ -25,7 +25,7 @@ namespace NzbDrone.Core.Notifications.Pushover public void SendNotification(string title, string message, string apiKey, string userKey, PushoverPriority priority, string sound) { - var client = new RestClient(URL); + var client = RestClientFactory.BuildClient(URL); var request = new RestRequest(Method.POST); request.AddParameter("token", apiKey); request.AddParameter("user", userKey); diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index ab803b702..7e12f2d21 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -559,6 +559,7 @@ + diff --git a/src/NzbDrone.Core/Rest/RestClientFactory.cs b/src/NzbDrone.Core/Rest/RestClientFactory.cs new file mode 100644 index 000000000..042bfb5f2 --- /dev/null +++ b/src/NzbDrone.Core/Rest/RestClientFactory.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using RestSharp; +using NzbDrone.Common.EnvironmentInfo; + +namespace NzbDrone.Core.Rest +{ + public static class RestClientFactory + { + public static RestClient BuildClient(String baseUrl) + { + var restClient = new RestClient(baseUrl); + + restClient.UserAgent = String.Format("NzbDrone/{0} (RestSharp/{1}; {2}/{3})", + BuildInfo.Version, + restClient.GetType().Assembly.GetName().Version, + OsInfo.Os, OsInfo.Version.ToString(2)); + + return restClient; + } + } +} diff --git a/src/NzbDrone.Core/Update/UpdatePackageProvider.cs b/src/NzbDrone.Core/Update/UpdatePackageProvider.cs index 0b436adfb..5096a9788 100644 --- a/src/NzbDrone.Core/Update/UpdatePackageProvider.cs +++ b/src/NzbDrone.Core/Update/UpdatePackageProvider.cs @@ -17,7 +17,7 @@ namespace NzbDrone.Core.Update { public UpdatePackage GetLatestUpdate(string branch, Version currentVersion) { - var restClient = new RestClient(Services.RootUrl); + var restClient = RestClientFactory.BuildClient(Services.RootUrl); var request = new RestRequest("/v1/update/{branch}"); @@ -34,7 +34,7 @@ namespace NzbDrone.Core.Update public List GetRecentUpdates(string branch) { - var restClient = new RestClient(Services.RootUrl); + var restClient = RestClientFactory.BuildClient(Services.RootUrl); var request = new RestRequest("/v1/update/{branch}/changes");