Skip to content
This repository was archived by the owner on Jan 2, 2020. It is now read-only.

Commit 959c3ae

Browse files
TomAFrenchMuyiwa Olu
authored andcommitted
Pagination (#24)
1 parent 13f9af8 commit 959c3ae

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

monzo/monzo.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"""
66

77
from monzo.auth import MonzoOAuth2Client
8+
from datetime import datetime
9+
from functools import partial
10+
811
import string
912
import random
1013

@@ -75,15 +78,37 @@ def get_first_account(self):
7578
raise LookupError('There are no accounts associated with this user.')
7679
return accounts['accounts'][0]
7780

78-
def get_transactions(self, account_id):
81+
def get_transactions(self, account_id, before=None, since=None, limit=None):
7982
"""Get all transactions of a given account. (https://monzo.com/docs/#list-transactions)
8083
8184
:param account_id: The unique identifier for the account which the transactions belong to.
85+
:param before: A datetime representing the time of the earliest transaction to return (Can't take transaction id as input)
86+
:param since: A datetime representing the time of the earliest transaction to return. (Can also take a transaction id)
87+
:param limit: The maximum number of transactions to return (Max = 100)
8288
:rtype: A collection of transaction objects for specific user.
8389
"""
90+
if isinstance(before, datetime):
91+
before = before.isoformat() + 'Z'
92+
if isinstance(since, datetime):
93+
since = since.isoformat() + 'Z'
8494
url = "{0}/transactions".format(self.API_URL)
85-
params = {'expand[]': 'merchant', 'account_id': account_id}
95+
params = {
96+
'expand[]': 'merchant',
97+
'account_id': account_id,
98+
'before': before,
99+
'since': since,
100+
'limit': limit,
101+
}
86102
response = self.oauth_session.make_request(url, params=params)
103+
if any([before,since,limit]):
104+
last_transaction_id = response['transactions'][-1]['id']
105+
next_page = partial(self.get_transactions,
106+
account_id,
107+
before=before,
108+
since = last_transaction_id,
109+
limit = limit)
110+
response.update({'next_page':next_page})
111+
87112
return response
88113

89114
def get_transaction(self, transaction_id):

0 commit comments

Comments
 (0)