|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | from monzo.auth import MonzoOAuth2Client |
| 8 | +from datetime import datetime |
| 9 | +from functools import partial |
| 10 | + |
8 | 11 | import string |
9 | 12 | import random |
10 | 13 |
|
@@ -75,15 +78,37 @@ def get_first_account(self): |
75 | 78 | raise LookupError('There are no accounts associated with this user.') |
76 | 79 | return accounts['accounts'][0] |
77 | 80 |
|
78 | | - def get_transactions(self, account_id): |
| 81 | + def get_transactions(self, account_id, before=None, since=None, limit=None): |
79 | 82 | """Get all transactions of a given account. (https://monzo.com/docs/#list-transactions) |
80 | 83 |
|
81 | 84 | :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) |
82 | 88 | :rtype: A collection of transaction objects for specific user. |
83 | 89 | """ |
| 90 | + if isinstance(before, datetime): |
| 91 | + before = before.isoformat() + 'Z' |
| 92 | + if isinstance(since, datetime): |
| 93 | + since = since.isoformat() + 'Z' |
84 | 94 | 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 | + } |
86 | 102 | 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 | + |
87 | 112 | return response |
88 | 113 |
|
89 | 114 | def get_transaction(self, transaction_id): |
|
0 commit comments