|
1 | 1 | import datetime
|
| 2 | +import urllib.parse as urlparse |
2 | 3 |
|
| 4 | +from bunq.sdk import client |
3 | 5 | from bunq.sdk import context
|
4 | 6 | from bunq.sdk import model
|
5 | 7 | from bunq.sdk import security
|
@@ -440,3 +442,101 @@ def serialize(cls, timestamp):
|
440 | 442 | """
|
441 | 443 |
|
442 | 444 | 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