Skip to content

Commit c125525

Browse files
committed
refactor pagination [#20]
1 parent 5d237c4 commit c125525

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

bunq/sdk/client.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class ApiClient(object):
5252
_METHOD_GET = 'GET'
5353
_METHOD_DELETE = 'DELETE'
5454

55+
# Delimiter between path and params in URI
56+
_DELIMITER_PATH_AND_PARAMS = '?'
57+
5558
# Status code for successful execution
5659
_STATUS_CODE_OK = 200
5760

@@ -97,11 +100,8 @@ def _request(self, method, uri_relative, request_bytes, params,
97100
:return: BunqResponseRaw
98101
"""
99102

100-
if params:
101-
uri_relative_with_params = uri_relative + '?' + urlencode(params)
102-
else:
103-
uri_relative_with_params = uri_relative
104-
103+
uri_relative_with_params = self._append_params_to_uri(uri_relative,
104+
params)
105105
self._api_context.ensure_session_active()
106106
all_headers = self._get_all_headers(
107107
method,
@@ -130,6 +130,20 @@ def _request(self, method, uri_relative, request_bytes, params,
130130

131131
return self._create_bunq_response_raw(response)
132132

133+
@classmethod
134+
def _append_params_to_uri(cls, uri, params):
135+
"""
136+
:type uri: str
137+
:type params: dict[str, str]
138+
139+
:rtype: str
140+
"""
141+
142+
if params:
143+
return uri + cls._DELIMITER_PATH_AND_PARAMS + urlencode(params)
144+
145+
return uri
146+
133147
def _get_all_headers(self, method, endpoint, request_bytes, custom_headers):
134148
"""
135149
:type method: str
@@ -376,9 +390,9 @@ class Pagination(object):
376390
"""
377391

378392
# Field constants
379-
_FIELD_OLDER_ID = 'older_id'
380-
_FIELD_NEWER_ID = 'newer_id'
381-
_FIELD_COUNT = 'count'
393+
FIELD_OLDER_ID = 'older_id'
394+
FIELD_NEWER_ID = 'newer_id'
395+
FIELD_COUNT = 'count'
382396

383397
def __init__(self):
384398
self._older_id = None
@@ -393,7 +407,7 @@ def url_params_previous(self):
393407
"""
394408

395409
params = {
396-
self._FIELD_OLDER_ID: str(self._older_id),
410+
self.FIELD_OLDER_ID: str(self._older_id),
397411
}
398412
self._add_count_to_params_if_needed(params)
399413

@@ -407,7 +421,7 @@ def _add_count_to_params_if_needed(self, params):
407421
"""
408422

409423
if self._count is not None:
410-
params[self._FIELD_COUNT] = str(self._count)
424+
params[self.FIELD_COUNT] = str(self._count)
411425

412426
@property
413427
def _next_id(self):
@@ -434,7 +448,7 @@ def url_params_next(self):
434448
"""
435449

436450
params = {
437-
self._FIELD_NEWER_ID: str(self._next_id),
451+
self.FIELD_NEWER_ID: str(self._next_id),
438452
}
439453
self._add_count_to_params_if_needed(params)
440454

bunq/sdk/json/adapters.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -483,29 +483,33 @@ def parse_pagination_dict(cls, response_obj):
483483

484484
pagination_dict = {}
485485

486-
cls.update_dict_id_field_from_response_field(pagination_dict,
487-
cls._FIELD_OLDER_ID,
488-
response_obj,
489-
cls._FIELD_OLDER_URL,
490-
cls._FIELD_OLDER_ID)
491-
cls.update_dict_id_field_from_response_field(pagination_dict,
492-
cls._FIELD_NEWER_ID,
493-
response_obj,
494-
cls._FIELD_NEWER_URL,
495-
cls._FIELD_NEWER_ID)
496-
cls.update_dict_id_field_from_response_field(pagination_dict,
497-
cls._FIELD_FUTURE_ID,
498-
response_obj,
499-
cls._FIELD_FUTURE_URL,
500-
cls._FIELD_NEWER_ID)
486+
cls.update_dict_id_field_from_response_field(
487+
pagination_dict,
488+
cls._FIELD_OLDER_ID,
489+
response_obj,
490+
cls._FIELD_OLDER_URL,
491+
client.Pagination.FIELD_OLDER_ID
492+
)
493+
cls.update_dict_id_field_from_response_field(
494+
pagination_dict,
495+
cls._FIELD_NEWER_ID,
496+
response_obj,
497+
cls._FIELD_NEWER_URL,
498+
client.Pagination.FIELD_NEWER_ID
499+
)
500+
cls.update_dict_id_field_from_response_field(
501+
pagination_dict,
502+
cls._FIELD_FUTURE_ID,
503+
response_obj,
504+
cls._FIELD_FUTURE_URL,
505+
client.Pagination.FIELD_NEWER_ID
506+
)
501507

502508
return pagination_dict
503509

504510
@classmethod
505-
def update_dict_id_field_from_response_field(cls, dict_,
506-
dict_id_field,
507-
response_obj,
508-
response_field,
511+
def update_dict_id_field_from_response_field(cls, dict_, dict_id_field,
512+
response_obj, response_field,
509513
response_param):
510514
"""
511515
:type dict_: dict
@@ -526,7 +530,7 @@ def update_dict_id_field_from_response_field(cls, dict_,
526530

527531
if cls._FIELD_COUNT in parameters:
528532
dict_[cls._FIELD_COUNT] = int(
529-
parameters[cls._FIELD_COUNT][cls._INDEX_FIRST]
533+
parameters[client.Pagination.FIELD_COUNT][cls._INDEX_FIRST]
530534
)
531535

532536
@classmethod

0 commit comments

Comments
 (0)