Skip to content

Commit 29913ff

Browse files
author
Pascal Wink
committed
update classname to ApiObject or Object
1 parent 83f2374 commit 29913ff

33 files changed

+1701
-1272
lines changed

bunq/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from bunq.sdk.http.pagination import Pagination
66
from bunq.sdk.json import converter
77
from bunq.sdk.model.core.anchor_object_interface import AnchorObjectInterface
8-
from bunq.sdk.model.generated.object_ import Geolocation, ShareDetail, MonetaryAccountReference
8+
from bunq.sdk.model.generated.object_ import GeolocationObject, ShareDetailObject, MonetaryAccountReference
99
from bunq.sdk.util.type_alias import T
1010

1111

@@ -37,9 +37,9 @@ def initialize_converter() -> None:
3737
converter.register_adapter(InstallationContext, InstallationContextAdapter)
3838
converter.register_adapter(ApiEnvironmentType, ApiEnvironmentTypeAdapter)
3939
converter.register_adapter(float, FloatAdapter)
40-
converter.register_adapter(Geolocation, GeolocationAdapter)
40+
converter.register_adapter(GeolocationObject, GeolocationAdapter)
4141
converter.register_adapter(MonetaryAccountReference, MonetaryAccountReferenceAdapter)
42-
converter.register_adapter(ShareDetail, ShareDetailAdapter)
42+
converter.register_adapter(ShareDetailObject, ShareDetailAdapter)
4343
converter.register_adapter(datetime.datetime, DateTimeAdapter)
4444
converter.register_adapter(Pagination, PaginationAdapter)
4545

bunq/sdk/context/api_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from bunq.sdk.exception.bunq_exception import BunqException
1313
from bunq.sdk.json import converter
1414
from bunq.sdk.model.core.payment_service_provider_credential_internal import PaymentServiceProviderCredentialInternal
15-
from bunq.sdk.model.generated.endpoint import UserCredentialPasswordIp, Session
15+
from bunq.sdk.model.generated.endpoint import UserCredentialPasswordIpApiObject, SessionApiObject
1616
from bunq.sdk.security import security
1717

1818
if typing.TYPE_CHECKING:
@@ -115,7 +115,7 @@ def __initialize_installation(self) -> None:
115115
def __initialize_psd2_credential(self,
116116
certificate: str,
117117
private_key: str,
118-
all_chain_certificate: List[str]) -> UserCredentialPasswordIp:
118+
all_chain_certificate: List[str]) -> UserCredentialPasswordIpApiObject:
119119
session_token = self.installation_context.token
120120
client_key_pair = self.installation_context.private_key_client
121121

bunq/sdk/context/session_context.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
from bunq.sdk.exception.bunq_exception import BunqException
77
from bunq.sdk.model.core.bunq_model import BunqModel
88
from bunq.sdk.model.core.session_token import SessionToken
9-
from bunq.sdk.model.generated.endpoint import UserPerson, UserCompany, UserApiKey, UserPaymentServiceProvider
9+
from bunq.sdk.model.generated.endpoint import UserPersonApiObject, UserCompanyApiObject, UserApiKeyApiObject, UserPaymentServiceProviderApiObject
1010

1111

1212
class SessionContext:
1313
"""
1414
:type _token: str
1515
:type _expiry_time: datetime.datetime
1616
:type _user_id: int
17-
:type _user_person: UserPerson|None
18-
:type _user_company: UserCompany|None
19-
:type _user_api_key: UserApiKey|None
20-
:type _user_payment_service_provider: UserPaymentServiceProvider|None
17+
:type _user_person: UserPersonApiObject|None
18+
:type _user_company: UserCompanyApiObject|None
19+
:type _user_api_key: UserApiKeyApiObject|None
20+
:type _user_payment_service_provider: UserPaymentServiceProviderApiObject|None
2121
"""
2222

2323
# Error constants
@@ -37,19 +37,19 @@ def user_id(self) -> int:
3737
return self._user_id
3838

3939
@property
40-
def user_person(self) -> Optional[UserPerson]:
40+
def user_person(self) -> Optional[UserPersonApiObject]:
4141
return self._user_person
4242

4343
@property
44-
def user_company(self) -> Optional[UserCompany]:
44+
def user_company(self) -> Optional[UserCompanyApiObject]:
4545
return self._user_company
4646

4747
@property
48-
def user_api_key(self) -> Optional[UserApiKey]:
48+
def user_api_key(self) -> Optional[UserApiKeyApiObject]:
4949
return self._user_api_key
5050

5151
@property
52-
def user_payment_service_provider(self) -> Optional[UserPaymentServiceProvider]:
52+
def user_payment_service_provider(self) -> Optional[UserPaymentServiceProviderApiObject]:
5353
return self._user_payment_service_provider
5454

5555
def __init__(self, token: SessionToken, expiry_time: datetime.datetime, user: BunqModel) -> None:
@@ -63,28 +63,28 @@ def __init__(self, token: SessionToken, expiry_time: datetime.datetime, user: Bu
6363
self.__set_user(user)
6464

6565
def __get_user_id(self, user: BunqModel) -> int:
66-
if isinstance(user, UserPerson):
66+
if isinstance(user, UserPersonApiObject):
6767
return user.id_
6868

69-
if isinstance(user, UserCompany):
69+
if isinstance(user, UserCompanyApiObject):
7070
return user.id_
7171

72-
if isinstance(user, UserApiKey):
72+
if isinstance(user, UserApiKeyApiObject):
7373
return user.id_
7474

75-
if isinstance(user, UserPaymentServiceProvider):
75+
if isinstance(user, UserPaymentServiceProviderApiObject):
7676
return user.id_
7777

7878
raise BunqException(self._ERROR_UNEXPECTED_USER_INSTANCE)
7979

8080
def __set_user(self, user: BunqModel):
81-
if isinstance(user, UserPerson):
81+
if isinstance(user, UserPersonApiObject):
8282
self._user_person = user
83-
elif isinstance(user, UserCompany):
83+
elif isinstance(user, UserCompanyApiObject):
8484
self._user_company = user
85-
elif isinstance(user, UserApiKey):
85+
elif isinstance(user, UserApiKeyApiObject):
8686
self._user_api_key = user
87-
elif isinstance(user, UserPaymentServiceProvider):
87+
elif isinstance(user, UserPaymentServiceProviderApiObject):
8888
self._user_payment_service_provider = user
8989
else:
9090
raise BunqException(self._ERROR_UNEXPECTED_USER_INSTANCE)

bunq/sdk/context/user_context.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from bunq.sdk.exception.bunq_exception import BunqException
22
from bunq.sdk.model.core.bunq_model import BunqModel
3-
from bunq.sdk.model.generated.endpoint import UserPerson, UserCompany, UserApiKey, MonetaryAccountBank, User, \
4-
UserPaymentServiceProvider
3+
from bunq.sdk.model.generated.endpoint import UserPersonApiObject, UserCompanyApiObject, UserApiKeyApiObject, MonetaryAccountBankApiObject, UserApiObject, \
4+
UserPaymentServiceProviderApiObject
55

66

77
class UserContext:
@@ -21,19 +21,19 @@ def __init__(self, user_id: int, user: BunqModel) -> None:
2121

2222
@staticmethod
2323
def __get_user_object() -> BunqModel:
24-
return User.list().value[0].get_referenced_object()
24+
return UserApiObject.list().value[0].get_referenced_object()
2525

2626
def _set_user(self, user: BunqModel) -> None:
27-
if isinstance(user, UserPerson):
27+
if isinstance(user, UserPersonApiObject):
2828
self._user_person = user
2929

30-
elif isinstance(user, UserCompany):
30+
elif isinstance(user, UserCompanyApiObject):
3131
self._user_company = user
3232

33-
elif isinstance(user, UserApiKey):
33+
elif isinstance(user, UserApiKeyApiObject):
3434
self._user_api_key = user
3535

36-
elif isinstance(user, UserPaymentServiceProvider):
36+
elif isinstance(user, UserPaymentServiceProviderApiObject):
3737
self._user_payment_service_provider = user
3838

3939
else:
@@ -44,7 +44,7 @@ def init_main_monetary_account(self) -> None:
4444
if self._user_payment_service_provider is not None:
4545
return
4646

47-
all_monetary_account = MonetaryAccountBank.list().value
47+
all_monetary_account = MonetaryAccountBankApiObject.list().value
4848

4949
for account in all_monetary_account:
5050
if account.status == self._STATUS_ACTIVE:
@@ -87,17 +87,17 @@ def refresh_user_context(self) -> None:
8787
self.init_main_monetary_account()
8888

8989
@property
90-
def user_company(self) -> UserCompany:
90+
def user_company(self) -> UserCompanyApiObject:
9191
return self._user_company
9292

9393
@property
94-
def user_person(self) -> UserPerson:
94+
def user_person(self) -> UserPersonApiObject:
9595
return self._user_person
9696

9797
@property
98-
def user_api_key(self) -> UserApiKey:
98+
def user_api_key(self) -> UserApiKeyApiObject:
9999
return self._user_api_key
100100

101101
@property
102-
def primary_monetary_account(self) -> MonetaryAccountBank:
102+
def primary_monetary_account(self) -> MonetaryAccountBankApiObject:
103103
return self._primary_monetary_account

bunq/sdk/json/anchor_object_adapter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,21 @@ def _get_object_class(cls, class_name: str) -> BunqModel:
5151
if class_name in cls._override_field_map:
5252
class_name = cls._override_field_map[class_name]
5353

54+
try:
55+
return getattr(endpoint, class_name + "ApiObject")
56+
except AttributeError:
57+
pass
58+
5459
try:
5560
return getattr(endpoint, class_name)
5661
except AttributeError:
5762
pass
5863

64+
try:
65+
return getattr(object_, class_name + "Object")
66+
except AttributeError:
67+
pass
68+
5969
try:
6070
return getattr(object_, class_name)
6171
except AttributeError:

bunq/sdk/json/converter.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,58 @@ def _str_to_type_from_member_module(cls,
248248
module_: ModuleType,
249249
string: str) -> Type[T]:
250250
"""
251-
252251
:raise: BunqException when could not find the class for the string.
253252
"""
254-
253+
# Handle the case where string doesn't contain a dot
254+
if cls._DELIMITER_MODULE not in string:
255+
# Try direct lookup in current module
256+
if hasattr(module_, string):
257+
return getattr(module_, string)
258+
259+
# Check module type and try appropriate suffix
260+
if "object_" in module_.__name__:
261+
# For object_ modules, try with Object suffix
262+
obj_name = string + "Object"
263+
if hasattr(module_, obj_name):
264+
return getattr(module_, obj_name)
265+
elif "endpoint" in module_.__name__:
266+
# For endpoint modules, try with ApiObject suffix
267+
api_name = string + "ApiObject"
268+
if hasattr(module_, api_name):
269+
return getattr(module_, api_name)
270+
else:
271+
# For other modules, try ApiObject suffix as default
272+
api_name = string + "ApiObject"
273+
if hasattr(module_, api_name):
274+
return getattr(module_, api_name)
275+
276+
# If not found, fallback to legacy behavior with appropriate error
277+
error_message = cls._ERROR_COULD_NOT_FIND_CLASS.format(string)
278+
raise BunqException(error_message)
279+
280+
# Original behavior for strings with dots
255281
module_name_short, class_name = string.split(cls._DELIMITER_MODULE)
256282
members = inspect.getmembers(module_, inspect.ismodule)
257283

258284
for name, module_member in members:
259285
if module_name_short == name:
260-
return getattr(module_member, class_name)
286+
# Try original class name first
287+
if hasattr(module_member, class_name):
288+
return getattr(module_member, class_name)
289+
290+
# If "object_" module, try with Object suffix
291+
if "object_" in module_member.__name__:
292+
obj_name = class_name + "Object"
293+
if hasattr(module_member, obj_name):
294+
return getattr(module_member, obj_name)
295+
296+
# If "endpoint" module, try with ApiObject suffix
297+
if "endpoint" in module_member.__name__:
298+
api_name = class_name + "ApiObject"
299+
if hasattr(module_member, api_name):
300+
return getattr(module_member, api_name)
261301

262302
error_message = cls._ERROR_COULD_NOT_FIND_CLASS.format(string)
263-
264303
raise BunqException(error_message)
265304

266305
@classmethod

bunq/sdk/json/geolocation_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Type, Dict
22

33
from bunq.sdk.json import converter
4-
from bunq.sdk.model.generated.object_ import Geolocation
4+
from bunq.sdk.model.generated.object_ import GeolocationObject
55

66

77
class GeolocationAdapter(converter.JsonAdapter):
@@ -27,7 +27,7 @@ def deserialize(cls,
2727
raise NotImplementedError()
2828

2929
@classmethod
30-
def serialize(cls, geolocation: Geolocation) -> Dict:
30+
def serialize(cls, geolocation: GeolocationObject) -> Dict:
3131
obj = {}
3232

3333
cls.add_if_not_none(obj, cls._FIELD_LATITUDE, geolocation.latitude)

bunq/sdk/json/installation_adapter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ def serialize(cls, installation: Installation) -> List:
5353
{cls._FIELD_TOKEN: converter.serialize(installation.token)},
5454
{cls._FIELD_SERVER_PUBLIC_KEY: converter.serialize(installation.server_public_key)},
5555
]
56+
57+
@classmethod
58+
def can_serialize(cls) -> bool:
59+
return True

bunq/sdk/json/monetary_account_reference_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from typing import Type, Dict
22

33
from bunq.sdk.json import converter
4-
from bunq.sdk.model.generated.object_ import MonetaryAccountReference, LabelMonetaryAccount
4+
from bunq.sdk.model.generated.object_ import MonetaryAccountReference, LabelMonetaryAccountObject
55

66

77
class MonetaryAccountReferenceAdapter(converter.JsonAdapter):
88
@classmethod
99
def deserialize(cls,
1010
target_class: Type[MonetaryAccountReference],
1111
obj: Dict) -> MonetaryAccountReference:
12-
label_monetary_account = converter.deserialize(LabelMonetaryAccount, obj)
12+
label_monetary_account = converter.deserialize(LabelMonetaryAccountObject, obj)
1313

1414
return target_class.create_from_label_monetary_account(label_monetary_account)
1515

bunq/sdk/json/session_server_adapter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from bunq.sdk.model.core.id import Id
66
from bunq.sdk.model.core.session_server import SessionServer
77
from bunq.sdk.model.core.session_token import SessionToken
8-
from bunq.sdk.model.generated.endpoint import UserCompany, UserPerson, UserApiKey, UserPaymentServiceProvider
8+
from bunq.sdk.model.generated.endpoint import UserCompanyApiObject, UserPersonApiObject, UserApiKeyApiObject, UserPaymentServiceProviderApiObject
99

1010

1111
class SessionServerAdapter(converter.JsonAdapter):
@@ -65,25 +65,25 @@ def deserialize(cls,
6565
if cls._FIELD_USER_COMPANY in user_dict_wrapped:
6666
session_server.__dict__[cls._ATTRIBUTE_USER_COMPANY] = \
6767
converter.deserialize(
68-
UserCompany,
68+
UserCompanyApiObject,
6969
user_dict_wrapped[cls._FIELD_USER_COMPANY]
7070
)
7171
elif cls._FIELD_USER_PERSON in user_dict_wrapped:
7272
session_server.__dict__[cls._ATTRIBUTE_USER_PERSON] = \
7373
converter.deserialize(
74-
UserPerson,
74+
UserPersonApiObject,
7575
user_dict_wrapped[cls._FIELD_USER_PERSON]
7676
)
7777
elif cls._FIELD_USER_API_KEY in user_dict_wrapped:
7878
session_server.__dict__[cls._ATTRIBUTE_USER_API_KEY] = \
7979
converter.deserialize(
80-
UserApiKey,
80+
UserApiKeyApiObject,
8181
user_dict_wrapped[cls._FIELD_USER_API_KEY]
8282
)
8383
elif cls._FIELD_USER_PAYMENT_SERVER_PROVIDER in user_dict_wrapped:
8484
session_server.__dict__[cls._ATTRIBUTE_USER_PAYMENT_SERVER_PROVIDER] = \
8585
converter.deserialize(
86-
UserPaymentServiceProvider,
86+
UserPaymentServiceProviderApiObject,
8787
user_dict_wrapped[cls._FIELD_USER_PAYMENT_SERVER_PROVIDER]
8888
)
8989
else:

0 commit comments

Comments
 (0)