Skip to content

Commit 1513bde

Browse files
author
Kevin Hellemun
committed
Added and refactored contexts. (#83)
1 parent f29040f commit 1513bde

File tree

4 files changed

+167
-5
lines changed

4 files changed

+167
-5
lines changed

BunqSdk/Context/ApiContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static ApiContext Create(ApiEnvironmentType environmentType, string apiKe
8484
/// Create and initialize an API Context.
8585
/// </summary>
8686
public static ApiContext Create(ApiEnvironmentType environmentType, string apiKey, string deviceDescription,
87-
IList<string> permittedIps, string proxy = null)
87+
List<string> permittedIps, string proxy = null)
8888
{
8989
var apiContext = new ApiContext
9090
{
@@ -100,7 +100,7 @@ public static ApiContext Create(ApiEnvironmentType environmentType, string apiKe
100100
/// <summary>
101101
/// Initializes an API context with Installation, DeviceServer and a SessionServer.
102102
/// </summary>
103-
private void Initialize(string deviceDescription, IList<string> permittedIps)
103+
private void Initialize(string deviceDescription, List<string> permittedIps)
104104
{
105105
/* The calls below are order-sensitive: to initialize a Device Registration, we need an
106106
* Installation, and to initialize a Session we need a Device Registration. */
@@ -120,9 +120,9 @@ private void InitializeInstallationContext()
120120
InstallationContext = new InstallationContext(installationResponse.Value, keyPairClient);
121121
}
122122

123-
private void RegisterDevice(string deviceDescription, IList<string> permittedIps)
123+
private void RegisterDevice(string deviceDescription, List<string> permittedIps)
124124
{
125-
DeviceServer.Create(this, GenerateRequestBodyBytes(deviceDescription, permittedIps));
125+
DeviceServerInternal.Create(this, deviceDescription, ApiKey, permittedIps);
126126
}
127127

128128
private IDictionary<string, object> GenerateRequestBodyBytes(string description, IList<string> permittedIps)
@@ -168,7 +168,7 @@ public void CloseSession()
168168

169169
private void DeleteSession()
170170
{
171-
Session.Delete(this, SESSION_ID_DUMMY);
171+
Session.Delete(SESSION_ID_DUMMY);
172172
}
173173

174174
/// <summary>

BunqSdk/Context/BunqContext.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using Bunq.Sdk.Exception;
3+
4+
namespace Bunq.Sdk.Context
5+
{
6+
public static class BunqContext
7+
{
8+
private const string ErrorApicontextHasNotBeenLoaded = "apiContext has not been loaded.";
9+
private const string ErrorUserContextHasNotBeenLoaded = "userContext has not been loaded, you can load this by loading apiContext.";
10+
private static ApiContext apiContext;
11+
private static UserContext userContext;
12+
13+
public static ApiContext ApiContext {
14+
get
15+
{
16+
if (apiContext == null)
17+
{
18+
throw new BunqException(ErrorApicontextHasNotBeenLoaded);
19+
}
20+
else
21+
{
22+
return apiContext;
23+
}
24+
}
25+
private set => apiContext = value ?? throw new ArgumentNullException(nameof(value));
26+
}
27+
28+
public static UserContext UserContext
29+
{
30+
get
31+
{
32+
if (userContext == null)
33+
{
34+
throw new BunqException(ErrorUserContextHasNotBeenLoaded);
35+
}
36+
else
37+
{
38+
return userContext;
39+
}
40+
}
41+
private set => userContext = value ?? throw new ArgumentNullException(nameof(value));
42+
}
43+
44+
public static void LoadApiContext(ApiContext apiContextToLoad)
45+
{
46+
ApiContext = apiContextToLoad;
47+
UserContext = new UserContext(apiContextToLoad.SessionContext.UserId);
48+
UserContext.InitPrimaryMonetaryAccount();
49+
}
50+
}
51+
}

BunqSdk/Context/SessionContext.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Diagnostics;
3+
using Bunq.Sdk.Exception;
24
using Bunq.Sdk.Model.Core;
35
using Newtonsoft.Json;
46

@@ -9,6 +11,13 @@ namespace Bunq.Sdk.Context
911
/// </summary>
1012
public class SessionContext
1113
{
14+
/// <summary>
15+
/// Error constatns.
16+
/// </summary>
17+
private const string ErrorCouldNotDetermineUserId = "Could not determine user id.";
18+
private const string ErrorSessionserverUsercompanyIdNull = "sessionServer.UserCompany.Id != null";
19+
private const string ErrorsessionserverUserpersonIdNull = "sessionServer.UserPerson.Id != null";
20+
1221
/// <summary>
1322
/// Default assumed value for session timeout.
1423
/// </summary>
@@ -25,6 +34,9 @@ public class SessionContext
2534
/// </summary>
2635
[JsonProperty(PropertyName = "expiry_time")]
2736
public DateTime ExpiryTime { get; private set; }
37+
38+
[JsonProperty(PropertyName = "user_id")]
39+
public int UserId { get; private set; }
2840

2941
[JsonConstructor]
3042
private SessionContext()
@@ -35,6 +47,25 @@ public SessionContext(SessionServer sessionServer)
3547
{
3648
Token = sessionServer.SessionToken.Token;
3749
ExpiryTime = DateTime.Now.AddSeconds(GetSessionTimeout(sessionServer));
50+
UserId = GetUserId(sessionServer);
51+
}
52+
53+
private static int GetUserId(SessionServer sessionServer)
54+
{
55+
if (sessionServer.UserCompany != null && sessionServer.UserPerson == null)
56+
{
57+
Debug.Assert(sessionServer.UserCompany.Id != null, ErrorSessionserverUsercompanyIdNull);
58+
return sessionServer.UserCompany.Id.Value;
59+
}
60+
else if (sessionServer.UserPerson != null && sessionServer.UserCompany == null)
61+
{
62+
Debug.Assert(sessionServer.UserPerson.Id != null, ErrorsessionserverUserpersonIdNull);
63+
return sessionServer.UserPerson.Id.Value;
64+
}
65+
else
66+
{
67+
throw new BunqException(ErrorCouldNotDetermineUserId);
68+
}
3869
}
3970

4071
private static double GetSessionTimeout(SessionServer sessionServer)

BunqSdk/Context/UserContext.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using Bunq.Sdk.Exception;
3+
using Bunq.Sdk.Model.Core;
4+
using Bunq.Sdk.Model.Generated.Endpoint;
5+
6+
namespace Bunq.Sdk.Context
7+
{
8+
public class UserContext
9+
{
10+
/// <summary>
11+
/// Error constatns.
12+
/// </summary>
13+
private const string ErrorUnexpectedUser = "\'\"{0}\" is unexpected user instance.\'";
14+
15+
private const string ErrorNoActiveMonetaryAccountFound = "No active monetary account found.";
16+
private const string ErrorPirmaryMonetaryAccountHasNotBeenLoaded = "Pirmary monetary account has not been loaded.";
17+
private const string MonetaryAccountStatusActive = "ACTIVE";
18+
private MonetaryAccountBank primaryMonetaryAccountBank;
19+
20+
public UserPerson UserPerson { get; private set; }
21+
public UserCompany UserCompany { get; private set; }
22+
23+
public MonetaryAccountBank PrimaryMonetaryAccountBank
24+
{
25+
get => primaryMonetaryAccountBank ?? throw new BunqException(ErrorPirmaryMonetaryAccountHasNotBeenLoaded);
26+
private set => primaryMonetaryAccountBank = value ?? throw new ArgumentNullException(nameof(value));
27+
}
28+
29+
public int UserId { get; }
30+
31+
public UserContext(int userId)
32+
{
33+
UserId = userId;
34+
35+
var userObject = User.List().Value[0].GetReferencedObject();
36+
this.SetUser(userObject);
37+
}
38+
39+
private void SetUser(BunqModel user)
40+
{
41+
if (user.GetType() == typeof(UserPerson))
42+
{
43+
this.UserPerson = (UserPerson) user;
44+
}
45+
else if (user.GetType() == typeof(UserCompany))
46+
{
47+
this.UserCompany = (UserCompany) user;
48+
}
49+
else
50+
{
51+
throw new BunqException(string.Format(ErrorUnexpectedUser, user.GetType()));
52+
}
53+
}
54+
55+
public void InitPrimaryMonetaryAccount()
56+
{
57+
var allMonetaryAccount = MonetaryAccountBank.List().Value;
58+
59+
foreach (var accountBank in allMonetaryAccount)
60+
{
61+
if (!accountBank.Status.Equals(MonetaryAccountStatusActive)) continue;
62+
this.PrimaryMonetaryAccountBank = accountBank;
63+
64+
return;
65+
}
66+
67+
throw new BunqException(ErrorNoActiveMonetaryAccountFound);
68+
}
69+
70+
public bool IsOnlyUserPersonSet()
71+
{
72+
return UserCompany == null && UserPerson != null;
73+
}
74+
75+
public bool isOnlyUserCompanySet()
76+
{
77+
return UserPerson == null && UserCompany != null;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)