Skip to content

Commit 626b832

Browse files
committed
feature/python-sdk-refactor Merge Request fixes and code cleanup.
1 parent 2fb8645 commit 626b832

20 files changed

+100
-98
lines changed

bunq/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from bunq.sdk.context.api_environment_type import ApiEnvironmentType
2+
from bunq.sdk.context.installation_context import InstallationContext
13
from bunq.sdk.http.pagination import Pagination
24
from bunq.sdk.json import converter
3-
from bunq.sdk.model.core.anchored_object_interface import AnchoredObjectInterface
5+
from bunq.sdk.model.core.anchor_object_interface import AnchorObjectInterface
46

57

68
def initialize_converter():
@@ -28,18 +30,19 @@ def initialize_converter():
2830
from bunq.sdk.json.share_detail_adapter import ShareDetailAdapter
2931
from bunq.sdk.json.date_time_adapter import DateTimeAdapter
3032
from bunq.sdk.json.pagination_adapter import PaginationAdapter
33+
from bunq.sdk.json.anchor_object_adapter import AnchorObjectAdapter
3134

3235
converter.register_adapter(Installation, InstallationAdapter)
3336
converter.register_adapter(
3437
SessionServer,
3538
SessionServerAdapter
3639
)
3740
converter.register_adapter(
38-
api_context.InstallationContext,
41+
InstallationContext,
3942
InstallationContextAdapter
4043
)
4144
converter.register_adapter(
42-
api_context.ApiEnvironmentType,
45+
ApiEnvironmentType,
4346
ApiEnvironmentTypeAdapter
4447
)
4548
converter.register_adapter(float, FloatAdapter)
@@ -53,11 +56,10 @@ def initialize_converter():
5356
converter.register_adapter(Pagination, PaginationAdapter)
5457

5558
def register_anchor_adapter(class_to_register):
56-
if issubclass(class_to_register, AnchoredObjectInterface):
57-
from bunq.sdk.json.anchored_object_model_adapter import AnchoredObjectModelAdapter
59+
if issubclass(class_to_register, AnchorObjectInterface):
5860
converter.register_adapter(
5961
class_to_register,
60-
AnchoredObjectModelAdapter
62+
AnchorObjectAdapter
6163
)
6264

6365
def get_class(class_string_to_get):

bunq/sdk/context/api_context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ def _initialize_installation(self):
6969
"""
7070
:rtype: None
7171
"""
72+
from bunq.sdk.model.core.installation import Installation
7273

7374
private_key_client = security.generate_rsa_private_key()
74-
from bunq.sdk.model.core.installation import Installation
75+
7576
installation = Installation.create(
7677
self,
7778
security.public_key_to_string(private_key_client.publickey())
@@ -111,6 +112,7 @@ def _initialize_session(self):
111112
"""
112113

113114
from bunq.sdk.model.core.session_server import SessionServer
115+
114116
session_server = SessionServer.create(self).value
115117
token = session_server.token.token
116118
expiry_time = self._get_expiry_timestamp(session_server)

bunq/sdk/http/api_client.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ApiClient(object):
1313
"""
14-
:type _api_context: bunq.sdk.context.ApiContext
14+
:type _api_context: ApiContext
1515
"""
1616

1717
# Error constants
@@ -32,7 +32,7 @@ class ApiClient(object):
3232
]
3333

3434
# HTTPS type of proxy, the only used at bunq
35-
_FIELD_PROXY_HTTPS = 'https'
35+
FIELD_PROXY_HTTPS = 'https'
3636

3737
# Header constants
3838
HEADER_ATTACHMENT_DESCRIPTION = 'X-Bunq-Attachment-Description'
@@ -49,33 +49,33 @@ class ApiClient(object):
4949
HEADER_RESPONSE_ID_LOWER_CASED = 'x-bunq-client-response-id'
5050

5151
# Default header values
52-
_USER_AGENT_BUNQ = 'bunq-sdk-python/1.10.16'
52+
USER_AGENT_BUNQ = 'bunq-sdk-python/1.10.16'
5353
GEOLOCATION_ZERO = '0 0 0 0 NL'
5454
LANGUAGE_EN_US = 'en_US'
5555
REGION_NL_NL = 'nl_NL'
5656
CACHE_CONTROL_NONE = 'no-cache'
5757

5858
# Request method names
5959
METHOD_POST = 'POST'
60-
_METHOD_PUT = 'PUT'
61-
_METHOD_GET = 'GET'
62-
_METHOD_DELETE = 'DELETE'
60+
METHOD_PUT = 'PUT'
61+
METHOD_GET = 'GET'
62+
METHOD_DELETE = 'DELETE'
6363

6464
# Delimiter between path and params in URL
65-
_DELIMITER_URL_QUERY = '?'
65+
DELIMITER_URL_QUERY = '?'
6666

6767
# Status code for successful execution
6868
STATUS_CODE_OK = 200
6969

7070
# Fields for fetching errors
71-
_FIELD_ERROR = 'Error'
72-
_FIELD_ERROR_DESCRIPTION = 'error_description'
71+
FIELD_ERROR = 'Error'
72+
FIELD_ERROR_DESCRIPTION = 'error_description'
7373

7474
# Empty string
75-
_STRING_EMPTY = ''
75+
STRING_EMPTY = ''
7676

7777
# Empty bytes
78-
_BYTES_EMPTY = b''
78+
BYTES_EMPTY = b''
7979

8080
def __init__(self, api_context):
8181
self._api_context = api_context
@@ -97,8 +97,7 @@ def post(self, uri_relative, request_bytes, custom_headers):
9797
custom_headers
9898
)
9999

100-
def _request(self, method, uri_relative, request_bytes, params,
101-
custom_headers):
100+
def _request(self, method, uri_relative, request_bytes, params, custom_headers):
102101
"""
103102
:type method: str
104103
:type uri_relative: str
@@ -109,11 +108,11 @@ def _request(self, method, uri_relative, request_bytes, params,
109108
:return: BunqResponseRaw
110109
"""
111110

111+
from bunq.sdk.context.bunq_context import BunqContext
112+
112113
uri_relative_with_params = self._append_params_to_uri(uri_relative, params)
113114
if uri_relative not in self._URIS_NOT_REQUIRING_ACTIVE_SESSION:
114115
if self._api_context.ensure_session_active():
115-
from bunq.sdk.context.bunq_context import BunqContext
116-
117116
BunqContext.update_api_context(self._api_context)
118117

119118
all_headers = self._get_all_headers(
@@ -128,7 +127,7 @@ def _request(self, method, uri_relative, request_bytes, params,
128127
self._get_uri_full(uri_relative_with_params),
129128
data=request_bytes,
130129
headers=all_headers,
131-
proxies={self._FIELD_PROXY_HTTPS: self._api_context.proxy_url},
130+
proxies={self.FIELD_PROXY_HTTPS: self._api_context.proxy_url},
132131
)
133132

134133
self._assert_response_success(response)
@@ -153,7 +152,7 @@ def _append_params_to_uri(cls, uri, params):
153152
"""
154153

155154
if params:
156-
return uri + cls._DELIMITER_URL_QUERY + urlencode(params)
155+
return uri + cls.DELIMITER_URL_QUERY + urlencode(params)
157156

158157
return uri
159158

@@ -189,7 +188,7 @@ def _get_default_headers(cls):
189188
"""
190189

191190
return {
192-
cls.HEADER_USER_AGENT: cls._USER_AGENT_BUNQ,
191+
cls.HEADER_USER_AGENT: cls.USER_AGENT_BUNQ,
193192
cls.HEADER_REQUEST_ID: cls._generate_random_request_id(),
194193
cls.HEADER_GEOLOCATION: cls.GEOLOCATION_ZERO,
195194
cls.HEADER_LANGUAGE: cls.LANGUAGE_EN_US,
@@ -264,8 +263,8 @@ def _fetch_error_descriptions(self, error_dict):
264263

265264
error_descriptions = []
266265

267-
for error in error_dict[self._FIELD_ERROR]:
268-
description = error[self._FIELD_ERROR_DESCRIPTION]
266+
for error in error_dict[self.FIELD_ERROR]:
267+
description = error[self.FIELD_ERROR_DESCRIPTION]
269268
error_descriptions.append(description)
270269

271270
return error_descriptions
@@ -297,7 +296,7 @@ def put(self, uri_relative, request_bytes, custom_headers):
297296
"""
298297

299298
return self._request(
300-
self._METHOD_PUT,
299+
self.METHOD_PUT,
301300
uri_relative,
302301
request_bytes,
303302
{},
@@ -314,9 +313,9 @@ def get(self, uri_relative, params, custom_headers):
314313
"""
315314

316315
return self._request(
317-
self._METHOD_GET,
316+
self.METHOD_GET,
318317
uri_relative,
319-
self._BYTES_EMPTY,
318+
self.BYTES_EMPTY,
320319
params,
321320
custom_headers
322321
)
@@ -330,9 +329,9 @@ def delete(self, uri_relative, custom_headers):
330329
"""
331330

332331
return self._request(
333-
self._METHOD_DELETE,
332+
self.METHOD_DELETE,
334333
uri_relative,
335-
self._BYTES_EMPTY,
334+
self.BYTES_EMPTY,
336335
{},
337336
custom_headers
338337
)

bunq/sdk/json/anchored_object_model_adapter.py renamed to bunq/sdk/json/anchor_object_adapter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from bunq import AnchoredObjectInterface
1+
from bunq import AnchorObjectInterface
22
from bunq.sdk.exception.bunq_exception import BunqException
33
from bunq.sdk.json import converter
44
from bunq.sdk.model.generated import endpoint
55
from bunq.sdk.model.generated import object_
66

77

8-
class AnchoredObjectModelAdapter(converter.JsonAdapter):
9-
_ERROR_MODEL_NOT_FOUND = '{} is not in endpoint nor object.'
8+
class AnchorObjectAdapter(converter.JsonAdapter):
9+
__ERROR_MODEL_NOT_FOUND = '{} is not in endpoint nor object.'
1010

1111
__STRING_FORMAT_UNDERSCORE = '_'
1212

@@ -28,7 +28,7 @@ def deserialize(cls, cls_target, obj_raw):
2828

2929
if isinstance(
3030
model_,
31-
AnchoredObjectInterface
31+
AnchorObjectInterface
3232
) and model_.is_all_field_none():
3333
for field in model_.__dict__:
3434
object_class = cls._get_object_class(field)
@@ -67,4 +67,4 @@ def _get_object_class(cls, class_name):
6767
except AttributeError:
6868
pass
6969

70-
raise BunqException(cls._ERROR_MODEL_NOT_FOUND.format(class_name))
70+
raise BunqException(cls.__ERROR_MODEL_NOT_FOUND.format(class_name))

bunq/sdk/json/api_environment_type_adapter.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
from bunq.sdk.context import api_context
1+
from bunq.sdk.context.api_environment_type import ApiEnvironmentType
22
from bunq.sdk.json import converter
33

44

55
class ApiEnvironmentTypeAdapter(converter.JsonAdapter):
66
@classmethod
77
def deserialize(cls, target_class, name):
88
"""
9-
:type target_class: api_context.ApiEnvironmentType|type
9+
:type target_class: ApiEnvironmentType|type
1010
:type name: str
1111
1212
:rtype: context.ApiEnvironmentType
1313
"""
1414

15-
_ = target_class
16-
17-
return api_context.ApiEnvironmentType[name]
15+
return ApiEnvironmentType[name]
1816

1917
@classmethod
2018
def serialize(cls, api_environment_type):
2119
"""
22-
:type api_environment_type: api_context.ApiEnvironmentType
20+
:type api_environment_type: ApiEnvironmentType
2321
2422
:rtype: str
2523
"""

bunq/sdk/json/converter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import sys
55
import warnings
66

7-
# Indentation size we use for the serialized JSON output
87
from bunq.sdk.exception.bunq_exception import BunqException
98

9+
# Indentation size we use for the serialized JSON output
1010
_JSON_INDENT = 4
1111

1212

@@ -32,8 +32,7 @@ class JsonAdapter(object):
3232
_PREFIX_KEY_PROTECTED = '_'
3333

3434
# Constants to fetch param types from the docstrings
35-
_TEMPLATE_PATTERN_PARAM_TYPES = \
36-
':type (_?{}):[\s\n\r]+([\w.]+)(?:\[([\w.]+)\])?'
35+
_TEMPLATE_PATTERN_PARAM_TYPES = ':type (_?{}):[\s\n\r]+([\w.]+)(?:\[([\w.]+)\])?'
3736
_PATTERN_PARAM_NAME_TYPED_ANY = ':type (\w+):'
3837
_SUBMATCH_INDEX_NAME = 1
3938
_SUBMATCH_INDEX_TYPE_MAIN = 2

bunq/sdk/json/geolocation_adapter.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ def deserialize(cls, target_class, obj):
2626
:raise: NotImplementedError
2727
"""
2828

29-
_ = target_class, obj
30-
3129
raise NotImplementedError()
3230

3331
@classmethod

bunq/sdk/json/installation_context_adapter.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from bunq.sdk.context import api_context
21
from bunq.sdk.json import converter
32
from bunq.sdk.security import security
43

@@ -20,10 +19,10 @@ class InstallationContextAdapter(converter.JsonAdapter):
2019
@classmethod
2120
def deserialize(cls, target_class, obj):
2221
"""
23-
:type target_class: api_context.InstallationContext|type
22+
:type target_class: InstallationContext|type
2423
:type obj: dict
2524
26-
:rtype: context.InstallationContext
25+
:rtype: InstallationContext
2726
"""
2827

2928
installation_context = target_class.__new__(target_class)
@@ -48,7 +47,7 @@ def deserialize(cls, target_class, obj):
4847
@classmethod
4948
def serialize(cls, installation_context):
5049
"""
51-
:type installation_context: api_context.InstallationContext
50+
:type installation_context: InstallationContext
5251
5352
:rtype: dict
5453
"""

bunq/sdk/json/monetary_account_reference_adapter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ def deserialize(cls, target_class, obj):
1717
obj
1818
)
1919

20-
return target_class.create_from_label_monetary_account(
21-
label_monetary_account
22-
)
20+
return target_class.create_from_label_monetary_account(label_monetary_account)
2321

2422
@classmethod
2523
def serialize(cls, monetary_account_reference):

bunq/sdk/json/pagination_adapter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def deserialize(cls, target_class, pagination_response):
2525
:type target_class: Pagination|type
2626
:type pagination_response: dict
2727
28-
:rtype: client.Pagination
28+
:rtype: Pagination
2929
"""
3030

3131
pagination = Pagination()
@@ -103,6 +103,4 @@ def serialize(cls, pagination):
103103
:raise: NotImplementedError
104104
"""
105105

106-
_ = pagination
107-
108106
raise NotImplementedError()

0 commit comments

Comments
 (0)