Added UserAgent to RestClient.

This commit is contained in:
Taloth Saldono 2014-07-20 13:36:31 +02:00 committed by Mark McDowall
parent e9f39493f4
commit 9a649cf58e
14 changed files with 48 additions and 15 deletions

View File

@ -43,6 +43,10 @@
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath> <HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="RestSharp, Version=104.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\RestSharp.104.4.0\lib\net4\RestSharp.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" /> <Reference Include="System.Configuration.Install" />

View File

@ -3,5 +3,6 @@
<package id="loggly-csharp" version="2.3" targetFramework="net40" /> <package id="loggly-csharp" version="2.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" /> <package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" />
<package id="NLog" version="2.1.0" targetFramework="net40" /> <package id="NLog" version="2.1.0" targetFramework="net40" />
<package id="RestSharp" version="104.4.0" targetFramework="net40" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" /> <package id="SharpZipLib" version="0.86.0" targetFramework="net40" />
</packages> </packages>

View File

@ -43,7 +43,7 @@ namespace NzbDrone.Core.DataAugmentation.Xem
{ {
_logger.Debug("Fetching Series IDs from"); _logger.Debug("Fetching Series IDs from");
var restClient = new RestClient(XEM_BASE_URL); var restClient = RestClientFactory.BuildClient(XEM_BASE_URL);
var request = BuildRequest("havemap"); var request = BuildRequest("havemap");
@ -57,7 +57,7 @@ namespace NzbDrone.Core.DataAugmentation.Xem
{ {
_logger.Debug("Fetching Mappings for: {0}", id); _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"); var request = BuildRequest("all");
request.AddParameter("id", id); request.AddParameter("id", id);
@ -71,7 +71,7 @@ namespace NzbDrone.Core.DataAugmentation.Xem
public List<SceneMapping> GetSceneTvdbNames() public List<SceneMapping> GetSceneTvdbNames()
{ {
_logger.Debug("Fetching alternate names"); _logger.Debug("Fetching alternate names");
var restClient = new RestClient(XEM_BASE_URL); var restClient = RestClientFactory.BuildClient(XEM_BASE_URL);
var request = BuildRequest("allNames"); var request = BuildRequest("allNames");
request.AddParameter("origin", "tvdb"); request.AddParameter("origin", "tvdb");

View File

@ -172,7 +172,7 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
_logger.Debug("Url: " + url); _logger.Debug("Url: " + url);
var client = new RestClient(url); var client = RestClientFactory.BuildClient(url);
client.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password); client.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password);
return client; return client;

View File

@ -7,6 +7,7 @@ using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Rest;
using NzbDrone.Core.Download.Clients.Sabnzbd.Responses; using NzbDrone.Core.Download.Clients.Sabnzbd.Responses;
using RestSharp; using RestSharp;
@ -138,7 +139,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
_logger.Debug("Url: " + url); _logger.Debug("Url: " + url);
return new RestClient(url); return RestClientFactory.BuildClient(url);
} }
private void CheckForError(IRestResponse response) private void CheckForError(IRestResponse response)

View File

@ -106,7 +106,7 @@ namespace NzbDrone.Core.MetadataSource
private static IRestClient BuildClient(string resource, string method) 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) private static Series MapSeries(Show show)

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml.Linq; using System.Xml.Linq;
using NzbDrone.Core.Rest;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using RestSharp; using RestSharp;
@ -38,7 +39,7 @@ namespace NzbDrone.Core.MetadataSource.Tvdb
private static IRestClient BuildClient(string resource) 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) private static Series MapSeries(XElement item)

View File

@ -28,7 +28,7 @@ namespace NzbDrone.Core.Notifications.NotifyMyAndroid
public void SendNotification(string title, string message, string apiKey, NotifyMyAndroidPriority priority) 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); var request = new RestRequest("notify", Method.POST);
request.RequestFormat = DataFormat.Xml; request.RequestFormat = DataFormat.Xml;
request.AddParameter("apikey", apiKey); request.AddParameter("apikey", apiKey);
@ -43,7 +43,7 @@ namespace NzbDrone.Core.Notifications.NotifyMyAndroid
private void Verify(string apiKey) private void Verify(string apiKey)
{ {
var client = new RestClient(URL); var client = RestClientFactory.BuildClient(URL);
var request = new RestRequest("verify", Method.GET); var request = new RestRequest("verify", Method.GET);
request.RequestFormat = DataFormat.Xml; request.RequestFormat = DataFormat.Xml;
request.AddParameter("apikey", apiKey, ParameterType.GetOrPost); request.AddParameter("apikey", apiKey, ParameterType.GetOrPost);

View File

@ -7,6 +7,7 @@ using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Rest;
using RestSharp; using RestSharp;
namespace NzbDrone.Core.Notifications.Plex namespace NzbDrone.Core.Notifications.Plex
@ -75,7 +76,7 @@ namespace NzbDrone.Core.Notifications.Plex
private RestClient GetMyPlexClient(string username, string password) 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); client.Authenticator = new HttpBasicAuthenticator(username, password);
return client; return client;
@ -96,7 +97,7 @@ namespace NzbDrone.Core.Notifications.Plex
private RestClient GetPlexServerClient(PlexServerSettings settings) 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) private RestRequest GetPlexServerRequest(string resource, Method method, PlexServerSettings settings)

View File

@ -25,7 +25,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
public void SendNotification(string title, string message, string apiKey, string deviceId) 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); var request = BuildRequest(deviceId);
request.AddParameter("type", "note"); request.AddParameter("type", "note");

View File

@ -25,7 +25,7 @@ namespace NzbDrone.Core.Notifications.Pushover
public void SendNotification(string title, string message, string apiKey, string userKey, PushoverPriority priority, string sound) 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); var request = new RestRequest(Method.POST);
request.AddParameter("token", apiKey); request.AddParameter("token", apiKey);
request.AddParameter("user", userKey); request.AddParameter("user", userKey);

View File

@ -559,6 +559,7 @@
<Compile Include="Qualities\QualityModelComparer.cs" /> <Compile Include="Qualities\QualityModelComparer.cs" />
<Compile Include="Qualities\QualityProfileItem.cs" /> <Compile Include="Qualities\QualityProfileItem.cs" />
<Compile Include="Rest\JsonNetSerializer.cs" /> <Compile Include="Rest\JsonNetSerializer.cs" />
<Compile Include="Rest\RestClientFactory.cs" />
<Compile Include="RootFolders\RootFolderRepository.cs" /> <Compile Include="RootFolders\RootFolderRepository.cs" />
<Compile Include="Security.cs" /> <Compile Include="Security.cs" />
<Compile Include="ThingiProvider\ConfigContractNotFoundException.cs" /> <Compile Include="ThingiProvider\ConfigContractNotFoundException.cs" />

View File

@ -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;
}
}
}

View File

@ -17,7 +17,7 @@ namespace NzbDrone.Core.Update
{ {
public UpdatePackage GetLatestUpdate(string branch, Version currentVersion) 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}"); var request = new RestRequest("/v1/update/{branch}");
@ -34,7 +34,7 @@ namespace NzbDrone.Core.Update
public List<UpdatePackage> GetRecentUpdates(string branch) public List<UpdatePackage> GetRecentUpdates(string branch)
{ {
var restClient = new RestClient(Services.RootUrl); var restClient = RestClientFactory.BuildClient(Services.RootUrl);
var request = new RestRequest("/v1/update/{branch}/changes"); var request = new RestRequest("/v1/update/{branch}/changes");