Skip to content

Commit 252fbc3

Browse files
committed
cleanup Pagination and payment list example [#20]
1 parent ca26141 commit 252fbc3

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

bunq/sdk/client.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ 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 = '?'
55+
# Delimiter between path and params in URL
56+
_DELIMITER_URL_QUERY = '?'
5757

5858
# Status code for successful execution
5959
_STATUS_CODE_OK = 200
@@ -140,7 +140,7 @@ def _append_params_to_uri(cls, uri, params):
140140
"""
141141

142142
if params:
143-
return uri + cls._DELIMITER_PATH_AND_PARAMS + urlencode(params)
143+
return uri + cls._DELIMITER_URL_QUERY + urlencode(params)
144144

145145
return uri
146146

@@ -343,13 +343,14 @@ class BunqResponse(object):
343343
"""
344344
:type _value: T
345345
:type _headers: dict[str, str]
346-
:type _pagination: Pagination
346+
:type _pagination: Pagination|None
347347
"""
348348

349349
def __init__(self, value, headers, pagination=None):
350350
"""
351351
:type value: T
352352
:type headers: dict[str, str]
353+
:type pagination Pagination|None
353354
"""
354355

355356
self._value = value
@@ -394,9 +395,9 @@ class Pagination(object):
394395
'there is no previous page.'
395396

396397
# URL Param constants
397-
URL_PARAM_OLDER_ID = 'older_id'
398-
URL_PARAM_NEWER_ID = 'newer_id'
399-
URL_PARAM_COUNT = 'count'
398+
PARAM_OLDER_ID = 'older_id'
399+
PARAM_NEWER_ID = 'newer_id'
400+
PARAM_COUNT = 'count'
400401

401402
def __init__(self):
402403
self.older_id = None
@@ -414,9 +415,7 @@ def url_params_previous_page(self):
414415
if not self.has_previous_item():
415416
raise exception.BunqException(self._ERROR_NO_PREVIOUS_PAGE)
416417

417-
params = {
418-
self.URL_PARAM_OLDER_ID: str(self.older_id),
419-
}
418+
params = {self.PARAM_OLDER_ID: str(self.older_id)}
420419
self._add_count_to_params_if_needed(params)
421420

422421
return params
@@ -447,7 +446,7 @@ def _add_count_to_params_if_needed(self, params):
447446
"""
448447

449448
if self.count is not None:
450-
params[self.URL_PARAM_COUNT] = str(self.count)
449+
params[self.PARAM_COUNT] = str(self.count)
451450

452451
@property
453452
def _next_id(self):
@@ -473,9 +472,7 @@ def url_params_next_page(self):
473472
:rtype: dict[str, str]
474473
"""
475474

476-
params = {
477-
self.URL_PARAM_NEWER_ID: str(self._next_id),
478-
}
475+
params = {self.PARAM_NEWER_ID: str(self._next_id)}
479476
self._add_count_to_params_if_needed(params)
480477

481478
return params

bunq/sdk/json/adapters.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,21 +490,21 @@ def parse_pagination_dict(cls, response_obj):
490490
cls._FIELD_OLDER_ID,
491491
response_obj,
492492
cls._FIELD_OLDER_URL,
493-
client.Pagination.URL_PARAM_OLDER_ID
493+
client.Pagination.PARAM_OLDER_ID
494494
)
495495
cls.update_dict_id_field_from_response_field(
496496
pagination_dict,
497497
cls._FIELD_NEWER_ID,
498498
response_obj,
499499
cls._FIELD_NEWER_URL,
500-
client.Pagination.URL_PARAM_NEWER_ID
500+
client.Pagination.PARAM_NEWER_ID
501501
)
502502
cls.update_dict_id_field_from_response_field(
503503
pagination_dict,
504504
cls._FIELD_FUTURE_ID,
505505
response_obj,
506506
cls._FIELD_FUTURE_URL,
507-
client.Pagination.URL_PARAM_NEWER_ID
507+
client.Pagination.PARAM_NEWER_ID
508508
)
509509

510510
return pagination_dict
@@ -530,10 +530,9 @@ def update_dict_id_field_from_response_field(cls, dict_, dict_id_field,
530530
parameters[response_param][cls._INDEX_FIRST]
531531
)
532532

533-
if cls._FIELD_COUNT in parameters:
533+
if cls._FIELD_COUNT in parameters and cls._FIELD_COUNT not in dict_:
534534
dict_[cls._FIELD_COUNT] = int(
535-
parameters[client.Pagination.URL_PARAM_COUNT][
536-
cls._INDEX_FIRST]
535+
parameters[client.Pagination.PARAM_COUNT][cls._INDEX_FIRST]
537536
)
538537

539538
@classmethod

bunq/sdk/model/generated/endpoint.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9195,7 +9195,6 @@ class UserCompany(model.BunqModel):
91959195
FIELD_NAME = "name"
91969196
FIELD_PUBLIC_NICK_NAME = "public_nick_name"
91979197
FIELD_AVATAR_UUID = "avatar_uuid"
9198-
FIELD_ADDRESS = "address"
91999198
FIELD_ADDRESS_MAIN = "address_main"
92009199
FIELD_ADDRESS_POSTAL = "address_postal"
92019200
FIELD_LANGUAGE = "language"
@@ -10035,7 +10034,6 @@ class UserPerson(model.BunqModel):
1003510034
FIELD_MIDDLE_NAME = "middle_name"
1003610035
FIELD_LAST_NAME = "last_name"
1003710036
FIELD_PUBLIC_NICK_NAME = "public_nick_name"
10038-
FIELD_ADDRESS = "address"
1003910037
FIELD_ADDRESS_MAIN = "address_main"
1004010038
FIELD_ADDRESS_POSTAL = "address_postal"
1004110039
FIELD_AVATAR_UUID = "avatar_uuid"
@@ -10532,7 +10530,6 @@ class UserLight(model.BunqModel):
1053210530
FIELD_LAST_NAME = "last_name"
1053310531
FIELD_PUBLIC_NICK_NAME = "public_nick_name"
1053410532
FIELD_COUNTER_BANK_IBAN = "counter_bank_iban"
10535-
FIELD_ADDRESS = "address"
1053610533
FIELD_ADDRESS_MAIN = "address_main"
1053710534
FIELD_ADDRESS_POSTAL = "address_postal"
1053810535
FIELD_AVATAR_UUID = "avatar_uuid"

examples/payment_list_example.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
# Console messages
88
_MESSAGE_LATEST_PAGE_IDS = 'Latest page IDs:'
99
_MESSAGE_SECOND_LATEST_PAGE_IDS = 'Second latest page IDs:'
10+
_MESSAGE_NO_PRIOR_PAYMENTS_FOUND = 'No prior payments found!'
1011

1112
# Size of page of payments to list
1213
_PAGE_SIZE = 3
1314

14-
_USER_ITEM_ID = 0 # Put your user ID here
15-
_MONETARY_ACCOUNT_ITEM_ID = 0 # Put your monetary account ID here
15+
_USER_ITEM_ID = 1969 # Put your user ID here
16+
_MONETARY_ACCOUNT_ITEM_ID = 1988 # Put your monetary account ID here
1617

1718

1819
def run():
@@ -31,14 +32,16 @@ def run():
3132
for payment in payments_response.value:
3233
print(payment.id_)
3334

34-
payments_response_previous = generated.Payment.list(
35-
api_context,
36-
_USER_ITEM_ID,
37-
_MONETARY_ACCOUNT_ITEM_ID,
38-
payments_response.pagination.url_params_previous_page
39-
)
40-
41-
print(_MESSAGE_SECOND_LATEST_PAGE_IDS)
42-
43-
for payment in payments_response_previous.value:
44-
print(payment.id_)
35+
if payments_response.pagination.has_previous_item():
36+
print(_MESSAGE_SECOND_LATEST_PAGE_IDS)
37+
payments_response_previous = generated.Payment.list(
38+
api_context,
39+
_USER_ITEM_ID,
40+
_MONETARY_ACCOUNT_ITEM_ID,
41+
payments_response.pagination.url_params_previous_page
42+
)
43+
44+
for payment in payments_response_previous.value:
45+
print(payment.id_)
46+
else:
47+
print(_MESSAGE_NO_PRIOR_PAYMENTS_FOUND)

0 commit comments

Comments
 (0)