Skip to content

Commit 7044be7

Browse files
author
Kevin Hellemun
committed
Updated context. (#75)
1 parent 0436bd9 commit 7044be7

File tree

1 file changed

+156
-10
lines changed

1 file changed

+156
-10
lines changed

bunq/sdk/context.py

Lines changed: 156 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import aenum
44
from Cryptodome.PublicKey import RSA
55

6-
from bunq.sdk.model import core
76
from bunq.sdk import security
7+
from bunq.sdk.exception import BunqException
88
from bunq.sdk.json import converter
9+
from bunq.sdk.model import core
10+
from bunq.sdk.model.device_server_internal import DeviceServerInternal
911
from bunq.sdk.model.generated import endpoint
1012

1113

@@ -119,13 +121,11 @@ def _register_device(self, device_description,
119121
:rtype: None
120122
"""
121123

122-
endpoint.DeviceServer.create(
123-
self,
124-
{
125-
endpoint.DeviceServer.FIELD_DESCRIPTION: device_description,
126-
endpoint.DeviceServer.FIELD_SECRET: self.api_key,
127-
endpoint.DeviceServer.FIELD_PERMITTED_IPS: permitted_ips,
128-
}
124+
DeviceServerInternal.create(
125+
device_description,
126+
self.api_key,
127+
permitted_ips,
128+
api_context=self
129129
)
130130

131131
def _initialize_session(self):
@@ -136,8 +136,9 @@ def _initialize_session(self):
136136
session_server = core.SessionServer.create(self).value
137137
token = session_server.token.token
138138
expiry_time = self._get_expiry_timestamp(session_server)
139+
user_id = session_server.get_referenced_object().id_
139140

140-
self._session_context = SessionContext(token, expiry_time)
141+
self._session_context = SessionContext(token, expiry_time, user_id)
141142

142143
@classmethod
143144
def _get_expiry_timestamp(cls, session_server):
@@ -377,16 +378,18 @@ class SessionContext(object):
377378
"""
378379
:type _token: str
379380
:type _expiry_time: datetime.datetime
381+
:type _user_id: int
380382
"""
381383

382-
def __init__(self, token, expiry_time):
384+
def __init__(self, token, expiry_time, user_id):
383385
"""
384386
:type token: str
385387
:type expiry_time: datetime.datetime
386388
"""
387389

388390
self._token = token
389391
self._expiry_time = expiry_time
392+
self._user_id = user_id
390393

391394
@property
392395
def token(self):
@@ -403,3 +406,146 @@ def expiry_time(self):
403406
"""
404407

405408
return self._expiry_time
409+
410+
@property
411+
def user_id(self):
412+
return self._user_id
413+
414+
415+
class UserContext(object):
416+
_ERROR_UNEXPECTED_USER_INSTANCE = '"{}" is unexpected user instance.'
417+
_ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = \
418+
'No active monetary account found.'
419+
_STATUS_ACTIVE = 'ACTIVE'
420+
421+
def __init__(self, user_id):
422+
"""
423+
:type user_id: int
424+
"""
425+
426+
self._user_id = user_id
427+
self._user_person = None
428+
self._user_company = None
429+
self._primary_monetary_account = None
430+
431+
user_object = endpoint.User.list().value[0].get_referenced_object()
432+
self._set_user(user_object)
433+
434+
def _set_user(self, user):
435+
if isinstance(user, endpoint.UserPerson):
436+
self._user_person = user
437+
438+
elif isinstance(user, endpoint.UserCompany):
439+
self._user_company = user
440+
441+
else:
442+
raise BunqException(
443+
self._ERROR_UNEXPECTED_USER_INSTANCE.format(user.__class__))
444+
445+
def init_main_monetary_account(self):
446+
all_monetary_account = endpoint.MonetaryAccountBank.list().value
447+
448+
for account in all_monetary_account:
449+
if account.status == self._STATUS_ACTIVE:
450+
self._primary_monetary_account = account
451+
452+
return
453+
454+
raise BunqException(self._ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND)
455+
456+
@property
457+
def user_id(self):
458+
return self._user_id
459+
460+
def is_only_user_person_set(self):
461+
"""
462+
:rtype: bool
463+
"""
464+
465+
return self._user_person is not None and self._user_company is None
466+
467+
def is_only_user_company_set(self):
468+
"""
469+
:rtype: bool
470+
"""
471+
472+
return self._user_company is not None and self._user_person is None
473+
474+
def is_both_user_type_set(self):
475+
"""
476+
:rtype: bool
477+
"""
478+
479+
return self._user_company is not None and self._user_person is not None
480+
481+
@property
482+
def user_company(self):
483+
"""
484+
:rtype: endpoint.UserCompany
485+
"""
486+
487+
return self._user_company
488+
489+
@property
490+
def user_person(self):
491+
"""
492+
:rtype: endpoint.UserPerson
493+
"""
494+
495+
return self._user_person
496+
497+
@property
498+
def primary_monetary_account(self):
499+
"""
500+
:rtype: endpoint.MonetaryAccountBank
501+
"""
502+
503+
return self._primary_monetary_account
504+
505+
506+
class BunqContext(object):
507+
_ERROR_CLASS_SHOULD_NOT_BE_INITIALIZED = \
508+
'This class should not be instantiated'
509+
_ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED = \
510+
'ApiContext has not been loaded. Please load ApiContext in BunqContext'
511+
_ERROR_USER_CONTEXT_HAS_NOT_BEEN_LOADED = \
512+
'UserContext has not been loaded, please load this' \
513+
' by loading ApiContext.'
514+
515+
_api_context = None
516+
_user_context = None
517+
518+
def __init__(self):
519+
raise TypeError(self._ERROR_CLASS_SHOULD_NOT_BE_INITIALIZED)
520+
521+
@classmethod
522+
def load_api_context(cls, api_context):
523+
"""
524+
:type api_context: ApiContext
525+
"""
526+
527+
cls._api_context = api_context
528+
cls._user_context = UserContext(api_context.session_context.user_id)
529+
cls._user_context.init_main_monetary_account()
530+
531+
@classmethod
532+
def api_context(cls):
533+
"""
534+
:rtype: ApiContext
535+
"""
536+
537+
if cls._api_context is not None:
538+
return cls._api_context
539+
540+
raise BunqException(cls._ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED)
541+
542+
@classmethod
543+
def user_context(cls):
544+
"""
545+
:rtype: UserContext
546+
"""
547+
548+
if cls._user_context is not None:
549+
return cls._user_context
550+
551+
raise BunqException(cls._ERROR_USER_CONTEXT_HAS_NOT_BEEN_LOADED)

0 commit comments

Comments
 (0)