Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add HttpMessageHandler #53

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions ChargeBee/Api/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using System.Text;
using ChargeBee.Internal;
using Newtonsoft.Json.Linq;
Expand All @@ -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
{
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion ChargeBee/Api/ApiUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down