Utility class that allows batching requests to the server
| 501 | |
| 502 | |
| 503 | class Pagination(ApiComponent): |
| 504 | """ Utility class that allows batching requests to the server """ |
| 505 | |
| 506 | def __init__(self, *, parent=None, data=None, constructor=None, |
| 507 | next_link=None, limit=None, **kwargs): |
| 508 | """ Returns an iterator that returns data until it's exhausted. |
| 509 | Then will request more data (same amount as the original request) |
| 510 | to the server until this data is exhausted as well. |
| 511 | Stops when no more data exists or limit is reached. |
| 512 | |
| 513 | :param parent: the parent class. Must implement attributes: |
| 514 | con, api_version, main_resource |
| 515 | :param data: the start data to be return |
| 516 | :param constructor: the data constructor for the next batch. |
| 517 | It can be a function. |
| 518 | :param str next_link: the link to request more data to |
| 519 | :param int limit: when to stop retrieving more data |
| 520 | :param kwargs: any extra key-word arguments to pass to the |
| 521 | construtctor. |
| 522 | """ |
| 523 | if parent is None: |
| 524 | raise ValueError('Parent must be another Api Component') |
| 525 | |
| 526 | super().__init__(protocol=parent.protocol, |
| 527 | main_resource=parent.main_resource) |
| 528 | |
| 529 | self.parent = parent |
| 530 | self.con = parent.con |
| 531 | self.constructor = constructor |
| 532 | self.next_link = next_link |
| 533 | self.limit = limit |
| 534 | self.data = data = list(data) if data else [] |
| 535 | |
| 536 | data_count = len(data) |
| 537 | if limit and limit < data_count: |
| 538 | self.data_count = limit |
| 539 | self.total_count = limit |
| 540 | else: |
| 541 | self.data_count = data_count |
| 542 | self.total_count = data_count |
| 543 | self.state = 0 |
| 544 | self.extra_args = kwargs |
| 545 | |
| 546 | def __str__(self): |
| 547 | return self.__repr__() |
| 548 | |
| 549 | def __repr__(self): |
| 550 | if callable(self.constructor) and not isinstance( |
| 551 | self.constructor, type): |
| 552 | return 'Pagination Iterator' |
| 553 | else: |
| 554 | return "'{}' Iterator".format( |
| 555 | self.constructor.__name__ if self.constructor else 'Unknown') |
| 556 | |
| 557 | def __bool__(self): |
| 558 | return bool(self.data) or bool(self.next_link) |
| 559 | |
| 560 | def __iter__(self): |
no outgoing calls
no test coverage detected