Browse Source

Merge

pull/202/head^2
leandro 9 years ago
parent
commit
9927c833c5
  1. 20
      TMDbLib/Client/TMDbClient.cs
  2. 5
      TMDbLib/Rest/RestClient.cs
  3. 9
      TMDbLib/Rest/RestRequest.cs
  4. 55
      TMDbLib/Utilities/TMDbAPIProxy.cs
  5. 7497
      TMDbLibTests/project.lock.json
  6. 6788
      TestApplication/project.lock.json

20
TMDbLib/Client/TMDbClient.cs

@ -7,6 +7,7 @@ using TMDbLib.Utilities.Converters;
using ParameterType = TMDbLib.Rest.ParameterType;
using RestClient = TMDbLib.Rest.RestClient;
using RestRequest = TMDbLib.Rest.RestRequest;
using System.Net;
namespace TMDbLib.Client
{
@ -19,7 +20,7 @@ namespace TMDbLib.Client
private RestClient _client;
private TMDbConfig _config;
public TMDbClient(string apiKey, bool useSsl = false, string baseUrl = ProductionUrl, JsonSerializer serializer = null)
public TMDbClient(string apiKey, bool useSsl = false, string baseUrl = ProductionUrl, JsonSerializer serializer = null, IWebProxy proxy = null)
{
DefaultLanguage = null;
DefaultCountry = null;
@ -31,6 +32,10 @@ namespace TMDbLib.Client
_serializer.Converters.Add(new SearchBaseConverter());
_serializer.Converters.Add(new TaggedImageConverter());
//Setup proxy to use during requests
//Proxy is optional. If passed, will be used in every request.
WebProxy = proxy;
Initialize(baseUrl, useSsl, apiKey);
}
@ -113,6 +118,19 @@ namespace TMDbLib.Client
set { MaxRetryCount = value ? 0 : 5; }
}
/// <summary>
/// Gets or sets the Web Proxy to use during requests to TMDb API.
/// </summary>
/// <remarks>
/// The Web Proxy is optional. If set, every request will be sent through it.
/// Use the constructor for setting it.
///
/// For convenience, this library also offers a <see cref="IWebProxy"/> implementation.
/// Check <see cref="TMDbLib.Utilities.TMDbAPIProxy"/> for more information.
/// </remarks>
public IWebProxy WebProxy { get; private set; }
/// <summary>
/// Used internally to assign a session id to a request. If no valid session is found, an exception is thrown.
/// </summary>

5
TMDbLib/Rest/RestClient.cs

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Net;
namespace TMDbLib.Rest
{
@ -9,18 +10,20 @@ namespace TMDbLib.Rest
{
private int _maxRetryCount;
public RestClient(Uri baseUrl, JsonSerializer serializer)
public RestClient(Uri baseUrl, JsonSerializer serializer, IWebProxy proxy = null)
{
BaseUrl = baseUrl;
Serializer = serializer;
DefaultQueryString = new List<KeyValuePair<string, string>>();
MaxRetryCount = 0;
Proxy = proxy;
}
internal Uri BaseUrl { get; }
internal List<KeyValuePair<string, string>> DefaultQueryString { get; }
internal Encoding Encoding { get; } = new UTF8Encoding(false);
internal IWebProxy Proxy { get; private set; }
public int MaxRetryCount
{

9
TMDbLib/Rest/RestRequest.cs

@ -204,7 +204,14 @@ namespace TMDbLib.Rest
do
{
HttpRequestMessage req = PrepRequest(method);
HttpResponseMessage resp = await new HttpClient().SendAsync(req).ConfigureAwait(false);
HttpClientHandler handler = new HttpClientHandler();
//Added to support proxy during requests to TMDb API
//Proxy is optional, so we only use it if set into the RestClient
if (_client.Proxy != null)
handler.Proxy = _client.Proxy;
HttpResponseMessage resp = await new HttpClient(handler).SendAsync(req).ConfigureAwait(false);
if (resp.StatusCode == (HttpStatusCode)429)
{

55
TMDbLib/Utilities/TMDbAPIProxy.cs

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace TMDbLib.Utilities
{
/// <summary>
/// Represents a Web Proxy to use for TMDb API Requests.
/// </summary>
/// <remarks>
/// This is a very simple implementation of a Web Proxy to be used when requesting data from TMDb API.
/// It does not support proxy bypassing or multi-proxy configuration based on the destination URL, for instance.
/// </remarks>
public class TMDbAPIProxy : IWebProxy
{
private Uri _proxyUri;
/// <summary>
/// Initializes a new instance for this Proxy
/// </summary>
public TMDbAPIProxy(Uri proxyUri, ICredentials credentials = null)
{
if (proxyUri == null) throw new ArgumentNullException("proxyUri");
_proxyUri = proxyUri;
Credentials = credentials;
}
/// <summary>
/// Gets or sets the credentials to use for authenticating in the proxy server.
/// </summary>
public ICredentials Credentials
{
get;
set;
}
/// <summary>
/// Gets the proxy server <see cref="Uri"/> to be used when accessing <paramref name="destination"/>.
/// </summary>
/// <param name="destination">The destination URL to be accessed.</param>
/// <returns></returns>
public Uri GetProxy(Uri destination)
{
return _proxyUri;
}
public bool IsBypassed(Uri host)
{
return false;
}
}
}

7497
TMDbLibTests/project.lock.json
File diff suppressed because it is too large
View File

6788
TestApplication/project.lock.json
File diff suppressed because it is too large
View File

Loading…
Cancel
Save