From 478b4d5729a877dcf53f6738c574470894d34d6c Mon Sep 17 00:00:00 2001 From: carlos Date: Thu, 14 Apr 2022 17:00:58 +1000 Subject: [PATCH 1/5] Adding HttpMessageHandler for standard2 --- ChargeBee/Api/ApiConfig.cs | 38 +++++++++----------- ChargeBee/Api/ApiUtil.cs | 74 +++++++++++++++++++++++++++----------- ChargeBee/ChargeBee.csproj | 5 ++- 3 files changed, 73 insertions(+), 44 deletions(-) diff --git a/ChargeBee/Api/ApiConfig.cs b/ChargeBee/Api/ApiConfig.cs index acb7a3d..a8853b8 100644 --- a/ChargeBee/Api/ApiConfig.cs +++ b/ChargeBee/Api/ApiConfig.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using System.Text; namespace ChargeBee.Api @@ -16,30 +17,22 @@ public sealed class ApiConfig public string SiteName { get; set; } public string Charset { get; set; } public static int ConnectTimeout { get; set; } + + public static HttpMessageHandler HttpMessageHandler { get; set; } - public string ApiBaseUrl - { - get - { - return String.Format("{0}://{1}.{2}/api/{3}", - Proto, - SiteName, - DomainSuffix, - API_VERSION); - } - } + public string ApiBaseUrl => + String.Format("{0}://{1}.{2}/api/{3}", + Proto, + SiteName, + DomainSuffix, + API_VERSION); - public string AuthValue - { - get - { - return String.Format("Basic {0}", - Convert.ToBase64String(Encoding.UTF8.GetBytes( + public string AuthValue => + String.Format("Basic {0}", + Convert.ToBase64String(Encoding.UTF8.GetBytes( String.Format("{0}:", ApiKey)))); - } - } - public ApiConfig(string siteName, string apiKey) + public ApiConfig(string siteName, string apiKey, HttpMessageHandler httpMessageHandler = null) { if (String.IsNullOrEmpty(siteName)) @@ -54,13 +47,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 ApiConfig Instance diff --git a/ChargeBee/Api/ApiUtil.cs b/ChargeBee/Api/ApiUtil.cs index f6db58e..9c4ce22 100644 --- a/ChargeBee/Api/ApiUtil.cs +++ b/ChargeBee/Api/ApiUtil.cs @@ -6,9 +6,7 @@ using System.Text; using System.Collections.Generic; using System.Runtime.InteropServices; - using Newtonsoft.Json; - using ChargeBee.Exceptions; using System.Net.Http; using System.Threading.Tasks; @@ -18,7 +16,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) { @@ -28,8 +31,10 @@ public static string BuildUrl(params string[] paths) { sb.Append('/').Append(Uri.EscapeDataString(path)); } + return sb.ToString(); } + private static HttpRequestMessage BuildRequest(string uri, HttpMethod method, Params parameters, ApiConfig env) { HttpRequestMessage request; @@ -47,15 +52,19 @@ private static HttpRequestMessage BuildRequest(string uri, HttpMethod method, Pa { request = new HttpRequestMessage(meth, new Uri($"{env.ApiBaseUrl}{uri}")); } + return request; } - private static HttpRequestMessage GetRequestMessage(string url, HttpMethod method, Params parameters, Dictionary headers, ApiConfig env) + + private static HttpRequestMessage GetRequestMessage(string url, HttpMethod method, Params parameters, + Dictionary headers, ApiConfig env) { HttpRequestMessage request = BuildRequest(url, method, parameters, env); AddHeaders(request, env); AddCustomHeaders(request, headers); return request; } + private static void AddHeaders(HttpRequestMessage request, ApiConfig env) { request.Headers.Add("Accept-Charset", env.Charset); @@ -96,14 +105,26 @@ private static void HandleException(HttpResponseMessage response) } catch (JsonException e) { - if(content.Contains("503")){ - throw new ArgumentException("Sorry, the server is currently unable to handle the request due to a temporary overload or scheduled maintenance. Please retry after sometime. \n type: internal_temporary_error, \n http_status_code: 503, \n error_code: internal_temporary_error,\n content: " + content,e); - }else if(content.Contains("504")){ - throw new ArgumentException("The server did not receive a timely response from an upstream server, request aborted. If this problem persists, contact us at support@chargebee.com. \n type: gateway_timeout, \n http_status_code: 504, \n error_code: gateway_timeout,\n content: " + content, e); - }else{ - throw new ArgumentException("Sorry, something went wrong when trying to process the request. If this problem persists, contact us at support@chargebee.com. \n type: internal_error, \n http_status_code: 500, \n error_code: internal_error,\n content: " + content,e); + if (content.Contains("503")) + { + throw new ArgumentException( + "Sorry, the server is currently unable to handle the request due to a temporary overload or scheduled maintenance. Please retry after sometime. \n type: internal_temporary_error, \n http_status_code: 503, \n error_code: internal_temporary_error,\n content: " + + content, e); + } + else if (content.Contains("504")) + { + throw new ArgumentException( + "The server did not receive a timely response from an upstream server, request aborted. If this problem persists, contact us at support@chargebee.com. \n type: gateway_timeout, \n http_status_code: 504, \n error_code: gateway_timeout,\n content: " + + content, e); + } + else + { + throw new ArgumentException( + "Sorry, something went wrong when trying to process the request. If this problem persists, contact us at support@chargebee.com. \n type: internal_error, \n http_status_code: 500, \n error_code: internal_error,\n content: " + + content, e); } } + string type = ""; errorJson.TryGetValue("type", out type); if ("payment".Equals(type)) @@ -122,14 +143,17 @@ private static void HandleException(HttpResponseMessage response) { throw new ApiException(response.StatusCode, errorJson); } - } - private static EntityResult GetEntityResult(String url, Params parameters, Dictionary headers, ApiConfig env, HttpMethod meth) - { - return GetEntityResultAsync(url, parameters, headers, env, meth).ConfigureAwait(false).GetAwaiter().GetResult(); + private static EntityResult GetEntityResult(String url, Params parameters, Dictionary headers, + ApiConfig env, HttpMethod meth) + { + return GetEntityResultAsync(url, parameters, headers, env, meth).ConfigureAwait(false).GetAwaiter() + .GetResult(); } - private static async Task GetEntityResultAsync(String url, Params parameters, Dictionary headers, ApiConfig env, HttpMethod meth) + + private static async Task GetEntityResultAsync(String url, Params parameters, + Dictionary headers, ApiConfig env, HttpMethod meth) { HttpRequestMessage request = GetRequestMessage(url, meth, parameters, headers, env); var response = await httpClient.SendAsync(request).ConfigureAwait(false); @@ -145,12 +169,15 @@ private static async Task GetEntityResultAsync(String url, Params return null; } } - public static EntityResult Post(string url, Params parameters, Dictionary headers, ApiConfig env) + + public static EntityResult Post(string url, Params parameters, Dictionary headers, + ApiConfig env) { return GetEntityResult(url, parameters, headers, env, HttpMethod.POST); } - public static Task PostAsync(string url, Params parameters, Dictionary headers, ApiConfig env) + public static Task PostAsync(string url, Params parameters, Dictionary headers, + ApiConfig env) { return GetEntityResultAsync(url, parameters, headers, env, HttpMethod.POST); } @@ -161,18 +188,21 @@ public static EntityResult Get(string url, Params parameters, Dictionary GetAsync(string url, Params parameters, Dictionary headers, ApiConfig env) + public static Task GetAsync(string url, Params parameters, Dictionary headers, + ApiConfig env) { url = String.Format("{0}?{1}", url, parameters.GetQuery(false)); return GetEntityResultAsync(url, parameters, headers, env, HttpMethod.GET); } - public static ListResult GetList(string url, Params parameters, Dictionary headers, ApiConfig env) + public static ListResult GetList(string url, Params parameters, Dictionary headers, + ApiConfig env) { return GetListAsync(url, parameters, headers, env).GetAwaiter().GetResult(); } - public static async Task GetListAsync(string url, Params parameters, Dictionary headers, ApiConfig env) + public static async Task GetListAsync(string url, Params parameters, + Dictionary headers, ApiConfig env) { url = String.Format("{0}?{1}", url, parameters.GetQuery(true)); HttpRequestMessage request = GetRequestMessage(url, HttpMethod.GET, parameters, headers, env); @@ -209,7 +239,6 @@ public static DateTime ConvertFromTimestamp(long timestamp) public static String SerializeObject(Object obj) { return JsonConvert.SerializeObject(obj); - } } @@ -222,14 +251,17 @@ public enum HttpMethod /// DELETE /// DELETE, + /// /// GET /// GET, + /// /// POST /// POST, + /// /// PUT /// diff --git a/ChargeBee/ChargeBee.csproj b/ChargeBee/ChargeBee.csproj index e737488..8b8cf57 100644 --- a/ChargeBee/ChargeBee.csproj +++ b/ChargeBee/ChargeBee.csproj @@ -1,6 +1,6 @@ - netstandard1.2;netstandard2.0;net45 + netstandard2.0 2.15.0 chargebee true @@ -60,6 +60,9 @@ + + + From 06601017d293c5a87701328e84cbf4410f6f139e Mon Sep 17 00:00:00 2001 From: carlos Date: Tue, 26 Sep 2023 10:12:41 +1000 Subject: [PATCH 2/5] Merge --- ChargeBee/ChargeBee.csproj | 2 -- 1 file changed, 2 deletions(-) diff --git a/ChargeBee/ChargeBee.csproj b/ChargeBee/ChargeBee.csproj index d99bee5..1cb068e 100644 --- a/ChargeBee/ChargeBee.csproj +++ b/ChargeBee/ChargeBee.csproj @@ -62,8 +62,6 @@ - netstandard1.2;netstandard2.0;net45 - 3.9.0 From b31b864014b94769a09933f4a0b5fa43b375df6e Mon Sep 17 00:00:00 2001 From: carlos Date: Tue, 26 Sep 2023 10:28:55 +1000 Subject: [PATCH 3/5] Update Nuget packages --- ChargeBee/ChargeBee.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ChargeBee/ChargeBee.csproj b/ChargeBee/ChargeBee.csproj index 1cb068e..2b583e6 100644 --- a/ChargeBee/ChargeBee.csproj +++ b/ChargeBee/ChargeBee.csproj @@ -1,6 +1,5 @@ - netstandard1.2;netstandard2.0;net45 3.9.0 chargebee true @@ -16,6 +15,7 @@ 512 ..\ true + netstandard2.0 pdbonly @@ -45,7 +45,7 @@ - + @@ -61,7 +61,7 @@ - + From c3c0f43b39e2d73251e2823d7443893b03f94cd7 Mon Sep 17 00:00:00 2001 From: carlos Date: Thu, 16 May 2024 11:35:48 +1000 Subject: [PATCH 4/5] Fixing handler --- ChargeBee/Api/ApiConfig.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ChargeBee/Api/ApiConfig.cs b/ChargeBee/Api/ApiConfig.cs index 0a9114b..4dfbd82 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,6 +64,7 @@ public ApiConfig(string siteName, string apiKey) ExportSleepMillis = 10000; SiteName = siteName; ApiKey = apiKey; + HttpMessageHandler = httpMessageHandler; } private static volatile ApiConfig m_instance; From 495a6be382c9542925dd731554567319be42ec94 Mon Sep 17 00:00:00 2001 From: carlos Date: Thu, 16 May 2024 13:41:02 +1000 Subject: [PATCH 5/5] Add optional HttpMessageHandler parameter to ApiConfig Modified the ApiConfig's configure method to allow an optional HttpMessageHandler parameter. This additional handler allows greater configurability for different client scenarios and advanced usage. --- ChargeBee/Api/ApiConfig.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ChargeBee/Api/ApiConfig.cs b/ChargeBee/Api/ApiConfig.cs index 4dfbd82..bb420ba 100644 --- a/ChargeBee/Api/ApiConfig.cs +++ b/ChargeBee/Api/ApiConfig.cs @@ -69,9 +69,9 @@ public ApiConfig(string siteName, string apiKey, HttpMessageHandler httpMessageH 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)