Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ UpgradeLog*.XML
Icon?
G# Thumbnails
._*
/.vs
67 changes: 46 additions & 21 deletions ChargeBee/Api/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand All @@ -41,38 +54,49 @@ 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!");

if (String.IsNullOrEmpty(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 t)where T : Resource
public static string SerializeObject<T>(T t) where T : Resource
{
return t.GetJToken().ToString();
}

public static T DeserializeObject<T>(string str)where T : Resource, new()
public static T DeserializeObject<T>(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;
Expand All @@ -89,8 +113,9 @@ public static ApiConfig Instance
}
}

public static void updateConnectTimeoutInMillis(int timeout) {
ConnectTimeout = timeout;
public void updateConnectTimeoutInMillis(int timeout)
{
ConnectTimeout = timeout;
}
}
}
38 changes: 18 additions & 20 deletions ChargeBee/Api/ApiUtil.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -132,7 +130,7 @@ private static EntityResult GetEntityResult(String url, Params parameters, Dicti
private static async Task<EntityResult> GetEntityResultAsync(String url, Params parameters, Dictionary<string, string> 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)
{
Expand Down Expand Up @@ -176,7 +174,7 @@ public static async Task<ListResult> 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)
{
Expand Down
148 changes: 75 additions & 73 deletions ChargeBee/ChargeBee.csproj
Original file line number Diff line number Diff line change
@@ -1,75 +1,77 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.2;netstandard2.0;net45</TargetFrameworks>
<Version>2.18.0</Version>
<PackageId>chargebee</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/chargebee/chargebee-dotnet</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/chargebee/chargebee-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/chargebee/chargebee-dotnet</RepositoryUrl>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ChargeBee</RootNamespace>
<AssemblyName>ChargeBee</AssemblyName>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\ChargeBee.xml</DocumentationFile>
<NoWarn>1591</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>Cb-Dotnet.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Net.Requests" Version="4.3.0" />
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.2;netstandard2.0;net462</TargetFrameworks>
<Version>2.18.0</Version>
<PackageId>chargebee</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/chargebee/chargebee-dotnet</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/chargebee/chargebee-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/chargebee/chargebee-dotnet</RepositoryUrl>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ChargeBee</RootNamespace>
<AssemblyName>ChargeBee</AssemblyName>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\ChargeBee.xml</DocumentationFile>
<NoWarn>1591</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>

<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<PropertyGroup>
<AssemblyOriginatorKeyFile>Cb-Dotnet.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Net.Requests" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<None Include="Cb-Dotnet.snk" />
</ItemGroup>

<ProjectExtensions>
<MonoDevelop>
<Properties>
<Policies>
<TextStylePolicy inheritsSet="null" scope="application/xml" />
<XmlFormattingPolicy scope="application/xml">
<DefaultFormat OmitXmlDeclaration="False" IndentContent="True" AttributesInNewLine="False" MaxAttributesPerLine="10" WrapAttributes="False" AlignAttributes="False" AlignAttributeValues="False" QuoteChar="&quot;" SpacesBeforeAssignment="0" SpacesAfterAssignment="0" EmptyLinesBeforeStart="0" EmptyLinesAfterStart="0" EmptyLinesBeforeEnd="0" EmptyLinesAfterEnd="0" />
</XmlFormattingPolicy>
</Policies>
</Properties>
</MonoDevelop>
</ProjectExtensions>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="Cb-Dotnet.snk" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="lib\" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>
<Properties>
<Policies>
<TextStylePolicy inheritsSet="null" scope="application/xml" />
<XmlFormattingPolicy scope="application/xml">
<DefaultFormat OmitXmlDeclaration="False" IndentContent="True" AttributesInNewLine="False" MaxAttributesPerLine="10" WrapAttributes="False" AlignAttributes="False" AlignAttributeValues="False" QuoteChar="&quot;" SpacesBeforeAssignment="0" SpacesAfterAssignment="0" EmptyLinesBeforeStart="0" EmptyLinesAfterStart="0" EmptyLinesBeforeEnd="0" EmptyLinesAfterEnd="0" />
</XmlFormattingPolicy>
</Policies>
</Properties>
</MonoDevelop>
</ProjectExtensions>
</Project>