andreroggeri / ynab-sdk-python

Python implementation of the YNAB API (https://api.youneedabudget.com/)
Apache License 2.0
27 stars 10 forks source link

get_transactions_from_account function does not accept the available optional filter params since_date or type #136

Open leotulipan opened 2 months ago

leotulipan commented 2 months ago

Your get_transactions_from_account implementation doesnt take the params since_date or type, which are really helpful for filtering and speeding up things when you only need transactions from the last couple of weeks. See the official docs here: https://api.ynab.com/v1#/Transactions/getTransactions.

I do not have the time to do a proper PR so I simply extended the class in my project like this

from ynab_sdk.api.transactions import TransactionsApi
from ynab_sdk.api.transactions import TransactionsApi
from ynab_sdk.api.transactions import TransactionsResponse

class ExtendedTransactionsApi(TransactionsApi):
    def get_transactions_from_account(self, budget_id, since_date=None, type=None):
        params = {}
        if since_date:
            params['since_date'] = since_date
        if type:
            params['type'] = type

        return self.service.get(f'/budgets/{budget_id}/transactions', params)

def construct_url_with_params(base_url, params):
    if params:
        param_str = "&".join(f"{k}={v}" for k, v in params.items())
        return f"{base_url}?{param_str}"
    else:
        return base_url

class ExtendedTransactionsApi(TransactionsApi):
    def get_transactions_from_account(self, budget_id, account_id, since_date=None, type=None):
        params = {}
        if since_date:
            params['since_date'] = since_date
        if type:
            params['type'] = type

        url = construct_url_with_params(f'/budgets/{budget_id}/accounts/{account_id}/transactions', params)
        response = self.client.get(url)
        return TransactionsResponse.from_dict(response)

class ExtendedYNAB(YNAB):
    def __init__(self, token):
        super().__init__(token)
        self.extended_transactions = ExtendedTransactionsApi(self.transactions.client)

#ynab = YNAB(YNAB_API_KEY)
ynab = ExtendedYNAB(YNAB_API_KEY)

Now I can do this:

transactions = ynab.extended_transactions.get_transactions_from_account(budget_id=budget, account_id=account_dict['Savings'], since_date="2024-02-26", type="uncategorized")