diff --git a/.gitignore b/.gitignore index ef9ac27..b726e59 100644 --- a/.gitignore +++ b/.gitignore @@ -113,3 +113,4 @@ UpgradeLog*.XML Icon? G# Thumbnails ._* +/.vs diff --git a/ChargeBee/Api/ApiConfig.cs b/ChargeBee/Api/ApiConfig.cs index 52a3be6..0391240 100644 --- a/ChargeBee/Api/ApiConfig.cs +++ b/ChargeBee/Api/ApiConfig.cs @@ -1,33 +1,46 @@ -using System; -using System.Text; using ChargeBee.Internal; using Newtonsoft.Json.Linq; +using System; +using System.Net.Http; +using System.Text; namespace ChargeBee.Api { public sealed class ApiConfig { - public static string DomainSuffix = "chargebee.com"; - public static string Proto = "https"; - public static string Version = "2.18.0"; - public static readonly string API_VERSION = "v2"; + public static string DomainSuffix = "chargebee.com"; + public static string Proto = "https"; + public static string Version = "2.18.0"; + public static readonly string API_VERSION = "v2"; public static int TimeTravelMillis { get; set; } - public static int ExportSleepMillis { get; set;} + public static int ExportSleepMillis { get; set; } public string ApiKey { get; set; } public string SiteName { get; set; } public string Charset { get; set; } - public static int ConnectTimeout { get; set; } + public int ConnectTimeout + { + get + { + return HttpClient.Timeout.Milliseconds; + } + set + { + HttpClient.Timeout = TimeSpan.FromMilliseconds(0 < value ? value : 30000); + } + } + + internal HttpClient HttpClient { get; } public string ApiBaseUrl { get { - return String.Format("{0}://{1}.{2}/api/{3}", + return String.Format("{0}://{1}.{2}/api/{3}", Proto, SiteName, DomainSuffix, - API_VERSION); + API_VERSION); } } @@ -41,9 +54,8 @@ public string AuthValue } } - public ApiConfig(string siteName, string apiKey) + public ApiConfig(string siteName, string apiKey, HttpClient client = null) { - if (String.IsNullOrEmpty(siteName)) throw new ArgumentException("Site name can't be empty!"); @@ -51,28 +63,40 @@ public ApiConfig(string siteName, string apiKey) throw new ArgumentException("Api key can't be empty!"); Charset = Encoding.UTF8.WebName; - ConnectTimeout = 30000; TimeTravelMillis = 3000; ExportSleepMillis = 10000; SiteName = siteName; ApiKey = apiKey; + + if (client == null) + { + HttpClient = new HttpClient + { + Timeout = TimeSpan.FromMilliseconds(30000) + }; + } + else + { + client.Timeout = TimeSpan.FromMilliseconds(30000); + HttpClient = client; + } } private static volatile ApiConfig m_instance; - public static void Configure(string siteName, string apiKey) - { - m_instance = new ApiConfig(siteName, apiKey); + public static void Configure(string siteName, string apiKey, HttpClient client = null) + { + m_instance = new ApiConfig(siteName, apiKey, client); } - public static string SerializeObject(T t)where T : Resource + public static string SerializeObject(T t) where T : Resource { return t.GetJToken().ToString(); } - public static T DeserializeObject(string str)where T : Resource, new() + public static T DeserializeObject(string str) where T : Resource, new() { - JToken JObj = JToken.Parse(str); + JToken JObj = JToken.Parse(str); T t = new T(); t.JObj = JObj; return t; @@ -89,8 +113,9 @@ public static ApiConfig Instance } } - public static void updateConnectTimeoutInMillis(int timeout) { - ConnectTimeout = timeout; + public void updateConnectTimeoutInMillis(int timeout) + { + ConnectTimeout = timeout; } } } diff --git a/ChargeBee/Api/ApiUtil.cs b/ChargeBee/Api/ApiUtil.cs index f6db58e..dcb1d4e 100644 --- a/ChargeBee/Api/ApiUtil.cs +++ b/ChargeBee/Api/ApiUtil.cs @@ -1,24 +1,17 @@ using System; -using System.ComponentModel; -using System.IO; -using System.Net; -using System.Reflection; -using System.Text; using System.Collections.Generic; -using System.Runtime.InteropServices; - -using Newtonsoft.Json; - -using ChargeBee.Exceptions; using System.Net.Http; +using System.Runtime.InteropServices; +using System.Text; using System.Threading.Tasks; +using ChargeBee.Exceptions; +using Newtonsoft.Json; 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) }; public static string BuildUrl(params string[] paths) { @@ -62,7 +55,7 @@ private static void AddHeaders(HttpRequestMessage request, ApiConfig env) request.Headers.Add("Authorization", env.AuthValue); request.Headers.Add("Accept", "application/json"); request.Headers.UserAgent.ParseAdd("ChargeBee-DotNet-Client v" + ApiConfig.Version); -#if NET45 +#if NET462 request.Headers.Add("Lang-Version",Environment.Version.ToString()); request.Headers.Add("OS-Version",Environment.OSVersion.ToString()); @@ -96,12 +89,17 @@ 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 = ""; @@ -132,7 +130,7 @@ private static EntityResult GetEntityResult(String url, Params parameters, Dicti 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); + var response = await env.HttpClient.SendAsync(request).ConfigureAwait(false); var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (response.IsSuccessStatusCode) { @@ -176,7 +174,7 @@ public static async Task GetListAsync(string url, Params parameters, { url = String.Format("{0}?{1}", url, parameters.GetQuery(true)); HttpRequestMessage request = GetRequestMessage(url, HttpMethod.GET, parameters, headers, env); - var response = await httpClient.SendAsync(request).ConfigureAwait(false); + var response = await env.HttpClient.SendAsync(request).ConfigureAwait(false); var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (response.IsSuccessStatusCode) { diff --git a/ChargeBee/ChargeBee.csproj b/ChargeBee/ChargeBee.csproj index 15f9ecf..e2bc5b6 100644 --- a/ChargeBee/ChargeBee.csproj +++ b/ChargeBee/ChargeBee.csproj @@ -1,75 +1,77 @@ - - - netstandard1.2;netstandard2.0;net45 - 2.18.0 - chargebee - true - https://github.com/chargebee/chargebee-dotnet - https://github.com/chargebee/chargebee-dotnet/blob/master/LICENSE - https://github.com/chargebee/chargebee-dotnet - Debug - AnyCPU - Library - Properties - ChargeBee - ChargeBee - 512 - ..\ - true - - - pdbonly - True - bin\Release\ - TRACE - prompt - 4 - bin\Release\ChargeBee.xml - 1591 - false - - - False - bin\Debug - 4 - true - false - - - true - - - Cb-Dotnet.snk - - - - - - - - - + + + + netstandard1.2;netstandard2.0;net462 + 2.18.0 + chargebee + true + https://github.com/chargebee/chargebee-dotnet + https://github.com/chargebee/chargebee-dotnet/blob/master/LICENSE + https://github.com/chargebee/chargebee-dotnet + Debug + AnyCPU + Library + Properties + ChargeBee + ChargeBee + 512 + ..\ + true + + + + pdbonly + True + bin\Release\ + TRACE + prompt + 4 + bin\Release\ChargeBee.xml + 1591 + false + + + + False + bin\Debug + 4 + true + false + + + + true + + + + Cb-Dotnet.snk + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - -