Skip to content

Commit

Permalink
Merge pull request #4 from figo-connect/develop
Browse files Browse the repository at this point in the history
Update SDK to correspond to the new version of API
  • Loading branch information
makst authored Aug 18, 2016
2 parents 4a51612 + c326349 commit 2201e79
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 49 deletions.
56 changes: 50 additions & 6 deletions ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
using figo;
using System;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace figo.ConsoleDemo {
class Program {
static void Main(string[] args) {
FigoSession session = new FigoSession { AccessToken = "ASHWLIkouP2O6_bgA2wWReRhletgWKHYjLqDaqb0LFfamim9RjexTo22ujRIP_cjLiRiSyQXyt2kM1eXU2XLFZQ0Hro15HikJQT_eNeT_9XQ" };
static void WaitTask(Task task)
{
try
{
task.Wait();
}
catch (AggregateException ae)
{
foreach (var e in ae.InnerExceptions)
{
// Handle only WebException
if (e is WebException)
{
Console.WriteLine("Message: {0}\nSource: {1}\nStackTrace: {2}", e.Message, e.Source, e.StackTrace);
Console.ReadLine();
Environment.Exit(1);
}
else
{
throw;
}
}
}
}

// print out a list of accounts including its balance
static void Main(string[] args) {
var validCertificates = new string[]
{
"38AE4A326F16EA1581338BB0D8E4A635E727F107",
"DBE2E9158FC9903084FE36CAA61138D85A205D93"
};
FigoSession session = new FigoSession {
AccessToken = "ASHWLIkouP2O6_bgA2wWReRhletgWKHYjLqDaqb0LFfamim9RjexTo22ujRIP_cjLiRiSyQXyt2kM1eXU2XLFZQ0Hro15HikJQT_eNeT_9XQ",
// the same as default one here, but can be modified for testing purposes
ApiEndpoint = "https://api.figo.me",
OnRequestInitialize = (request) =>
{
// figo.net is portable, not all of the platforms have a support for ServerCertificateValidationCallback
// that's why check certificate on the client side
request.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => {
X509Certificate x509Certificate = cert;
return validCertificates.Contains(x509Certificate.GetCertHashString());
};
}
};
// print out a list of accounts including its balance
var task_accounts = session.GetAccounts();
task_accounts.Wait();
WaitTask(task_accounts);
foreach(FigoAccount account in task_accounts.Result) {
Console.WriteLine(account.Name);

var task_balance = session.GetAccountBalance(account);
task_balance.Wait();
WaitTask(task_balance);
Console.WriteLine(task_balance.Result.Balance);
}

// print out the list of all transactions on a specific account
var task_transactions = session.GetTransactions("A1.2");
task_transactions.Wait();
WaitTask(task_transactions);
foreach(FigoTransaction transaction in task_transactions.Result) {
Console.WriteLine(transaction.Purpose);
}
Expand Down
95 changes: 73 additions & 22 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,80 @@ PM> Install-Package figo
And just as easy to use:
```csharp
using figo;
using System;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class FigoExample {
static void Main(string[] args) {
FigoSession session = new FigoSession("ASHWLIkouP2O6_bgA2wWReRhletgWKHYjLqDaqb0LFfamim9RjexTo22ujRIP_cjLiRiSyQXyt2kM1eXU2XLFZQ0Hro15HikJQT_eNeT_9XQ");

// print out a list of accounts including its balance
var task_accounts = session.getAccounts();
task_accounts.Wait();
foreach(FigoAccount account in task_accounts.Result) {
Console.WriteLine(account.Name);

var task_balance = session.getAccountBalance(account);
task_balance.Wait();
Console.WriteLine(task_balance.Result.Balance);
}

// print out the list of all transactions on a specific account
var task_transactions = session.getTransactions("A1.2");
task_transactions.Wait();
foreach(FigoTransaction transaction in task_transactions.Result) {
Console.WriteLine(transaction.PurposeText);
}
}
namespace figo.ConsoleDemo {
class Program {
static void WaitTask(Task task)
{
try
{
task.Wait();
}
catch (AggregateException ae)
{
foreach (var e in ae.InnerExceptions)
{
// Handle only WebException
if (e is WebException)
{
Console.WriteLine("Message: {0}\nSource: {1}\nStackTrace: {2}", e.Message, e.Source, e.StackTrace);
Console.ReadLine();
Environment.Exit(1);
}
else
{
throw;
}
}
}
}

static void Main(string[] args) {
var validCertificates = new string[]
{
"38AE4A326F16EA1581338BB0D8E4A635E727F107",
"DBE2E9158FC9903084FE36CAA61138D85A205D93"
};
FigoSession session = new FigoSession {
AccessToken = "ASHWLIkouP2O6_bgA2wWReRhletgWKHYjLqDaqb0LFfamim9RjexTo22ujRIP_cjLiRiSyQXyt2kM1eXU2XLFZQ0Hro15HikJQT_eNeT_9XQ",
// the same as default one here, but can be modified for testing purposes
ApiEndpoint = "https://api.figo.me",
OnRequestInitialize = (request) =>
{
// figo.net is portable, not all of the platforms have a support for ServerCertificateValidationCallback
// that's why check certificate on the client side
request.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => {
X509Certificate x509Certificate = cert;
return validCertificates.Contains(x509Certificate.GetCertHashString());
};
}
};
// print out a list of accounts including its balance
var task_accounts = session.GetAccounts();
WaitTask(task_accounts);
foreach(FigoAccount account in task_accounts.Result) {
Console.WriteLine(account.Name);

var task_balance = session.GetAccountBalance(account);
WaitTask(task_balance);
Console.WriteLine(task_balance.Result.Balance);
}

// print out the list of all transactions on a specific account
var task_transactions = session.GetTransactions("A1.2");
WaitTask(task_transactions);
foreach(FigoTransaction transaction in task_transactions.Result) {
Console.WriteLine(transaction.Purpose);
}
}
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion figo.net/FigoConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected async Task<String> DoRequest(string endpoint, string method = "GET", s
return json_error;
} else if((int)status_code == 400) {
FigoException.ErrorResponse error_response = JsonConvert.DeserializeObject<FigoException.ErrorResponse>(json_error);
throw new FigoException(error_response.Error, error_response.ErrorDescription);
throw new FigoException(error_response.Error.Code.ToString(), error_response.Error.Description);
} else if((int)status_code == 401) {
throw new FigoException("access_denied", "Access Denied");
} else if((int)status_code == 404) {
Expand Down
30 changes: 26 additions & 4 deletions figo.net/FigoException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,43 @@ public FigoException(String error_code, String error_message, Exception exc) : b
this.ErrorCode = error_code;
}

public FigoException(ErrorResponse response) : this(response.Error, response.ErrorDescription) {
public FigoException(ErrorResponse response) : this(response.Error.Code.ToString(), response.Error.Description) {
}

public string ErrorCode {
get;
private set;
}

[JsonObject]
public class ErrorData
{
[JsonProperty("code")]
public Int32 Code { get; set; }

[JsonProperty("data")]
public Object Data { get; set; }

[JsonProperty("description")]
public String Description { get; set; }

[JsonProperty("group")]
public String Group { get; set; }

[JsonProperty("message")]
public String Message { get; set; }

[JsonProperty("name")]
public String Name { get; set; }
}

[JsonObject]
public class ErrorResponse {
[JsonProperty("error")]
public String Error { get; set; }
public ErrorData Error { get; set; }

[JsonProperty("error_description")]
public String ErrorDescription { get; set; }
[JsonProperty("status")]
public Int32 Status { get; set; }
}
}
}
17 changes: 10 additions & 7 deletions figo.net/FigoSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net;
using System.Threading.Tasks;
using figo.models;
using System.Net.Http;

namespace figo
{
Expand All @@ -19,6 +20,10 @@ public string ApiEndpoint {
get { return _apiEndoint; }
set { _apiEndoint = value; }
}
/// <summary>
/// action to invoke to modify request, e.g. for assigning ServerCertificateValidationCallback
/// </summary>
public Action<HttpWebRequest> OnRequestInitialize { get; set; }

/// <summary>
/// The users access token used for authentication
Expand Down Expand Up @@ -48,14 +53,12 @@ protected async Task<T> DoRequest<T>(string endpoint, string method = "GET", obj
}

protected async Task<String> DoRequest(string endpoint, string method = "GET", string body = null) {
WebRequest req = (WebRequest)WebRequest.Create(ApiEndpoint + endpoint);
HttpWebRequest req = WebRequest.CreateHttp(ApiEndpoint + endpoint);
req.Method = method;
req.Headers["Authorization"] = "Bearer " + AccessToken;
if(req is HttpWebRequest) {
((HttpWebRequest)req).ContinueTimeout = Timeout;
((HttpWebRequest)req).Accept = "application/json";
}

req.ContinueTimeout = Timeout;
req.Accept = "application/json";
OnRequestInitialize?.Invoke(req);
if (body != null) {
req.ContentType = "application/json";

Expand Down Expand Up @@ -83,7 +86,7 @@ protected async Task<String> DoRequest(string endpoint, string method = "GET", s
return json_error;
} else if((int)status_code == 400) {
FigoException.ErrorResponse error_response = JsonConvert.DeserializeObject<FigoException.ErrorResponse>(json_error);
throw new FigoException(error_response.Error, error_response.ErrorDescription);
throw new FigoException(error_response.Error.Code.ToString(), error_response.Error.Description);
} else if((int)status_code == 401) {
throw new FigoException("access_denied", "Access Denied");
} else if((int)status_code == 404) {
Expand Down
8 changes: 4 additions & 4 deletions figo.net/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("figo")]
[assembly: AssemblyDescription(".net client library for figo.io")]
[assembly: AssemblyDescription(".NET client library for figo.io")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("figo GmbH")]
[assembly: AssemblyProduct("figo")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]
Expand All @@ -26,5 +26,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
4 changes: 2 additions & 2 deletions figo.net/figo.net.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<authors>$author$</authors>
<owners>$author$</owners>
<projectUrl>https://github.com/figo-connect/net-figo</projectUrl>
<iconUrl>http://figo.me/wp-content/uploads/2012/03/favicon1.ico</iconUrl>
<iconUrl>https://www.figo.io/app/themes/figo/favicon-32x32.png?v1</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>Copyright 2014</copyright>
<copyright>Copyright 2016</copyright>
<tags>finance SDK</tags>
</metadata>
</package>
6 changes: 3 additions & 3 deletions figo.net/models/FigoUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ public class FigoUser {
/// <summary>
/// This flag indicates whether the figo Account plan is free or premium
/// </summary>
[JsonProperty("premium")]
[Obsolete("Deprecated.", true)]
public bool IsPremium { get; set; }
public bool ShouldSerializeIsPremium() { return false; }

/// <summary>
/// Timestamp of premium figo Account expiry
/// </summary>
[JsonProperty("premium_expires_on")]
[Obsolete("Deprecated.", true)]
public DateTime PremiumExpiresOn { get; set; }
public bool ShouldSerializePremiumExpiresOn() { return false; }

/// <summary>
/// Provider for premium subscription or Null of no subscription is active
/// </summary>
[JsonProperty("premium_subscription")]
[Obsolete("Deprecated.", true)]
public String PremiumSubscription { get; set; }
public bool ShouldSerializePremiumSubscription() { return false; }

Expand Down

0 comments on commit 2201e79

Please sign in to comment.