Skip to content

Commit fb4714b

Browse files
committed
Fix Pagination usability [#20]
1 parent b64ea05 commit fb4714b

File tree

5 files changed

+106
-32
lines changed

5 files changed

+106
-32
lines changed

bunq/sdk/client.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ def pagination(self):
383383

384384
class Pagination(object):
385385
"""
386-
:type _older_id: int|None
387-
:type _newer_id: int|None
388-
:type _future_id: int|None
389-
:type _count: int|None
386+
:type older_id: int|None
387+
:type newer_id: int|None
388+
:type future_id: int|None
389+
:type count: int|None
390390
"""
391391

392392
# Error constants
@@ -399,10 +399,10 @@ class Pagination(object):
399399
URL_PARAM_COUNT = 'count'
400400

401401
def __init__(self):
402-
self._older_id = None
403-
self._newer_id = None
404-
self._future_id = None
405-
self._count = None
402+
self.older_id = None
403+
self.newer_id = None
404+
self.future_id = None
405+
self.count = None
406406

407407
@property
408408
def url_params_previous_page(self):
@@ -415,18 +415,29 @@ def url_params_previous_page(self):
415415
raise exception.BunqException(self._ERROR_NO_PREVIOUS_PAGE)
416416

417417
params = {
418-
self.URL_PARAM_OLDER_ID: str(self._older_id),
418+
self.URL_PARAM_OLDER_ID: str(self.older_id),
419419
}
420420
self._add_count_to_params_if_needed(params)
421421

422422
return params
423423

424+
@property
425+
def url_params_count_only(self):
426+
"""
427+
:rtype: dict[str, str]
428+
"""
429+
430+
params = {}
431+
self._add_count_to_params_if_needed(params)
432+
433+
return params
434+
424435
def has_previous_item(self):
425436
"""
426437
:rtype: bool
427438
"""
428439

429-
return self._older_id is not None
440+
return self.older_id is not None
430441

431442
def _add_count_to_params_if_needed(self, params):
432443
"""
@@ -435,8 +446,8 @@ def _add_count_to_params_if_needed(self, params):
435446
:rtype: None
436447
"""
437448

438-
if self._count is not None:
439-
params[self.URL_PARAM_COUNT] = str(self._count)
449+
if self.count is not None:
450+
params[self.URL_PARAM_COUNT] = str(self.count)
440451

441452
@property
442453
def _next_id(self):
@@ -445,16 +456,16 @@ def _next_id(self):
445456
"""
446457

447458
if self.has_next_item_assured():
448-
return self._newer_id
459+
return self.newer_id
449460

450-
return self._future_id
461+
return self.future_id
451462

452463
def has_next_item_assured(self):
453464
"""
454465
:rtype: bool
455466
"""
456467

457-
return self._newer_id is not None
468+
return self.newer_id is not None
458469

459470
@property
460471
def url_params_next_page(self):

bunq/sdk/json/adapters.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ def deserialize(cls, target_class, pagination_response):
468468
:rtype: client.Pagination
469469
"""
470470

471-
pagination = client.Pagination.__new__(client.Pagination)
472-
pagination.__dict__ = cls.parse_pagination_dict(pagination_response)
471+
pagination = client.Pagination()
472+
pagination.__dict__.update(
473+
cls.parse_pagination_dict(pagination_response)
474+
)
473475

474476
return pagination
475477

bunq/sdk/json/registry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22

3+
from bunq.sdk import client
34
from bunq.sdk import context
45
from bunq.sdk import model
56
from bunq.sdk.json import adapters
@@ -36,3 +37,4 @@ def initialize():
3637
)
3738
converter.register_adapter(object_.ShareDetail, adapters.ShareDetailAdapter)
3839
converter.register_adapter(datetime.datetime, adapters.DateTimeAdapter)
40+
converter.register_adapter(client.Pagination, adapters.PaginationAdapter)

bunq/sdk/model/model.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from bunq.sdk import client
22
from bunq.sdk import context
3-
from bunq.sdk.client import BunqResponse
43
from bunq.sdk.json import converter
54

65

76
class BunqModel(object):
87
# Field constants
98
_FIELD_RESPONSE = 'Response'
9+
_FIELD_PAGINATION = 'Pagination'
1010
_FIELD_ID = 'Id'
1111
_FIELD_UUID = 'Uuid'
1212

@@ -32,15 +32,15 @@ def _from_json_array_nested(cls, response_raw):
3232
obj = converter.json_to_class(dict, json)
3333
value = converter.deserialize(cls, obj[cls._FIELD_RESPONSE])
3434

35-
return BunqResponse(value, response_raw.headers)
35+
return client.BunqResponse(value, response_raw.headers)
3636

3737
@classmethod
3838
def _from_json(cls, response_raw, wrapper=None):
3939
"""
4040
:type response_raw: client.BunqResponseRaw
4141
:type wrapper: str|None
4242
43-
:rtype: BunqResponse[cls]
43+
:rtype: client.BunqResponse[cls]
4444
"""
4545

4646
json = response_raw.body_bytes.decode()
@@ -50,7 +50,7 @@ def _from_json(cls, response_raw, wrapper=None):
5050
cls._unwrap_response_single(obj, wrapper)
5151
)
5252

53-
return BunqResponse(value, response_raw.headers)
53+
return client.BunqResponse(value, response_raw.headers)
5454

5555
@classmethod
5656
def _unwrap_response_single(cls, obj, wrapper=None):
@@ -71,7 +71,7 @@ def _process_for_id(cls, response_raw):
7171
"""
7272
:type response_raw: client.BunqResponseRaw
7373
74-
:rtype: BunqResponse[int]
74+
:rtype: client.BunqResponse[int]
7575
"""
7676

7777
json = response_raw.body_bytes.decode()
@@ -81,14 +81,14 @@ def _process_for_id(cls, response_raw):
8181
cls._unwrap_response_single(obj, cls._FIELD_ID)
8282
)
8383

84-
return BunqResponse(id_.id_, response_raw.headers)
84+
return client.BunqResponse(id_.id_, response_raw.headers)
8585

8686
@classmethod
8787
def _process_for_uuid(cls, response_raw):
8888
"""
8989
:type response_raw: client.BunqResponseRaw
9090
91-
:rtype: BunqResponse[str]
91+
:rtype: client.BunqResponse[str]
9292
"""
9393

9494
json = response_raw.body_bytes.decode()
@@ -98,15 +98,15 @@ def _process_for_uuid(cls, response_raw):
9898
cls._unwrap_response_single(obj, cls._FIELD_UUID)
9999
)
100100

101-
return BunqResponse(uuid.uuid, response_raw.headers)
101+
return client.BunqResponse(uuid.uuid, response_raw.headers)
102102

103103
@classmethod
104104
def _from_json_list(cls, response_raw, wrapper=None):
105105
"""
106106
:type response_raw: client.BunqResponseRaw
107107
:type wrapper: str|None
108108
109-
:rtype: BunqResponse[list[cls]]
109+
:rtype: client.BunqResponse[list[cls]]
110110
"""
111111

112112
json = response_raw.body_bytes.decode()
@@ -119,7 +119,11 @@ def _from_json_list(cls, response_raw, wrapper=None):
119119
item_deserialized = converter.deserialize(cls, item_unwrapped)
120120
array_deserialized.append(item_deserialized)
121121

122-
return BunqResponse(array_deserialized, response_raw.headers)
122+
pagination = converter.deserialize(client.Pagination,
123+
obj[cls._FIELD_PAGINATION])
124+
125+
return client.BunqResponse(array_deserialized, response_raw.headers,
126+
pagination)
123127

124128

125129
class Id(BunqModel):
@@ -158,12 +162,42 @@ def uuid(self):
158162

159163
class SessionToken(BunqModel):
160164
"""
165+
:type _id_: int
166+
:type _created: str
167+
:type _updated: str
161168
:type _token: str
162169
"""
163170

164171
def __init__(self):
172+
self._id_ = None
173+
self._created = None
174+
self._updated = None
165175
self._token = None
166176

177+
@property
178+
def id_(self):
179+
"""
180+
:rtype: int
181+
"""
182+
183+
return self._id_
184+
185+
@property
186+
def created(self):
187+
"""
188+
:rtype: str
189+
"""
190+
191+
return self._created
192+
193+
@property
194+
def updated(self):
195+
"""
196+
:rtype: str
197+
"""
198+
199+
return self._updated
200+
167201
@property
168202
def token(self):
169203
"""
@@ -238,7 +272,7 @@ def create(cls, api_context, public_key_string):
238272
:type api_context: context.ApiContext
239273
:type public_key_string: str
240274
241-
:rtype: BunqResponse[Installation]
275+
:rtype: client.BunqResponse[Installation]
242276
"""
243277

244278
api_client = client.ApiClient(api_context)
@@ -321,7 +355,7 @@ def create(cls, api_context):
321355
"""
322356
:type api_context: context.ApiContext
323357
324-
:rtype: BunqResponse[SessionServer]
358+
:rtype: client.BunqResponse[SessionServer]
325359
"""
326360

327361
api_client = client.ApiClient(api_context)

examples/payment_list_example.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
#!/usr/bin/env python3
22

3+
from bunq.sdk import client
34
from bunq.sdk import context
45
from bunq.sdk.model import generated
56

7+
# Console messages
8+
_MESSAGE_LATEST_PAGE_IDS = 'Latest page IDs:'
9+
_MESSAGE_SECOND_LATEST_PAGE_IDS = 'Second latest page IDs:'
10+
11+
# Size of page of payments to list
12+
_PAGE_SIZE = 3
13+
614
_USER_ITEM_ID = 0 # Put your user ID here
715
_MONETARY_ACCOUNT_ITEM_ID = 0 # Put your monetary account ID here
816

917

1018
def run():
1119
api_context = context.ApiContext.restore()
12-
payments = generated.Payment.list(
20+
pagination = client.Pagination()
21+
pagination.count = _PAGE_SIZE
22+
payments_response = generated.Payment.list(
23+
api_context,
24+
_USER_ITEM_ID,
25+
_MONETARY_ACCOUNT_ITEM_ID,
26+
pagination.url_params_count_only
27+
)
28+
29+
print(_MESSAGE_LATEST_PAGE_IDS)
30+
31+
for payment in payments_response.value:
32+
print(payment.id_)
33+
34+
payments_response_previous = generated.Payment.list(
1335
api_context,
1436
_USER_ITEM_ID,
1537
_MONETARY_ACCOUNT_ITEM_ID,
16-
).value
38+
payments_response.pagination.url_params_previous_page
39+
)
40+
41+
print(_MESSAGE_SECOND_LATEST_PAGE_IDS)
1742

18-
for payment in payments:
43+
for payment in payments_response_previous.value:
1944
print(payment.id_)

0 commit comments

Comments
 (0)