Skip to content

Commit 7e8956a

Browse files
Merge pull request #11 from julianonunes/master
Adding the option to set WebProxy object to be used on HttpClient
2 parents ea499f4 + ace3db8 commit 7e8956a

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

CSharpHTTPClient/Client.cs

+39-1
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,27 @@ public class Client : DynamicObject
6868
public string Version;
6969
public string UrlPath;
7070
public string MediaType;
71+
public WebProxy WebProxy;
7172
public enum Methods
7273
{
7374
DELETE, GET, PATCH, POST, PUT
7475
}
7576

77+
78+
/// <summary>
79+
/// REST API client.
80+
/// </summary>
81+
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
82+
/// <param name="requestHeaders">A dictionary of request headers</param>
83+
/// <param name="version">API version, override AddVersion to customize</param>
84+
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
85+
/// <returns>Fluent interface to a REST API</returns>
86+
public Client(WebProxy webProxy, string host, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null)
87+
: this(host, requestHeaders, version, urlPath)
88+
{
89+
WebProxy = webProxy;
90+
}
91+
7692
/// <summary>
7793
/// REST API client.
7894
/// </summary>
@@ -159,6 +175,28 @@ private Client BuildClient(string name = null)
159175

160176
}
161177

178+
/// <summary>
179+
/// Factory method to return the right HttpClient settings.
180+
/// </summary>
181+
/// <returns>Instance of HttpClient</returns>
182+
private HttpClient BuildHttpClient()
183+
{
184+
// Add the WebProxy if set
185+
if (WebProxy != null)
186+
{
187+
var httpClientHandler = new HttpClientHandler()
188+
{
189+
Proxy = WebProxy,
190+
PreAuthenticate = true,
191+
UseDefaultCredentials = false,
192+
};
193+
194+
return new HttpClient(httpClientHandler);
195+
}
196+
197+
return new HttpClient();
198+
}
199+
162200
/// <summary>
163201
/// Add the authorization header, override to customize
164202
/// </summary>
@@ -274,7 +312,7 @@ public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMe
274312
/// <returns>Response object</returns>
275313
private async Task<Response> RequestAsync(string method, string requestBody = null, string queryParams = null)
276314
{
277-
using (var client = new HttpClient())
315+
using (var client = BuildHttpClient())
278316
{
279317
try
280318
{

0 commit comments

Comments
 (0)