Skip to content

Commit

Permalink
#423 Users will have to create new ProxyConfig object to configure a …
Browse files Browse the repository at this point in the history
…Proxy
  • Loading branch information
linvi committed May 9, 2017
1 parent 52ad3fe commit 6560718
Show file tree
Hide file tree
Showing 16 changed files with 2,001 additions and 763 deletions.
2,565 changes: 1,910 additions & 655 deletions Examplinvi.UniversalApp/project.lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Examplinvi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;

// REST API
Expand Down Expand Up @@ -34,7 +35,7 @@ class Program
{
static void Main()
{
Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
Auth.SetUserCredentials("54aUlLwbmRN7SX3wswMJmqpQG", "Fid46uknw6665bA4NxCiOHPoIUv6KMsaxhul7P4MwHMecIytYw", "1693649419-Ubxt4bKWWGQiRY9Ko4BcvX03EJUm2BPcRbW6pPM", "wLa7UFyp4FEDR2MHPtr6SEy4E3iCBqqlNAXuampl2SXZ7");

TweetinviEvents.QueryBeforeExecute += (sender, args) =>
{
Expand Down Expand Up @@ -1677,13 +1678,12 @@ public static void ConfigureTweetinvi()
return;
}

TweetinviConfig.CurrentThreadSettings.ProxyURL = "http://228.23.13.21:4287";
TweetinviConfig.CurrentThreadSettings.ProxyConfig = new ProxyConfig("http://228.23.13.21:4287");

// Configure a proxy with Proxy with username and password
TweetinviConfig.CurrentThreadSettings.ProxyURL = "http://user:pass@228.23.13.21:4287";
TweetinviConfig.CurrentThreadSettings.ProxyConfig = new ProxyConfig("http://228.23.13.21:4287", new NetworkCredential("username", "password"));

TweetinviConfig.CurrentThreadSettings.HttpRequestTimeout = 5000;

TweetinviConfig.CurrentThreadSettings.UploadTimeout = 90000;
}

Expand Down Expand Up @@ -1721,4 +1721,4 @@ public static void GetTwitterPrivacyPolicy()

#endregion
}
}
}
8 changes: 4 additions & 4 deletions Tweetinvi.Core/Core/TweetinviSettingsAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ITweetinviSettingsAccessor
/// <summary>
/// Proxy URL used by the current thread.
/// </summary>
string ProxyURL { get; set; }
IProxyConfig ProxyConfig { get; set; }

/// <summary>
/// Http requests timeout in the current thread.
Expand Down Expand Up @@ -101,10 +101,10 @@ private bool HasTheApplicationSettingsBeenInitialized()
return StaticTweetinviSettings != null;
}

public string ProxyURL
public IProxyConfig ProxyConfig
{
get { return CurrentThreadSettings.ProxyURL; }
set { CurrentThreadSettings.ProxyURL = value; }
get { return CurrentThreadSettings.ProxyConfig; }
set { CurrentThreadSettings.ProxyConfig = value; }
}

public int HttpRequestTimeout
Expand Down
16 changes: 0 additions & 16 deletions Tweetinvi.Core/Core/Web/IWebProxyFactory.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Tweetinvi.Core/Public/Models/Interfaces/ITwitterQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface ITwitterQuery : ITwitterRequestParameters
/// <summary>
/// Proxy used to perform the query
/// </summary>
string Proxy { get; set; }
IProxyConfig ProxyConfig { get; set; }

/// <summary>
/// Duration after which the query is considered as having failed.
Expand Down
70 changes: 70 additions & 0 deletions Tweetinvi.Core/Public/Settings/ProxyConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Net;

namespace Tweetinvi
{
public interface IProxyConfig : IWebProxy
{
Uri Address { get; }
}

public class ProxyConfig : IProxyConfig
{
private Uri _proxyAddress;
private NetworkCredential _networkCredentials;

public ProxyConfig(Uri proxyAddress, NetworkCredential credentials = null)
{
_proxyAddress = proxyAddress;
Credentials = credentials;
}

public ProxyConfig(string url, NetworkCredential credentials = null) : this(new Uri(url), credentials)
{
}

public ProxyConfig(IProxyConfig proxyConfig)
{
if (proxyConfig != null)
{
_proxyAddress = proxyConfig.Address;

var networkCredentials = proxyConfig.Credentials as NetworkCredential;
if (networkCredentials != null)
{
Credentials = new NetworkCredential(networkCredentials.UserName, networkCredentials.Password, networkCredentials.Domain);
}
}
}

public ICredentials Credentials
{
get { return _networkCredentials; }
set
{
var isCompatibleWithTweetinvi = value == null || value is NetworkCredential;
if (!isCompatibleWithTweetinvi)
{
throw new Exception("Tweetinvi proxy credentials can only use System.Net.NetworkCredential");
}

_networkCredentials = (NetworkCredential)value;
}
}

public Uri Address
{
get { return _proxyAddress; }
}

public Uri GetProxy(Uri destination)
{
return _proxyAddress;
}

public bool IsBypassed(Uri host)
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public enum TweetMode
public interface ITweetinviSettings
{
/// <summary>
/// Proxy URL used to execute Http Requests.
/// Proxy used to execute Http Requests.
/// </summary>
string ProxyURL { get; set; }
IProxyConfig ProxyConfig { get; set; }

/// <summary>
/// Http Requests Timeout duration in milliseconds.
Expand Down Expand Up @@ -83,7 +83,7 @@ public class TweetinviSettings : ITweetinviSettings
{
public const long DEFAULT_ID = -1;

public string ProxyURL { get; set; }
public IProxyConfig ProxyConfig { get; set; }
public int HttpRequestTimeout { get; set; }
public RateLimitTrackerMode RateLimitTrackerMode { get; set; }
public TweetMode? TweetMode { get; set; }
Expand All @@ -99,7 +99,7 @@ public ITweetinviSettings Clone()
{
var clone = new TweetinviSettings();

clone.ProxyURL = ProxyURL;
clone.ProxyConfig = ProxyConfig == null || ProxyConfig.Address == null ? null : new ProxyConfig(ProxyConfig);
clone.HttpRequestTimeout = HttpRequestTimeout;
clone.UploadTimeout = UploadTimeout;
clone.RateLimitTrackerMode = RateLimitTrackerMode;
Expand All @@ -111,7 +111,7 @@ public ITweetinviSettings Clone()

public void InitialiseFrom(ITweetinviSettings other)
{
ProxyURL = other.ProxyURL;
ProxyConfig = other.ProxyConfig;
HttpRequestTimeout = other.HttpRequestTimeout;
UploadTimeout = other.UploadTimeout;
RateLimitTrackerMode = other.RateLimitTrackerMode;
Expand Down
4 changes: 2 additions & 2 deletions Tweetinvi.Core/Tweetinvi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="Public\Models\Enum\ProcessingState.cs" />
<Compile Include="Public\Models\Enum\SearchResultType.cs" />
<Compile Include="Public\Models\Interfaces\ITwitterRequestParameters.cs" />
<Compile Include="Public\Settings\ProxyConfig.cs" />
<Compile Include="Public\Streaming\StreamState.cs" />
<Compile Include="Public\Models\Enum\TimeZoneFromTwitter.cs" />
<Compile Include="Core\Attributes\WindowsTimeZone.cs" />
Expand Down Expand Up @@ -254,7 +255,7 @@
<Compile Include="Core\RateLimit\IRateLimitCacheManager.cs" />
<Compile Include="Core\RateLimit\IRateLimitHelper.cs" />
<Compile Include="Core\RateLimit\IRateLimitUpdater.cs" />
<Compile Include="Public\TweetinviSettings.cs" />
<Compile Include="Public\Settings\TweetinviSettings.cs" />
<Compile Include="Public\Models\Interfaces\ITwitterList.cs" />
<Compile Include="Public\Models\Interfaces\ITweetWithSearchMetadata.cs" />
<Compile Include="Public\Models\Entities\IDescriptionEntity.cs" />
Expand Down Expand Up @@ -324,7 +325,6 @@
<Compile Include="Core\Web\ChunkUploadInitParameters.cs" />
<Compile Include="Core\Web\HttpRequestParameters.cs" />
<Compile Include="Core\Web\ITwitterClientHandler.cs" />
<Compile Include="Core\Web\IWebProxyFactory.cs" />
<Compile Include="Core\Web\MultipartHttpRequestParameters.cs" />
<Compile Include="Core\Wrappers\IJConvertWrapper.cs" />
<Compile Include="Core\Wrappers\IJObjectStaticWrapper.cs" />
Expand Down
1 change: 1 addition & 0 deletions Tweetinvi.Core/Tweetinvi.Core.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=public_005Cmodels_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=public_005Cparameters_005Cenum/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=public_005Cparameters_005Cmodels/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=public_005Csettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=public_005Ctools/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion Tweetinvi.Factories/TwitterQueryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ITwitterQuery Create(string queryURL, HttpMethod httpMethod, bool withThr

var twitterQuery = _twitterQueryFactory.Create(queryURLParameter, httpMethodParameter);

twitterQuery.Proxy = _tweetinviSettingsAccessor.ProxyURL;
twitterQuery.ProxyConfig = _tweetinviSettingsAccessor.ProxyConfig;
twitterQuery.Timeout = TimeSpan.FromMilliseconds(_tweetinviSettingsAccessor.HttpRequestTimeout);

if (withThreadCredentials)
Expand Down
2 changes: 0 additions & 2 deletions Tweetinvi.WebLogic/Tweetinvi.WebLogic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
<Compile Include="TwitterRequestHandler.cs" />
<Compile Include="TwitterRequestParameters.cs" />
<Compile Include="Utils\HttpMethodExtensions.cs" />
<Compile Include="Utils\WebProxy.cs" />
<Compile Include="WebProxyFactory.cs" />
<Compile Include="WebRequestExecutor.cs" />
<Compile Include="OAuthWebRequestGenerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
1 change: 0 additions & 1 deletion Tweetinvi.WebLogic/TweetinviWebLogicModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void Initialize()
_container.RegisterType<IWebHelper, WebHelper>(RegistrationLifetime.InstancePerApplication);
_container.RegisterType<IHttpClientWebHelper, HttpClientWebHelper>();
_container.RegisterType<IWebRequestResult, WebRequestResult>();
_container.RegisterType<IWebProxyFactory, WebProxyFactory>(RegistrationLifetime.InstancePerApplication);

_container.RegisterType<ITwitterQuery, TwitterQuery>();
}
Expand Down
5 changes: 1 addition & 4 deletions Tweetinvi.WebLogic/TwitterClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class TwitterClientHandler : HttpClientHandler, ITwitterClientHandler
{
private readonly Action<ITwitterQuery, HttpRequestMessage> _action;
private readonly Func<ITwitterQuery, HttpRequestMessage, string> _func;
private readonly IWebProxyFactory _webProxyFactory;
protected readonly IOAuthWebRequestGenerator _webRequestGenerator;

private ITwitterQuery _twitterQuery;
Expand All @@ -24,9 +23,7 @@ public TwitterClientHandler()
UseCookies = false;
UseDefaultCredentials = false;


_webRequestGenerator = TweetinviCoreModule.TweetinviContainer.Resolve<IOAuthWebRequestGenerator>();
_webProxyFactory = TweetinviCoreModule.TweetinviContainer.Resolve<IWebProxyFactory>();
}

public TwitterClientHandler(Action<ITwitterQuery, HttpRequestMessage> action)
Expand All @@ -50,7 +47,7 @@ public ITwitterQuery TwitterQuery

if (value != null)
{
Proxy = _webProxyFactory.GetProxy(value.Proxy);
Proxy = value.ProxyConfig;

if (Proxy != null)
{
Expand Down
2 changes: 1 addition & 1 deletion Tweetinvi.WebLogic/TwitterQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TwitterQuery(string queryURL, HttpMethod httpMethod)
};
}

public string Proxy { get; set; }
public IProxyConfig ProxyConfig { get; set; }

private TimeSpan _timeout;
public TimeSpan Timeout
Expand Down
31 changes: 0 additions & 31 deletions Tweetinvi.WebLogic/Utils/WebProxy.cs

This file was deleted.

35 changes: 0 additions & 35 deletions Tweetinvi.WebLogic/WebProxyFactory.cs

This file was deleted.

0 comments on commit 6560718

Please sign in to comment.