diff --git a/CryptoExchange.Net/Clients/RestApiClient.cs b/CryptoExchange.Net/Clients/RestApiClient.cs index 882bbdc..3a4d9ea 100644 --- a/CryptoExchange.Net/Clients/RestApiClient.cs +++ b/CryptoExchange.Net/Clients/RestApiClient.cs @@ -71,7 +71,7 @@ namespace CryptoExchange.Net rateLimiters.Add(rateLimiter); RateLimiters = rateLimiters; - RequestFactory.Configure(options.RequestTimeout, httpClient); + RequestFactory.Configure(options.Proxy, options.RequestTimeout, httpClient); } /// diff --git a/CryptoExchange.Net/Interfaces/IRequestFactory.cs b/CryptoExchange.Net/Interfaces/IRequestFactory.cs index a4979fd..bcd1fa9 100644 --- a/CryptoExchange.Net/Interfaces/IRequestFactory.cs +++ b/CryptoExchange.Net/Interfaces/IRequestFactory.cs @@ -1,4 +1,5 @@ using CryptoExchange.Net.Objects; +using CryptoExchange.Net.Objects.Options; using System; using System.Net.Http; @@ -23,6 +24,7 @@ namespace CryptoExchange.Net.Interfaces /// /// Request timeout to use /// Optional shared http client instance - void Configure(TimeSpan requestTimeout, HttpClient? httpClient=null); + /// Optional proxy to use when no http client is provided + void Configure(ApiProxy? proxy, TimeSpan requestTimeout, HttpClient? httpClient=null); } } diff --git a/CryptoExchange.Net/Requests/RequestFactory.cs b/CryptoExchange.Net/Requests/RequestFactory.cs index 963eb1d..63d897c 100644 --- a/CryptoExchange.Net/Requests/RequestFactory.cs +++ b/CryptoExchange.Net/Requests/RequestFactory.cs @@ -1,8 +1,10 @@ using System; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; +using CryptoExchange.Net.Objects.Options; namespace CryptoExchange.Net.Requests { @@ -14,12 +16,27 @@ namespace CryptoExchange.Net.Requests private HttpClient? _httpClient; /// - public void Configure(TimeSpan requestTimeout, HttpClient? client = null) + public void Configure(ApiProxy? proxy, TimeSpan requestTimeout, HttpClient? client = null) { - _httpClient = client ?? new HttpClient() + if (client == null) { - Timeout = requestTimeout - }; + var handler = new HttpClientHandler(); + if (proxy != null) + { + handler.Proxy = new WebProxy + { + Address = new Uri($"{proxy.Host}:{proxy.Port}"), + Credentials = proxy.Password == null ? null : new NetworkCredential(proxy.Login, proxy.Password) + }; + } + + client = new HttpClient(handler) + { + Timeout = requestTimeout + }; + } + + _httpClient = client; } ///