Skip to content

Commit 43a07de

Browse files
committed
Added proxy config
1 parent 80fb19e commit 43a07de

8 files changed

Lines changed: 89 additions & 228 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SpotifyAPI.Web
2+
{
3+
public class ProxyConfigTest
4+
{
5+
6+
}
7+
}

‎SpotifyAPI.Web.Tests/ProxyConfigTest.cs‎

Lines changed: 0 additions & 116 deletions
This file was deleted.

‎SpotifyAPI.Web/Clients/SpotifyClientConfig.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public class SpotifyClientConfig
2323
/// <param name="httpClient"></param>
2424
/// <param name="retryHandler"></param>
2525
/// <param name="httpLogger"></param>
26-
/// <param name="paginator"></param>
26+
/// <param name="defaultPaginator"></param>
2727
public SpotifyClientConfig(
2828
Uri baseAddress,
2929
IAuthenticator authenticator,
3030
IJSONSerializer jsonSerializer,
3131
IHTTPClient httpClient,
3232
IRetryHandler retryHandler,
3333
IHTTPLogger httpLogger,
34-
IPaginator paginator
34+
IPaginator defaultPaginator
3535
)
3636
{
3737
BaseAddress = baseAddress;
@@ -40,7 +40,7 @@ IPaginator paginator
4040
HTTPClient = httpClient;
4141
RetryHandler = retryHandler;
4242
HTTPLogger = httpLogger;
43-
DefaultPaginator = paginator;
43+
DefaultPaginator = defaultPaginator;
4444
}
4545

4646
public SpotifyClientConfig WithToken(string token, string tokenType = "Bearer")
@@ -130,9 +130,9 @@ public SpotifyClientConfig WithJSONSerializer(IJSONSerializer jsonSerializer)
130130
}
131131

132132

133-
public SpotifyClientConfig WithDefaultPaginator(IPaginator paginator)
133+
public SpotifyClientConfig WithDefaultPaginator(IPaginator defaultPaginator)
134134
{
135-
Ensure.ArgumentNotNull(paginator, nameof(paginator));
135+
Ensure.ArgumentNotNull(defaultPaginator, nameof(defaultPaginator));
136136

137137
return new SpotifyClientConfig(
138138
BaseAddress,
@@ -141,7 +141,7 @@ public SpotifyClientConfig WithDefaultPaginator(IPaginator paginator)
141141
HTTPClient,
142142
RetryHandler,
143143
HTTPLogger,
144-
paginator
144+
defaultPaginator
145145
);
146146
}
147147

‎SpotifyAPI.Web/Http/Interfaces/IHttpClient.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace SpotifyAPI.Web.Http
66
public interface IHTTPClient : IDisposable
77
{
88
Task<IResponse> DoRequest(IRequest request);
9-
109
void SetRequestTimeout(TimeSpan timeout);
1110
}
1211
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace SpotifyAPI.Web
2+
{
3+
public interface IProxyConfig
4+
{
5+
string Host { get; }
6+
int Port { get; }
7+
string User { get; }
8+
string Password { get; }
9+
bool SkipSSLCheck { get; }
10+
/// <summary>
11+
/// Whether to bypass the proxy server for local addresses.
12+
/// </summary>
13+
bool BypassProxyOnLocal { get; }
14+
}
15+
}

‎SpotifyAPI.Web/Http/NetHttpClient.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net;
12
using System.Text;
23
using System;
34
using System.IO;
@@ -9,13 +10,22 @@ namespace SpotifyAPI.Web.Http
910
{
1011
public class NetHttpClient : IHTTPClient
1112
{
13+
private readonly HttpMessageHandler _httpMessageHandler;
1214
private readonly HttpClient _httpClient;
1315

1416
public NetHttpClient()
1517
{
1618
_httpClient = new HttpClient();
1719
}
1820

21+
public NetHttpClient(IProxyConfig proxyConfig)
22+
{
23+
Ensure.ArgumentNotNull(proxyConfig, nameof(proxyConfig));
24+
25+
_httpMessageHandler = CreateMessageHandler(proxyConfig);
26+
_httpClient = new HttpClient(_httpMessageHandler);
27+
}
28+
1929
public async Task<IResponse> DoRequest(IRequest request)
2030
{
2131
Ensure.ArgumentNotNull(request, nameof(request));
@@ -84,12 +94,43 @@ protected virtual void Dispose(bool disposing)
8494
if (disposing)
8595
{
8696
_httpClient?.Dispose();
97+
_httpMessageHandler?.Dispose();
8798
}
8899
}
89100

90101
public void SetRequestTimeout(TimeSpan timeout)
91102
{
92103
_httpClient.Timeout = timeout;
93104
}
105+
106+
private static HttpMessageHandler CreateMessageHandler(IProxyConfig proxyConfig)
107+
{
108+
var proxy = new WebProxy
109+
{
110+
Address = new UriBuilder(proxyConfig.Host) { Port = proxyConfig.Port }.Uri,
111+
UseDefaultCredentials = true,
112+
BypassProxyOnLocal = proxyConfig.BypassProxyOnLocal
113+
};
114+
115+
if (!string.IsNullOrEmpty(proxyConfig.User) || !string.IsNullOrEmpty(proxyConfig.Password))
116+
{
117+
proxy.UseDefaultCredentials = false;
118+
proxy.Credentials = new NetworkCredential(proxyConfig.User, proxyConfig.Password);
119+
}
120+
121+
var httpClientHandler = new HttpClientHandler
122+
{
123+
PreAuthenticate = proxy.UseDefaultCredentials,
124+
UseDefaultCredentials = proxy.UseDefaultCredentials,
125+
UseProxy = true,
126+
Proxy = proxy,
127+
};
128+
if (proxyConfig.SkipSSLCheck)
129+
{
130+
httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
131+
}
132+
133+
return httpClientHandler;
134+
}
94135
}
95136
}

‎SpotifyAPI.Web/Http/ProxyConfig.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace SpotifyAPI.Web
2+
{
3+
public class ProxyConfig : IProxyConfig
4+
{
5+
public ProxyConfig(string host, int port)
6+
{
7+
Ensure.ArgumentNotNullOrEmptyString(host, nameof(host));
8+
9+
Host = host;
10+
Port = port;
11+
}
12+
13+
public string Host { get; }
14+
public int Port { get; }
15+
public string User { get; set; }
16+
public string Password { get; set; }
17+
public bool BypassProxyOnLocal { get; set; }
18+
public bool SkipSSLCheck { get; set; }
19+
}
20+
}

‎SpotifyAPI.Web/ProxyConfig.cs‎

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)