Skip to content

Commit

Permalink
.NET Core wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed Aug 29, 2016
1 parent 3af7beb commit e2cedde
Show file tree
Hide file tree
Showing 12 changed files with 11,628 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Chargify.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5D223E15-C3F1-4516-AC83-41AF719DE77B}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -35,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{7F8C95
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChargifyNetSample.MVC", "Source\ChargifyNetSample.MVC\ChargifyNetSample.MVC.csproj", "{C618CDC0-A9D1-4FF0-93A8-5A46848BFE14}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ChargifyNET.DotCore", "Source\ChargifyNET.DotCore\ChargifyNET.DotCore.xproj", "{C0885DAE-B697-4771-AF67-93D0D8569056}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ChargifyNET.DotCoreTest", "Source\ChargifyNET.DotCoreTest\ChargifyNET.DotCoreTest.xproj", "{690B712E-B3D1-40AA-BA8F-386552186911}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -98,6 +102,30 @@ Global
{C618CDC0-A9D1-4FF0-93A8-5A46848BFE14}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{C618CDC0-A9D1-4FF0-93A8-5A46848BFE14}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{C618CDC0-A9D1-4FF0-93A8-5A46848BFE14}.Release|x86.ActiveCfg = Release|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Debug|x86.ActiveCfg = Debug|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Debug|x86.Build.0 = Debug|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Release|Any CPU.Build.0 = Release|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Release|x86.ActiveCfg = Release|Any CPU
{C0885DAE-B697-4771-AF67-93D0D8569056}.Release|x86.Build.0 = Release|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Debug|Any CPU.Build.0 = Debug|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Debug|x86.ActiveCfg = Debug|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Debug|x86.Build.0 = Debug|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Release|Any CPU.ActiveCfg = Release|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Release|Any CPU.Build.0 = Release|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Release|x86.ActiveCfg = Release|Any CPU
{690B712E-B3D1-40AA-BA8F-386552186911}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -109,6 +137,8 @@ Global
{F4C6848A-523E-4895-831B-BEB20D9F9965} = {B1023B37-8D93-4B15-80DB-4C99742E26EE}
{CD8E53D2-B177-494B-AE08-1CEEF98E43D7} = {D741C8EB-46FE-477B-97D2-5D2463EA6BDC}
{C618CDC0-A9D1-4FF0-93A8-5A46848BFE14} = {D741C8EB-46FE-477B-97D2-5D2463EA6BDC}
{C0885DAE-B697-4771-AF67-93D0D8569056} = {1109C7B5-C897-4B8C-8942-AF7F8B7FFE83}
{690B712E-B3D1-40AA-BA8F-386552186911} = {B1023B37-8D93-4B15-80DB-4C99742E26EE}
EndGlobalSection
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = Chargify.vsmdi
Expand Down
227 changes: 227 additions & 0 deletions Source/ChargifyNET.DotCore/ChargifyConnect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.DotNet.InternalAbstractions;
using System.Reflection;
using System.Net.Http;
using System.Text;

namespace ChargifyNET.DotCore
{
public class ChargifyConnect
{
#region Constructors

/// <summary>
/// Constructor
/// </summary>
public ChargifyConnect() { }

/// <summary>
/// Constructor
/// </summary>
/// <param name="url">The Chargify URL</param>
/// <param name="apiKey">Your Chargify api key</param>
/// <param name="password">Your Chargify api password</param>
public ChargifyConnect(string url, string apiKey, string password)
{
this.URL = url;
this.apiKey = apiKey;
this.Password = password;
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="url">The Chargify URL</param>
/// <param name="apiKey">Your Chargify api key</param>
/// <param name="password">Your Chargify api password</param>
/// <param name="sharedKey">Your Chargify hosted page shared key</param>
public ChargifyConnect(string url, string apiKey, string password, string sharedKey)
{
this.URL = url;
this.apiKey = apiKey;
this.Password = password;
this.SharedKey = sharedKey;
}

#endregion

#region Properties
private static string UserAgent
{
get
{
if (_userAgent == null)
{
_userAgent = $"Chargify.NET Client v{typeof(ChargifyConnect).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}";
}
return _userAgent;
}
}
private static string _userAgent;

/// <summary>
/// Get or set the API key
/// </summary>
public string apiKey { get; set; }

/// <summary>
/// Get or set the password
/// </summary>
public string Password { get; set; }

/// <summary>
/// Get or set the URL for chargify
/// </summary>
public string URL { get; set; }

/// <summary>
/// SharedKey used for url generation
/// </summary>
public string SharedKey { get; set; }

/// <summary>
/// Should Chargify.NET use JSON for output? XML by default, always XML for input.
/// </summary>
public bool UseJSON { get; set; }

/// <summary>
/// Should the library require a CVV?
/// </summary>
public bool CvvRequired { get { return this._cvvRequired; } set { this._cvvRequired = value; } }
private bool _cvvRequired = true;


/// <summary>
/// The timeout (in milliseconds) for any call to Chargify. The default is 180000
/// </summary>
public int Timeout
{
get
{
return this._timeout;
}
set
{
this._timeout = value;
}
}
private int _timeout = 180000;

/// <summary>
/// Method for determining if the properties have been set to allow this instance to connect correctly.
/// </summary>
public bool HasConnected
{
get
{
bool result = true;
if (string.IsNullOrEmpty(this.apiKey)) result = false;
if (string.IsNullOrEmpty(this.Password)) result = false;
if (string.IsNullOrEmpty(this.URL)) result = false;
return result;
}
}

/// <summary>
/// Caller can plug in a delegate for logging raw Chargify requests
/// </summary>
public Action<HttpRequestMethod, string, string> LogRequest { get; set; }

/// <summary>
/// Caller can plug in a delegate for logging raw Chargify responses
/// </summary>
public Action<HttpStatusCode, string, string> LogResponse { get; set; }

/// <summary>
/// Get a reference to the last Http Response from the chargify server. This is set after every call to
/// a Chargify Connect method
/// </summary>
public HttpResponseMessage LastResponse
{
get
{
return _lastResponse;
}
}
private HttpResponseMessage _lastResponse = null;

#endregion

public void Test()
{
Console.WriteLine(DoRequest("/subscriptions/12670109.xml", HttpRequestMethod.Get).Result);
}

private async Task<string> DoRequest(string methodString, HttpRequestMethod requestMethod, string postData = null)
{
// make sure values are set
if (string.IsNullOrEmpty(this.URL)) throw new InvalidOperationException("URL not set");
if (string.IsNullOrEmpty(this.apiKey)) throw new InvalidOperationException("apiKey not set");
if (string.IsNullOrEmpty(this.Password)) throw new InvalidOperationException("Password not set");

// create the URI
string addressString = string.Format("{0}{1}", this.URL, (this.URL.EndsWith("/") ? string.Empty : "/"));

var uriBuilder = new UriBuilder(addressString)
{
Scheme = "https",
Port = -1 // default port for scheme
};
Uri address = uriBuilder.Uri;

var handler = new HttpClientHandler();
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
var client = new HttpClient(handler)
{
Timeout = TimeSpan.FromMilliseconds(this._timeout),
BaseAddress = address
};

var byteArray = Encoding.ASCII.GetBytes($"{this.apiKey}:{this.Password}");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(this.UseJSON ? "application/json" : "application/xml"));
client.DefaultRequestHeaders.TransferEncodingChunked = false;

try
{
// Send the request for logging, if applicable.
LogRequest?.Invoke(requestMethod, addressString+methodString, postData);

switch (requestMethod)
{
case HttpRequestMethod.Get:
_lastResponse = await client.GetAsync(methodString);
break;
case HttpRequestMethod.Post:
_lastResponse = await client.PostAsync(methodString, new StringContent(postData, Encoding.UTF8, this.UseJSON ? "application/json" : "text/xml"));
break;
case HttpRequestMethod.Put:
_lastResponse = await client.PutAsync(methodString, new StringContent(postData, Encoding.UTF8, this.UseJSON ? "application/json" : "text/xml"));
break;
case HttpRequestMethod.Delete:
_lastResponse = await client.DeleteAsync(methodString);
break;
}

var content = await _lastResponse?.Content?.ReadAsStringAsync();

// Send the response for logging, if applicable.
LogResponse?.Invoke(_lastResponse.StatusCode, addressString + methodString, content);

return content;
}
catch (Exception ex)
{

throw;
}

}
}
}

21 changes: 21 additions & 0 deletions Source/ChargifyNET.DotCore/ChargifyNET.DotCore.xproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>

<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>c0885dae-b697-4771-af67-93d0d8569056</ProjectGuid>
<RootNamespace>ChargifyNET.DotCore</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
26 changes: 26 additions & 0 deletions Source/ChargifyNET.DotCore/HttpRequestMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

namespace ChargifyNET
{
/// <summary>
/// The type of REST request
/// </summary>
public enum HttpRequestMethod
{
/// <summary>
/// Requests a representation of the specified resource
/// </summary>
Get,
/// <summary>
/// Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI
/// </summary>
Post,
/// <summary>
/// Requests that the enclosed entity be stored under the supplied URI
/// </summary>
Put,
/// <summary>
/// Deletes the specified resource
/// </summary>
Delete
}
}
19 changes: 19 additions & 0 deletions Source/ChargifyNET.DotCore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ChargifyNET.DotCore")]
[assembly: AssemblyTrademark("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c0885dae-b697-4771-af67-93d0d8569056")]
18 changes: 18 additions & 0 deletions Source/ChargifyNET.DotCore/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "1.0.0-*",

"dependencies": {
"Microsoft.DotNet.InternalAbstractions": "1.0.0",
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"System.Net.Http": "4.1.0",
"System.Net.Primitives": "4.0.11",
"System.Text.Encoding": "4.0.11"
},

"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
Loading

0 comments on commit e2cedde

Please sign in to comment.