@@ -13,16 +13,17 @@ namespace Bunq.Sdk.Context
1313 public class SessionContext
1414 {
1515 /// <summary>
16- /// Error constatns .
16+ /// Error constants .
1717 /// </summary>
18- private const string ErrorCouldNotDetermineUserId = "Could not determine user id." ;
19- private const string ErrorSessionserverUserapikeyIdNull = "sessionServer.UserApiKey.Id != null" ;
20- private const string ErrorSessionserverUserpaymentserviceproviderkeyIdNull =
18+ private const string FIELD_ERROR_COULD_NOT_DETERMINE_USER_ID = "Could not determine user id." ;
19+ private const string FIELD_ERROR_SESSION_SERVER_USER_API_KEY_ID_NULL = "sessionServer.UserApiKey.Id != null" ;
20+ private const string FIELD_ERROR_SESSION_SERVER_USER_PAYMENT_SERVICE_PROVIDER_KEY_ID_NULL =
2121 "sessionServer.UserPaymentServiceProvider.Id != null" ;
22- private const string ErrorSessionserverUsercompanyIdNull = "sessionServer.UserCompany.Id != null" ;
23- private const string ErrorsessionserverUserpersonIdNull = "sessionServer.UserPerson.Id != null" ;
24- private const string ErrorCouldNotDetermineSessionTimeout = "Could not determine session timeout." ;
25- private const string ErrorSessionTimeoutIsNull = "Session timeout is null." ;
22+ private const string FIELD_ERROR_SESSION_SERVER_USER_COMPANY_ID_NULL = "sessionServer.UserCompany.Id != null" ;
23+ private const string FIELD_ERROR_SESSION_SERVER_USER_PERSON_ID_NULL = "sessionServer.UserPerson.Id != null" ;
24+ private const string FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT = "Could not determine session timeout." ;
25+ private const string FIELD_ERROR_SESSION_TIMEOUT_IS_NULL = "Session timeout is null." ;
26+ private const string FIELD_ERROR_ALL_FIELD_NULL = "All fields of an extended model or object are null." ;
2627
2728 /// <summary>
2829 /// Session token returned as a response to POST /session-server.
@@ -39,6 +40,18 @@ public class SessionContext
3940 [ JsonProperty ( PropertyName = "user_id" ) ]
4041 public int UserId { get ; private set ; }
4142
43+ [ JsonProperty ( PropertyName = "user_person" ) ]
44+ public UserPerson UserPerson { get ; private set ; }
45+
46+ [ JsonProperty ( PropertyName = "user_company" ) ]
47+ public UserCompany UserCompany { get ; private set ; }
48+
49+ [ JsonProperty ( PropertyName = "user_api_key" ) ]
50+ public UserApiKey UserApiKey { get ; private set ; }
51+
52+ [ JsonProperty ( PropertyName = "user_payment_service_provider" ) ]
53+ public UserPaymentServiceProvider UserPaymentServiceProvider { get ; private set ; }
54+
4255 [ JsonConstructor ]
4356 private SessionContext ( )
4457 {
@@ -49,41 +62,70 @@ public SessionContext(SessionServer sessionServer)
4962 Token = sessionServer . SessionToken . Token ;
5063 ExpiryTime = DateTime . Now . AddSeconds ( GetSessionTimeout ( sessionServer ) ) ;
5164 UserId = GetUserId ( sessionServer ) ;
65+ SetUser ( sessionServer . GetUserReference ( ) ) ;
66+ }
67+
68+ private void SetUser ( BunqModel user )
69+ {
70+ if ( user . GetType ( ) == typeof ( UserPerson ) )
71+ {
72+ UserPerson = ( UserPerson ) user ;
73+ }
74+ else if ( user . GetType ( ) == typeof ( UserCompany ) )
75+ {
76+ UserCompany = ( UserCompany ) user ;
77+ }
78+ else if ( user . GetType ( ) == typeof ( UserApiKey ) )
79+ {
80+ UserApiKey = ( UserApiKey ) user ;
81+ }
82+ else if ( user . GetType ( ) == typeof ( UserPaymentServiceProvider ) )
83+ {
84+ UserPaymentServiceProvider = ( UserPaymentServiceProvider ) user ;
85+ }
86+ else
87+ {
88+ throw new BunqException ( FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT ) ;
89+ }
5290 }
5391
5492 private static int GetUserId ( SessionServer sessionServer )
5593 {
5694 if ( sessionServer . UserCompany != null )
5795 {
58- Debug . Assert ( sessionServer . UserCompany . Id != null , ErrorSessionserverUsercompanyIdNull ) ;
96+ Debug . Assert ( sessionServer . UserCompany . Id != null ,
97+ FIELD_ERROR_SESSION_SERVER_USER_COMPANY_ID_NULL ) ;
5998
6099 return sessionServer . UserCompany . Id . Value ;
61100 }
62- else if ( sessionServer . UserPerson != null )
101+
102+ if ( sessionServer . UserPerson != null )
63103 {
64- Debug . Assert ( sessionServer . UserPerson . Id != null , ErrorsessionserverUserpersonIdNull ) ;
104+ Debug . Assert ( sessionServer . UserPerson . Id != null ,
105+ FIELD_ERROR_SESSION_SERVER_USER_PERSON_ID_NULL ) ;
65106
66107 return sessionServer . UserPerson . Id . Value ;
67108 }
68- else if ( sessionServer . UserApiKey != null )
109+
110+ if ( sessionServer . UserApiKey != null )
69111 {
70- Debug . Assert ( sessionServer . UserApiKey . Id != null , ErrorSessionserverUserapikeyIdNull ) ;
112+ Debug . Assert ( sessionServer . UserApiKey . Id != null ,
113+ FIELD_ERROR_SESSION_SERVER_USER_API_KEY_ID_NULL ) ;
71114
72115 return sessionServer . UserApiKey . Id . Value ;
73116 }
74- else if ( sessionServer . UserPaymentServiceProvider != null )
117+
118+ if ( sessionServer . UserPaymentServiceProvider != null )
75119 {
76120 Debug . Assert (
77121 sessionServer . UserPaymentServiceProvider . Id != null ,
78- ErrorSessionserverUserpaymentserviceproviderkeyIdNull
122+ FIELD_ERROR_SESSION_SERVER_USER_PAYMENT_SERVICE_PROVIDER_KEY_ID_NULL
79123 ) ;
80124
81125 return sessionServer . UserPaymentServiceProvider . Id . Value ;
82126 }
83- else
84- {
85- throw new BunqException ( ErrorCouldNotDetermineUserId ) ;
86- }
127+
128+ throw new BunqException ( FIELD_ERROR_COULD_NOT_DETERMINE_USER_ID ) ;
87129 }
88130
89131 private static double GetSessionTimeout ( SessionServer sessionServer )
@@ -92,22 +134,23 @@ private static double GetSessionTimeout(SessionServer sessionServer)
92134 {
93135 return GetSessionTimeOutForUser ( sessionServer . UserApiKey . RequestedByUser . GetReferencedObject ( ) ) ;
94136 }
95- else if ( sessionServer . UserCompany != null )
137+
138+ if ( sessionServer . UserCompany != null )
96139 {
97140 return GetSessionTimeOutForUser ( sessionServer . UserCompany ) ;
98141 }
99- else if ( sessionServer . UserPerson != null )
142+
143+ if ( sessionServer . UserPerson != null )
100144 {
101145 return GetSessionTimeOutForUser ( sessionServer . UserPerson ) ;
102146 }
103- else if ( sessionServer . UserPaymentServiceProvider != null )
147+
148+ if ( sessionServer . UserPaymentServiceProvider != null )
104149 {
105150 return GetSessionTimeOutForUser ( sessionServer . UserPaymentServiceProvider ) ;
106151 }
107- else
108- {
109- throw new BunqException ( ErrorCouldNotDetermineSessionTimeout ) ;
110- }
152+
153+ throw new BunqException ( FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT ) ;
111154 }
112155
113156 private static double GetSessionTimeOutForUser ( BunqModel user )
@@ -128,7 +171,7 @@ private static double GetSessionTimeOutForUser(BunqModel user)
128171 }
129172 else
130173 {
131- throw new BunqException ( ErrorCouldNotDetermineSessionTimeout ) ;
174+ throw new BunqException ( FIELD_ERROR_COULD_NOT_DETERMINE_SESSION_TIMEOUT ) ;
132175 }
133176
134177 return GetDoubleFromSessionTimeout ( sessionTimeout ) ;
@@ -138,12 +181,38 @@ private static double GetDoubleFromSessionTimeout(int? sessionTimeout)
138181 {
139182 if ( sessionTimeout == null )
140183 {
141- throw new BunqException ( ErrorSessionTimeoutIsNull ) ;
184+ throw new BunqException ( FIELD_ERROR_SESSION_TIMEOUT_IS_NULL ) ;
142185 }
143- else
186+
187+ return ( double ) sessionTimeout ;
188+ }
189+
190+ public BunqModel GetUserReference ( )
191+ {
192+ if ( UserCompany == null && UserApiKey == null && UserPerson != null && UserPaymentServiceProvider == null )
144193 {
145- return ( double ) sessionTimeout ;
194+ return UserPerson ;
146195 }
196+
197+ if ( UserPerson == null && UserApiKey == null && UserCompany != null &&
198+ UserPaymentServiceProvider == null )
199+ {
200+ return UserCompany ;
201+ }
202+
203+ if ( UserPerson == null && UserCompany == null && UserApiKey != null &&
204+ UserPaymentServiceProvider == null )
205+ {
206+ return UserApiKey ;
207+ }
208+
209+ if ( UserPerson == null && UserCompany == null && UserApiKey == null &&
210+ UserPaymentServiceProvider != null )
211+ {
212+ return UserPaymentServiceProvider ;
213+ }
214+
215+ throw new BunqException ( FIELD_ERROR_ALL_FIELD_NULL ) ;
147216 }
148217 }
149218}
0 commit comments