Skip to content

Commit c6b727d

Browse files
committed
Add pagination object [#20]
1 parent 0a9e998 commit c6b727d

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

bunq/sdk/client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,18 @@ def headers(self):
338338
"""
339339

340340
return self._headers
341+
342+
343+
class Pagination(object):
344+
"""
345+
:type _older_id: int|None
346+
:type _newer_id: int|None
347+
:type _future_id: int|None
348+
:type _count: int|None
349+
"""
350+
351+
def __init__(self):
352+
self._older_id = None
353+
self._newer_id = None
354+
self._future_id = None
355+
self._count = None

bunq/sdk/json/adapters.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import datetime
2+
import urllib.parse as urlparse
23

4+
from bunq.sdk import client
35
from bunq.sdk import context
46
from bunq.sdk import model
57
from bunq.sdk import security
@@ -440,3 +442,101 @@ def serialize(cls, timestamp):
440442
"""
441443

442444
return timestamp.strftime(cls._FORMAT_TIMESTAMP)
445+
446+
447+
class PaginationAdapter(converter.JsonAdapter):
448+
# Raw pagination response field constants.
449+
_FIELD_FUTURE_URL = 'future_url'
450+
_FIELD_NEWER_URL = 'newer_url'
451+
_FIELD_OLDER_URL = 'older_url'
452+
453+
# Processed pagination field constants.
454+
_FIELD_OLDER_ID = 'older_id'
455+
_FIELD_NEWER_ID = 'newer_id'
456+
_FIELD_FUTURE_ID = 'future_id'
457+
_FIELD_COUNT = 'count'
458+
459+
# Very first index in an array.
460+
_INDEX_FIRST = 0
461+
462+
@classmethod
463+
def deserialize(cls, target_class, pagination_response):
464+
"""
465+
:type target_class: client.Pagination|type
466+
:type pagination_response: dict
467+
468+
:rtype: client.Pagination
469+
"""
470+
471+
pagination = client.Pagination.__new__(client.Pagination)
472+
pagination.__dict__ = cls.parse_pagination_dict(pagination_response)
473+
474+
return pagination
475+
476+
@classmethod
477+
def parse_pagination_dict(cls, response_obj):
478+
"""
479+
:type response_obj: dict
480+
481+
:rtype: dict
482+
"""
483+
484+
pagination_dict = {}
485+
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)
501+
502+
return pagination_dict
503+
504+
@classmethod
505+
def update_dict_id_field_from_response_field(cls, dict_,
506+
dict_id_field,
507+
response_obj,
508+
response_field,
509+
response_param):
510+
"""
511+
:type dict_: dict
512+
:type dict_id_field: str
513+
:type response_obj: dict
514+
:type response_field: str
515+
:type response_param: str
516+
"""
517+
518+
url = response_obj[response_field]
519+
520+
if url is not None:
521+
url_parsed = urlparse.urlparse(url)
522+
parameters = urlparse.parse_qs(url_parsed.query)
523+
dict_[dict_id_field] = int(
524+
parameters[response_param][cls._INDEX_FIRST]
525+
)
526+
527+
if cls._FIELD_COUNT in parameters:
528+
dict_[cls._FIELD_COUNT] = int(
529+
parameters[cls._FIELD_COUNT][cls._INDEX_FIRST]
530+
)
531+
532+
@classmethod
533+
def serialize(cls, pagination):
534+
"""
535+
:type pagination: client.Pagination
536+
537+
:raise: NotImplementedError
538+
"""
539+
540+
_ = pagination
541+
542+
raise NotImplementedError()

0 commit comments

Comments
 (0)