Skip to content

Commit

Permalink
Merge pull request #113 from bunq/oauth-bunq/sdk_csharp#112
Browse files Browse the repository at this point in the history
Oauth #112
  • Loading branch information
OGKevin authored Jul 24, 2018
2 parents 83355f6 + a32be4a commit 5014057
Show file tree
Hide file tree
Showing 49 changed files with 571 additions and 2,697 deletions.
12 changes: 7 additions & 5 deletions BunqSdk.Tests/Model/Generated/Endpoint/CardDebitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public class CardDebitTest : BunqSdkTestBase
public void TestOrderNewMaestroCard()
{
SetUpTestCase();

var cardPinAssignment = new CardPinAssignment(
CardPinAssignmentTypePrimary,
PinCode,
BunqContext.UserContext.PrimaryMonetaryAccountBank.Id
);
CardPinAssignmentTypePrimary
)
{
PinCode = PinCode,
MonetaryAccountId = BunqContext.UserContext.PrimaryMonetaryAccountBank.Id
};
var allCardPinAssignments = new List<CardPinAssignment> {cardPinAssignment};
var cardDebit = CardDebit.Create(
GenerateRandomSecondLine(),
Expand Down
44 changes: 0 additions & 44 deletions BunqSdk.Tests/Model/Generated/Endpoint/PaymentChatTest.cs

This file was deleted.

12 changes: 0 additions & 12 deletions BunqSdk.Tests/Model/Generated/Object/NotificationUrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,6 @@ public void TestBunqMeTabModel()
);
}

[Fact]
public void TestChatMessageAnnouncementModel()
{
ExecuteNotificationUrlTest(
JsonPathChatMessageAnnouncementModel,
typeof(ChatMessage),
GetChatMessage,
GetChatMessageAnnouncement,
typeof(ChatMessageAnnouncement)
);
}

[Fact]
public void TestDraftPaymentModel()
{
Expand Down
69 changes: 57 additions & 12 deletions BunqSdk/Context/SessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using Bunq.Sdk.Exception;
using Bunq.Sdk.Model.Core;
using Bunq.Sdk.Model.Generated.Endpoint;
using Newtonsoft.Json;

namespace Bunq.Sdk.Context
Expand All @@ -15,13 +16,11 @@ public class SessionContext
/// Error constatns.
/// </summary>
private const string ErrorCouldNotDetermineUserId = "Could not determine user id.";
private const string ErrorSessionserverUserapikeyIdNull = "sessionServer.UserApiKey.Id != null";
private const string ErrorSessionserverUsercompanyIdNull = "sessionServer.UserCompany.Id != null";
private const string ErrorsessionserverUserpersonIdNull = "sessionServer.UserPerson.Id != null";

/// <summary>
/// Default assumed value for session timeout.
/// </summary>
private const double SESSION_TIMEOUT_DEFAULT = 0.0;
private const string ErrorCouldNotDetermineSessionTimeout = "Could not determine session timeout.";
private const string ErrorSessionTimeoutIsNull = "Session timeout is null.";

/// <summary>
/// Session token returned as a response to POST /session-server.
Expand Down Expand Up @@ -52,16 +51,24 @@ public SessionContext(SessionServer sessionServer)

private static int GetUserId(SessionServer sessionServer)
{
if (sessionServer.UserCompany != null && sessionServer.UserPerson == null)
if (sessionServer.UserCompany != null && sessionServer.UserApiKey == null &&sessionServer.UserPerson == null)
{
Debug.Assert(sessionServer.UserCompany.Id != null, ErrorSessionserverUsercompanyIdNull);

return sessionServer.UserCompany.Id.Value;
}
else if (sessionServer.UserPerson != null && sessionServer.UserCompany == null)
else if (sessionServer.UserPerson != null && sessionServer.UserApiKey == null && sessionServer.UserCompany == null)
{
Debug.Assert(sessionServer.UserPerson.Id != null, ErrorsessionserverUserpersonIdNull);

return sessionServer.UserPerson.Id.Value;
}
else if (sessionServer.UserApiKey != null && sessionServer.UserCompany == null && sessionServer.UserPerson == null)
{
Debug.Assert(sessionServer.UserApiKey.Id != null, ErrorSessionserverUserapikeyIdNull);

return sessionServer.UserApiKey.Id.Value;
}
else
{
throw new BunqException(ErrorCouldNotDetermineUserId);
Expand All @@ -70,17 +77,55 @@ private static int GetUserId(SessionServer sessionServer)

private static double GetSessionTimeout(SessionServer sessionServer)
{
if (sessionServer.UserCompany != null && sessionServer.UserCompany.SessionTimeout != null)
if (sessionServer.UserApiKey != null && sessionServer.UserCompany == null && sessionServer.UserPerson == null)
{
return GetSesisonTimeOutForUser(sessionServer.UserApiKey.RequestedByUser.GetReferencedObject());
}
else if (sessionServer.UserCompany != null && sessionServer.UserApiKey == null && sessionServer.UserPerson == null)
{
return (double) sessionServer.UserCompany.SessionTimeout;
return GetSesisonTimeOutForUser(sessionServer.UserCompany);
}
else if (sessionServer.UserPerson != null && sessionServer.UserApiKey == null && sessionServer.UserCompany == null)
{
return GetSesisonTimeOutForUser(sessionServer.UserPerson);
}
else
{
throw new BunqException(ErrorCouldNotDetermineSessionTimeout);
}
}

private static double GetSesisonTimeOutForUser(BunqModel user)
{
int? sessionTimeout;

if (sessionServer.UserPerson != null && sessionServer.UserPerson.SessionTimeout != null)
if (user.GetType() == typeof(UserPerson))
{
return (double) sessionServer.UserPerson.SessionTimeout;
sessionTimeout = ((UserPerson) user).SessionTimeout;
}
else if (user.GetType() == typeof(UserCompany))
{
sessionTimeout = ((UserCompany) user).SessionTimeout;
}
else
{
throw new BunqException(ErrorCouldNotDetermineSessionTimeout);
}

return GetDoubleFromSessionTimeout(sessionTimeout);
}

return SESSION_TIMEOUT_DEFAULT;
private static double GetDoubleFromSessionTimeout(int? sessionTimeout)
{
if (sessionTimeout == null)
{
throw new BunqException(ErrorSessionTimeoutIsNull);
}
else
{
return (double) sessionTimeout;
}
}

}
}
14 changes: 12 additions & 2 deletions BunqSdk/Context/UserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class UserContext

public UserPerson UserPerson { get; private set; }
public UserCompany UserCompany { get; private set; }
public UserApiKey UserApiKey { get; private set; }

public MonetaryAccountBank PrimaryMonetaryAccountBank
{
Expand Down Expand Up @@ -51,6 +52,10 @@ private void SetUser(BunqModel user)
{
this.UserCompany = (UserCompany) user;
}
else if (user.GetType() == typeof(UserApiKey))
{
this.UserApiKey = (UserApiKey) user;
}
else
{
throw new BunqException(string.Format(ErrorUnexpectedUser, user.GetType()));
Expand All @@ -74,12 +79,17 @@ public void InitPrimaryMonetaryAccount()

public bool IsOnlyUserPersonSet()
{
return UserCompany == null && UserPerson != null;
return UserCompany == null && UserApiKey == null && UserPerson != null;
}

public bool IsOnlyUserCompanySet()
{
return UserPerson == null && UserCompany != null;
return UserPerson == null && UserApiKey == null && UserCompany != null;
}

public bool IsOnlyUserApiKeySet()
{
return UserApiKey == null && UserCompany != null && UserPerson != null;
}

public void RefreshUserContext()
Expand Down
43 changes: 30 additions & 13 deletions BunqSdk/Json/SessionServerConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Bunq.Sdk.Exception;
using Bunq.Sdk.Model.Core;
using Bunq.Sdk.Model.Generated.Endpoint;
using Newtonsoft.Json;
Expand All @@ -12,27 +13,43 @@ namespace Bunq.Sdk.Json
/// </summary>
public class SessionServerConverter : JsonConverter
{
private const int INDEX_ID = 0;
private const string FIELD_ID = "Id";
private const string ErrorCouldNotDetermineUser = "Could not determine user.";

private const int INDEX_TOKEN = 1;
private const string FIELD_TOKEN = "Token";
private const int IndexId = 0;
private const string FieldId = "Id";

private const int INDEX_USER = 2;
private const string FIELD_USER_COMPANY = "UserCompany";
private const string FIELD_USER_PERSON = "UserPerson";
private const int IndexToken = 1;
private const string FieldToken = "Token";

private const int IndexUser = 2;
private const string FieldUserApiKey = "UserApiKey";
private const string FieldUserCompany = "UserCompany";
private const string FieldUserPerson = "UserPerson";

public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
var jObjects = JArray.Load(reader);
var id = FetchObject<Id>(jObjects[INDEX_ID], FIELD_ID);
var token = FetchObject<SessionToken>(jObjects[INDEX_TOKEN], FIELD_TOKEN);
var userBody = jObjects[INDEX_USER];
var id = FetchObject<Id>(jObjects[IndexId], FieldId);
var token = FetchObject<SessionToken>(jObjects[IndexToken], FieldToken);
var userBody = jObjects[IndexUser];

return userBody[FIELD_USER_COMPANY] == null
? new SessionServer(id, token, FetchObject<UserPerson>(userBody, FIELD_USER_PERSON))
: new SessionServer(id, token, FetchObject<UserCompany>(userBody, FIELD_USER_COMPANY));
if (userBody[FieldUserApiKey] != null)
{
return new SessionServer(id, token, FetchObject<UserApiKey>(userBody, FieldUserApiKey));
}
else if (userBody[FieldUserCompany] != null)
{
return new SessionServer(id, token, FetchObject<UserCompany>(userBody, FieldUserCompany));
}
else if (userBody[FieldUserPerson] != null)
{
return new SessionServer(id, token, FetchObject<UserPerson>(userBody, FieldUserPerson));
}
else
{
throw new BunqException(ErrorCouldNotDetermineUser);
}
}

private static T FetchObject<T>(JToken jToken, string fieldName)
Expand Down
8 changes: 8 additions & 0 deletions BunqSdk/Model/Core/SessionServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SessionServer : BunqModel

public Id Id { get; private set; }
public SessionToken SessionToken { get; private set; }
public UserApiKey UserApiKey { get; private set; }
public UserCompany UserCompany { get; private set; }
public UserPerson UserPerson { get; private set; }

Expand All @@ -37,6 +38,13 @@ public SessionServer(Id id, SessionToken sessionToken, UserPerson userPerson)
SessionToken = sessionToken;
UserPerson = userPerson;
}

public SessionServer(Id id, SessionToken sessionToken, UserApiKey userApiKey)
{
Id = id;
SessionToken = sessionToken;
UserApiKey = userApiKey;
}

/// <summary>
/// Create a new session for a DeviceServer. Provide the Installation token
Expand Down
22 changes: 22 additions & 0 deletions BunqSdk/Model/Generated/Endpoint/BillingContractSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public class BillingContractSubscription : BunqModel
[JsonProperty(PropertyName = "subscription_type")]
public string SubscriptionType { get; set; }

/// <summary>
/// The subscription status.
/// </summary>
[JsonProperty(PropertyName = "status")]
public string Status { get; set; }

/// <summary>
/// The subscription substatus.
/// </summary>
[JsonProperty(PropertyName = "sub_status")]
public string SubStatus { get; set; }

/// <summary>
/// </summary>
/// <param name="subscriptionType">The subscription type of the user. Can be one of PERSON_LIGHT_V1, PERSON_MORE_V1, PERSON_FREE_V1, PERSON_PREMIUM_V1, COMPANY_V1, or COMPANY_V2.</param>
Expand Down Expand Up @@ -152,6 +164,16 @@ public override bool IsAllFieldNull()
return false;
}

if (this.Status != null)
{
return false;
}

if (this.SubStatus != null)
{
return false;
}

return true;
}

Expand Down
8 changes: 2 additions & 6 deletions BunqSdk/Model/Generated/Endpoint/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class Card : BunqModel
public const string FIELD_LIMIT = "limit";
public const string FIELD_MAG_STRIPE_PERMISSION = "mag_stripe_permission";
public const string FIELD_COUNTRY_PERMISSION = "country_permission";
public const string FIELD_MONETARY_ACCOUNT_CURRENT_ID = "monetary_account_current_id";
public const string FIELD_PIN_CODE_ASSIGNMENT = "pin_code_assignment";
public const string FIELD_MONETARY_ACCOUNT_ID_FALLBACK = "monetary_account_id_fallback";

Expand Down Expand Up @@ -186,14 +185,12 @@ public class Card : BunqModel
/// <param name="limit">The limits to define for the card, among CARD_LIMIT_CONTACTLESS, CARD_LIMIT_ATM, CARD_LIMIT_DIPPING and CARD_LIMIT_POS_ICC (e.g. 25 EUR for CARD_LIMIT_CONTACTLESS). All the limits must be provided on update.</param>
/// <param name="magStripePermission">Whether or not it is allowed to use the mag stripe for the card.</param>
/// <param name="countryPermission">The countries for which to grant (temporary) permissions to use the card.</param>
/// <param name="monetaryAccountCurrentId">The ID of the monetary account that card transactions will use.</param>
/// <param name="pinCodeAssignment">Array of Types, PINs, account IDs assigned to the card.</param>
/// <param name="monetaryAccountIdFallback">ID of the MA to be used as fallback for this card if insufficient balance. Fallback account is removed if not supplied.</param>
public static BunqResponse<Card> Update(int cardId, string pinCode = null, string activationCode = null,
string status = null, List<CardLimit> limit = null, CardMagStripePermission magStripePermission = null,
List<CardCountryPermission> countryPermission = null, int? monetaryAccountCurrentId = null,
List<CardPinAssignment> pinCodeAssignment = null, int? monetaryAccountIdFallback = null,
IDictionary<string, string> customHeaders = null)
List<CardCountryPermission> countryPermission = null, List<CardPinAssignment> pinCodeAssignment = null,
int? monetaryAccountIdFallback = null, IDictionary<string, string> customHeaders = null)
{
if (customHeaders == null) customHeaders = new Dictionary<string, string>();

Expand All @@ -207,7 +204,6 @@ public static BunqResponse<Card> Update(int cardId, string pinCode = null, strin
{FIELD_LIMIT, limit},
{FIELD_MAG_STRIPE_PERMISSION, magStripePermission},
{FIELD_COUNTRY_PERMISSION, countryPermission},
{FIELD_MONETARY_ACCOUNT_CURRENT_ID, monetaryAccountCurrentId},
{FIELD_PIN_CODE_ASSIGNMENT, pinCodeAssignment},
{FIELD_MONETARY_ACCOUNT_ID_FALLBACK, monetaryAccountIdFallback},
};
Expand Down
Loading

0 comments on commit 5014057

Please sign in to comment.