bunq / sdk_python

Python SDK for bunq API
MIT License
106 stars 25 forks source link

Get list of ALL payments #164

Closed Cubicle13 closed 1 year ago

Cubicle13 commented 1 year ago

Steps to reproduce:

def get_all_payment(count: int = 2) -> list[Payment]:
        pagination = Pagination()
        pagination.count = count
        paylist:list[Payment] = Payment.list(params=pagination.url_params_count_only).value
        while(pagination.has_previous_page):
                paylist.extend(Payment.list(params=pagination.url_params_previous_page).value)
        return paylist

a = get_all_payment()
a

What should happen:

This should make a list of all transactions (in this case 2 by two)

What happens:

It says "there is no previous page." or "there is no next page." when I try doing this with url_params_next page.

Traceback

BunqException Traceback (most recent call last) Cell In[123], line 20 17 paylist.extend(Payment.list(params=pagination.url_params_previous_page).value) 18 return paylist ---> 20 a = get_all_payment() 21 a

Cell In[123], line 17, in get_all_payment(count) 15 paylist:list[Payment] = Payment.list(params=pagination.url_params_count_only).value 16 while(pagination.has_previous_page): ---> 17 paylist.extend(Payment.list(params=pagination.url_params_previous_page).value) 18 return paylist

File c:\Users...\AppData\Local\Programs\Python\Python311\Lib\site-packages\bunq\sdk\http\pagination.py:31, in Pagination.url_params_previous_page(self) 29 @property 30 def url_params_previous_page(self) -> Dict[str, str]: ---> 31 self.assert_has_previous_page() 33 params = {self.PARAM_OLDER_ID: str(self.older_id)} 34 self._add_count_to_params_if_needed(params)

File c:\Users...\AppData\Local\Programs\Python\Python311\Lib\site-packages\bunq\sdk\http\pagination.py:45, in Pagination.assert_has_previous_page(self) 39 """ 40 41 :raise: BunqException 42 """ 44 if not self.has_previous_page(): ---> 45 raise BunqException(self._ERROR_NO_PREVIOUS_PAGE)

BunqException: Could not generate previous page URL params: there is no previous page.

SDK version and environment

  • Tested on 1.14.1
  • [ ] Sandbox
  • [x] Production

Extra info:

Looked everywhere on the Bunq Developer forum, other bunq SDKs, other API's.

Cubicle13 commented 1 year ago

Had some help, got fixed. Saw a lot of users wanted this as well; posting to help others. Feel free to comment.

#Gets all payments after the last retrieved id (last_id)
def get_all_payment(last_id:int = 0) -> list[Payment]:
        paylist:list[Payment] = []
        newer_id = last_id
        while(True):
                pagination = Pagination()
                pagination.count = 3        
                pagination.newer_id = newer_id
                pl:list[Payment] = Payment.list(params=pagination.url_params_next_page).value

                if (len(pl) == 0):
                        break

                pl.extend(paylist)
                paylist = pl
                newer_id = paylist[0].id_

                time.sleep(1)

        return paylist

a = get_all_payment()
a