From ace3db8eba0355a9ae5773020b7d2dc6ace419d0 Mon Sep 17 00:00:00 2001 From: Juliano Nunes Date: Thu, 14 Jul 2016 09:32:53 -0300 Subject: [PATCH] Adding the option to set WebProxy object to be used on HttpClient --- CSharpHTTPClient/Client.cs | 40 +++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/CSharpHTTPClient/Client.cs b/CSharpHTTPClient/Client.cs index 84a3f46..8f3583e 100644 --- a/CSharpHTTPClient/Client.cs +++ b/CSharpHTTPClient/Client.cs @@ -68,11 +68,27 @@ public class Client : DynamicObject public string Version; public string UrlPath; public string MediaType; + public WebProxy WebProxy; public enum Methods { DELETE, GET, PATCH, POST, PUT } + + /// + /// REST API client. + /// + /// Base url (e.g. https://api.sendgrid.com) + /// A dictionary of request headers + /// API version, override AddVersion to customize + /// Path to endpoint (e.g. /path/to/endpoint) + /// Fluent interface to a REST API + public Client(WebProxy webProxy, string host, Dictionary requestHeaders = null, string version = null, string urlPath = null) + : this(host, requestHeaders, version, urlPath) + { + WebProxy = webProxy; + } + /// /// REST API client. /// @@ -159,6 +175,28 @@ private Client BuildClient(string name = null) } + /// + /// Factory method to return the right HttpClient settings. + /// + /// Instance of HttpClient + private HttpClient BuildHttpClient() + { + // Add the WebProxy if set + if (WebProxy != null) + { + var httpClientHandler = new HttpClientHandler() + { + Proxy = WebProxy, + PreAuthenticate = true, + UseDefaultCredentials = false, + }; + + return new HttpClient(httpClientHandler); + } + + return new HttpClient(); + } + /// /// Add the authorization header, override to customize /// @@ -274,7 +312,7 @@ public async virtual Task MakeRequest(HttpClient client, HttpRequestMe /// Response object private async Task RequestAsync(string method, String requestBody = null, String queryParams = null) { - using (var client = new HttpClient()) + using (var client = BuildHttpClient()) { try {