Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion CSharpHTTPClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


/// <summary>
/// REST API client.
/// </summary>
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
/// <param name="requestHeaders">A dictionary of request headers</param>
/// <param name="version">API version, override AddVersion to customize</param>
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
/// <returns>Fluent interface to a REST API</returns>
public Client(WebProxy webProxy, string host, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null)
: this(host, requestHeaders, version, urlPath)
{
WebProxy = webProxy;
}

/// <summary>
/// REST API client.
/// </summary>
Expand Down Expand Up @@ -159,6 +175,28 @@ private Client BuildClient(string name = null)

}

/// <summary>
/// Factory method to return the right HttpClient settings.
/// </summary>
/// <returns>Instance of HttpClient</returns>
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();
}

/// <summary>
/// Add the authorization header, override to customize
/// </summary>
Expand Down Expand Up @@ -274,7 +312,7 @@ public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMe
/// <returns>Response object</returns>
private async Task<Response> RequestAsync(string method, String requestBody = null, String queryParams = null)
{
using (var client = new HttpClient())
using (var client = BuildHttpClient())
{
try
{
Expand Down