Skip to content

Commit

Permalink
V3 architecture (#54)
Browse files Browse the repository at this point in the history
* - Framework compatibility update
* - Unit tests update
* - Doc update
* - Assembly available
  • Loading branch information
Antonio Buedo authored Jul 22, 2019
1 parent d8dc451 commit a85c0d1
Show file tree
Hide file tree
Showing 79 changed files with 311 additions and 711 deletions.
Binary file added Assemblies/BitPay.Net.Lib_3.0.1907.zip
Binary file not shown.
8 changes: 1 addition & 7 deletions Assemblies/BitPay.config.json.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"BitPayConfiguration": {
"Environment": "Test",
"Environment": "",
"EnvConfig": {
"Test": {
"ClientDescription": "",
"ApiUrl": "https://test.bitpay.com/",
"ApiVersion": "2.0.0",
"PrivateKeyPath": "",
"ApiTokens": {
"pos": "",
Expand All @@ -14,9 +11,6 @@
}
},
"Prod": {
"ClientDescription": "",
"ApiUrl": "https://bitpay.com/",
"ApiVersion": "2.0.0",
"PrivateKeyPath": "",
"ApiTokens": {
"pos": "",
Expand Down
104 changes: 66 additions & 38 deletions BitPay/BitPay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,53 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BitPayAPI.Exceptions;
using BitPayAPI.Models;
using BitPayAPI.Models.Bill;
using BitPayAPI.Models.Invoice;
using BitPayAPI.Models.Ledger;
using BitPayAPI.Models.Payout;
using BitPayAPI.Models.Rate;
using BitPayAPI.Models.Settlement;
using BitPaySDK.Exceptions;
using BitPaySDK.Models;
using BitPaySDK.Models.Bill;
using BitPaySDK.Models.Invoice;
using BitPaySDK.Models.Ledger;
using BitPaySDK.Models.Payout;
using BitPaySDK.Models.Rate;
using BitPaySDK.Models.Settlement;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;

/**
* @author Antonio Buedo
* @date 7.8.2019
* @version 2.2.1907
* @date 7.18.2019
* @version 3.0.1907
*
* See bitpay.com/api for more information.
*/

namespace BitPayAPI
namespace BitPaySDK
{
public class BitPay
{
private static IConfiguration _configuration { get; set; }
private static string _env;
private Dictionary<string, string> _tokenCache; // {facade, token}
private static string _configFilePath;

private const string BitpayApiVersion = "2.0.0";
private const string BitpayPluginInfo = "BitPay_DotNet_Client_v2.2.1907";

private string _baseUrl;
private string _clientName;
private EcKey _ecKey;

private HttpClient _httpClient;

/// <summary>
/// Constructor for use if the keys and SIN are managed by this library.
/// </summary>
/// <param name="environment">Target environment. Options: Env.Test / Env.Prod</param>
/// <param name="privateKeyPath">Private Key file path.</param>
/// <param name="tokens">Env.Tokens containing the available tokens.</param>
public BitPay(string environment, string privateKeyPath, Env.Tokens tokens)
{
_env = environment;
BuildConfig(privateKeyPath, tokens);
InitKeys().Wait();
Init().Wait();
}

/// <summary>
/// Constructor for use if the keys and SIN are managed by this library.
/// </summary>
Expand Down Expand Up @@ -84,7 +93,7 @@ public async Task AuthorizeClient(string pairingCode)
{
var token = new Token
{
Id = Identity, Guid = Guid.NewGuid().ToString(), PairingCode = pairingCode, Label = _clientName
Id = Identity, Guid = Guid.NewGuid().ToString(), PairingCode = pairingCode
};
var json = JsonConvert.SerializeObject(token);
var response = await Post("tokens", json);
Expand Down Expand Up @@ -114,8 +123,7 @@ public async Task<string> RequestClientAuthorization(string facade)
{
Id = Identity,
Guid = Guid.NewGuid().ToString(),
Facade = facade,
Label = _clientName
Facade = facade
};
var json = JsonConvert.SerializeObject(token);
var response = await Post("tokens", json).ConfigureAwait(false);
Expand Down Expand Up @@ -725,9 +733,8 @@ private async Task Init()
{
try
{
_baseUrl = _configuration.GetSection("BitPayConfiguration:EnvConfig:"+ _env +":ApiUrl").Value;
_baseUrl = _env == Env.Test ? Env.TestUrl : Env.ProdUrl;
_httpClient = new HttpClient {BaseAddress = new Uri(_baseUrl)};
NormalizeClientName(_configuration.GetSection("BitPayConfiguration:EnvConfig:"+ _env +":ClientDescription").Value);
DeriveIdentity();
await LoadAccessTokens();
}
Expand Down Expand Up @@ -867,8 +874,8 @@ private async Task<HttpResponseMessage> Get(string uri, Dictionary<string, strin
{
var fullUrl = _baseUrl + uri;
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("x-accept-version", BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", BitpayPluginInfo);
_httpClient.DefaultRequestHeaders.Add("x-accept-version", Env.BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", Env.BitpayPluginInfo);
if (parameters != null)
{
fullUrl += "?";
Expand Down Expand Up @@ -905,8 +912,8 @@ private async Task<HttpResponseMessage> Delete(string uri, Dictionary<string, st
{
var fullUrl = _baseUrl + uri;
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("x-accept-version", BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", BitpayPluginInfo);
_httpClient.DefaultRequestHeaders.Add("x-accept-version", Env.BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", Env.BitpayPluginInfo);

if (parameters != null)
{
Expand Down Expand Up @@ -940,8 +947,8 @@ private async Task<HttpResponseMessage> Post(string uri, string json, bool signa
{
var bodyContent = new StringContent(UnicodeToAscii(json));
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("x-accept-version", BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", BitpayPluginInfo);
_httpClient.DefaultRequestHeaders.Add("x-accept-version", Env.BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", Env.BitpayPluginInfo);
bodyContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
if (signatureRequired)
{
Expand All @@ -965,8 +972,8 @@ private async Task<HttpResponseMessage> Put(string uri, string json)
{
var bodyContent = new StringContent(UnicodeToAscii(json));
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("x-accept-version", BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", BitpayPluginInfo);
_httpClient.DefaultRequestHeaders.Add("x-accept-version", Env.BitpayApiVersion);
_httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", Env.BitpayPluginInfo);
bodyContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var signature = KeyUtils.Sign(_ecKey, _baseUrl + uri + json);
Expand Down Expand Up @@ -1049,15 +1056,6 @@ private string UnicodeToAscii(string json)
return new string(asciiChars);
}

private void NormalizeClientName(string clientName)
{
if (clientName.Equals(BitpayPluginInfo)) clientName += " on " + Environment.MachineName;

// Eliminate special characters from the client name (used as a token label). Trim to 60 chars.
_clientName = new Regex("[^a-zA-Z0-9_ ]").Replace(clientName, "_");
if (_clientName.Length > 60) _clientName = _clientName.Substring(0, 60);
}

/// <summary>
/// Loads the configuration file (JSON)
/// </summary>
Expand All @@ -1080,5 +1078,35 @@ private void GetConfig()
throw new ConfigNotFoundException(ex);
}
}

/// <summary>
/// Builds the configuration object
/// </summary>
/// <returns></returns>
private void BuildConfig(string privateKeyPath, Env.Tokens tokens)
{
try
{
if (!File.Exists(privateKeyPath))
{
throw new Exception("Private Key file not found");
}
var config = new Dictionary<string, string>
{
{"BitPayConfiguration:Environment", _env},
{"BitPayConfiguration:EnvConfig:" + _env + ":PrivateKeyPath", privateKeyPath},
{"BitPayConfiguration:EnvConfig:" + _env + ":ApiTokens:pos", tokens.POS},
{"BitPayConfiguration:EnvConfig:" + _env + ":ApiTokens:merchant", tokens.Merchant},
{"BitPayConfiguration:EnvConfig:" + _env + ":ApiTokens:payroll", tokens.Payout}
};

var builder = new ConfigurationBuilder().AddInMemoryCollection(config);
_configuration = builder.Build();
}
catch (Exception ex)
{
throw new ConfigNotFoundException(ex);
}
}
}
}
Loading

0 comments on commit a85c0d1

Please sign in to comment.