diff --git a/ChargeBee/Api/ApiConfig.cs b/ChargeBee/Api/ApiConfig.cs index 1d6efd1..6744da9 100644 --- a/ChargeBee/Api/ApiConfig.cs +++ b/ChargeBee/Api/ApiConfig.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using System.Text; using ChargeBee.Internal; using Newtonsoft.Json.Linq; @@ -19,6 +20,8 @@ public sealed class ApiConfig public string Charset { get; set; } public static int ConnectTimeout { get; set; } public string BaseUrl { get; set; } + + public static HttpMessageHandler HttpMessageHandler { get; set; } public string ApiBaseUrl { @@ -46,7 +49,7 @@ public string AuthValue } } - public ApiConfig(string siteName, string apiKey) + public ApiConfig(string siteName, string apiKey, HttpMessageHandler httpMessageHandler = null) { if (String.IsNullOrEmpty(siteName)) @@ -61,13 +64,14 @@ public ApiConfig(string siteName, string apiKey) ExportSleepMillis = 10000; SiteName = siteName; ApiKey = apiKey; + HttpMessageHandler = httpMessageHandler; } private static volatile ApiConfig m_instance; - public static void Configure(string siteName, string apiKey) + public static void Configure(string siteName, string apiKey, HttpMessageHandler httpMessageHandler = null) { - m_instance = new ApiConfig(siteName, apiKey); + m_instance = new ApiConfig(siteName, apiKey, httpMessageHandler); } public static void SetBaseUrl(string url) diff --git a/ChargeBee/Api/ApiUtil.cs b/ChargeBee/Api/ApiUtil.cs index 39b9350..4e593ca 100644 --- a/ChargeBee/Api/ApiUtil.cs +++ b/ChargeBee/Api/ApiUtil.cs @@ -18,7 +18,12 @@ namespace ChargeBee.Api public static class ApiUtil { private static DateTime m_unixTime = new DateTime(1970, 1, 1); - private static HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromMilliseconds(0 < ApiConfig.ConnectTimeout ? ApiConfig.ConnectTimeout : 30000) }; + + private static readonly HttpClient httpClient = + new HttpClient(ApiConfig.HttpMessageHandler ?? new HttpClientHandler()) + { + Timeout = TimeSpan.FromMilliseconds(0 < ApiConfig.ConnectTimeout ? ApiConfig.ConnectTimeout : 30000) + }; public static string BuildUrl(params string[] paths) { @@ -31,6 +36,7 @@ public static string BuildUrl(params string[] paths) else sb.Append('/').Append(Uri.EscapeDataString(path)); } + return sb.ToString(); } private static HttpRequestMessage BuildRequest(string uri, HttpMethod method, Params parameters, ApiConfig env, bool supportsFilter) @@ -59,6 +65,7 @@ private static HttpRequestMessage GetRequestMessage(string url, HttpMethod metho AddCustomHeaders(request, headers); return request; } + private static void AddHeaders(HttpRequestMessage request, ApiConfig env) { request.Headers.Add("Accept-Charset", env.Charset);