From 7ef1c326940f84d703b158a3f14a7d75ecf3c357 Mon Sep 17 00:00:00 2001 From: lasconic Date: Thu, 10 Oct 2024 19:46:49 +0200 Subject: [PATCH] add start_Date, end_date and checked to transactions, add unpaged transactions retrieval --- finary_uapi/__main__.py | 18 ++++- finary_uapi/user_portfolio.py | 123 +++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/finary_uapi/__main__.py b/finary_uapi/__main__.py index fd9942b..f515c65 100644 --- a/finary_uapi/__main__.py +++ b/finary_uapi/__main__.py @@ -6,12 +6,12 @@ finary_uapi institution_connections finary_uapi organizations finary_uapi timeseries - finary_uapi checking_accounts transactions [--page=] [--perpage=] [--account=] [--institution=] [--query=] + finary_uapi checking_accounts transactions [--page=] [--perpage=] [--account=] [--institution=] [--query=] [--start-date=] [--end-date=] [--marked=] finary_uapi fonds_euro finary_uapi startups finary_uapi investments finary_uapi investments dividends - finary_uapi investments transactions [--page=] [--perpage=] [--account=] [--institution=] [--query=] + finary_uapi investments transactions [--page=] [--perpage=] [--account=] [--institution=] [--query=] [--start-date=] [--end-date=] [--marked=] finary_uapi crowdlendings finary_uapi crowdlendings distribution finary_uapi crowdlendings add @@ -45,7 +45,7 @@ finary_uapi securities delete finary_uapi insights finary_uapi loans - finary_uapi credit_accounts transactions [--page=] [--perpage=] [--account=] [--institution=] [--query=] + finary_uapi credit_accounts transactions [--page=] [--perpage=] [--account=] [--institution=] [--query=] [--start-date=] [--end-date=] [--marked=] finary_uapi real_estates finary_uapi real_estates add rent
[] finary_uapi real_estates add
[] @@ -72,6 +72,9 @@ --account= Account ids, comma separated if necessary --institution= Institutions (banks) ids, comma separated if necessary --query= Full text search + --start-date= Start date for transactions (format: YYYY-MM-DD) + --end-date= End date for transactions (format: YYYY-MM-DD) + --marked= Filter marked transactions (true or false) """ # noqa @@ -181,6 +184,9 @@ def main() -> int: # pragma: nocover account_id=args["--account"], institution_id=args["--institution"], query=args["--query"], + start_date=args["--start-date"], + end_date=args["--end-date"], + marked=args["--marked"], ) elif args["fonds_euro"]: result = get_user_fonds_euro(session) @@ -360,6 +366,9 @@ def main() -> int: # pragma: nocover account_id=args["--account"], institution_id=args["--institution"], query=args["--query"], + start_date=args["--start-date"], + end_date=args["--end-date"], + marked=args["--marked"], ) else: result = get_portfolio_investments(session) @@ -396,6 +405,9 @@ def main() -> int: # pragma: nocover account_id=args["--account"], institution_id=args["--institution"], query=args["--query"], + start_date=args["--start-date"], + end_date=args["--end-date"], + marked=args["--marked"], ) elif args["real_estates"]: result = get_user_real_estates(session) diff --git a/finary_uapi/user_portfolio.py b/finary_uapi/user_portfolio.py index c9a315f..e17e0ee 100644 --- a/finary_uapi/user_portfolio.py +++ b/finary_uapi/user_portfolio.py @@ -83,10 +83,27 @@ def get_portfolio_transactions( query: str = "", account_id: str = "", institution_id: str = "", + start_date: str = "", + end_date: str = "", + marked: str = "", ): """ `portfolio_type` is "investments", "checking_accounts", "credit_accounts" + `start_date` and `end_date` should be in YYYY-MM-DD format + `marked` can be "true" or "false" """ + if page == "-1": + return get_portfolio_unpaged_transactions( + session, + portfolio_type=portfolio_type, + per_page=per_page, + query=query, + account_id=account_id, + institution_id=institution_id, + start_date=start_date, + end_date=end_date, + marked=marked, + ) url = f"{portfolio_api}/{portfolio_type}/transactions" params: Dict[str, Union[str, int]] = {} params["page"] = page @@ -97,6 +114,12 @@ def get_portfolio_transactions( params["account_id"] = account_id if institution_id: params["institution_id"] = institution_id + if start_date: + params["start_date"] = start_date + if end_date: + params["end_date"] = end_date + if marked: + params["marked"] = marked x = session.get(url, params=params) return x.json() @@ -108,10 +131,22 @@ def get_portfolio_checking_accounts_transactions( query: str = "", account_id: str = "", institution_id: str = "", + start_date: str = "", + end_date: str = "", + marked: str = "", ): portfolio_type = "checking_accounts" return get_portfolio_transactions( - session, portfolio_type, page, per_page, query, account_id, institution_id + session, + portfolio_type, + page, + per_page, + query, + account_id, + institution_id, + start_date, + end_date, + marked, ) @@ -122,10 +157,22 @@ def get_portfolio_credit_accounts_transactions( query: str = "", account_id: str = "", institution_id: str = "", + start_date: str = "", + end_date: str = "", + marked: str = "", ): portfolio_type = "credit_accounts" return get_portfolio_transactions( - session, portfolio_type, page, per_page, query, account_id, institution_id + session, + portfolio_type, + page, + per_page, + query, + account_id, + institution_id, + start_date, + end_date, + marked, ) @@ -136,8 +183,78 @@ def get_portfolio_investments_transactions( query: str = "", account_id: str = "", institution_id: str = "", + start_date: str = "", + end_date: str = "", + marked: str = "", ): portfolio_type = "investments" return get_portfolio_transactions( - session, portfolio_type, page, per_page, query, account_id, institution_id + session, + portfolio_type, + page, + per_page, + query, + account_id, + institution_id, + start_date, + end_date, + marked, ) + + +def get_portfolio_unpaged_transactions( + session: requests.Session, + portfolio_type: str = "checking_accounts", + per_page=50, + query: str = "", + account_id: str = "", + institution_id: str = "", + start_date: str = "", + end_date: str = "", + marked: str = "", +): + """ + Retrieves all transactions for a given portfolio type without pagination. + + This function calls get_portfolio_transactions multiple times, incrementing the page + number until there are no more results. It then extends the "result" array of the first response. + """ + page = 1 + + first_response = get_portfolio_transactions( + session, + portfolio_type, + page, + per_page, + query, + account_id, + institution_id, + start_date, + end_date, + marked, + ) + + if "result" not in first_response: + return first_response + + while True: + page += 1 + next_response = get_portfolio_transactions( + session, + portfolio_type, + page, + per_page, + query, + account_id, + institution_id, + start_date, + end_date, + marked, + ) + + if "result" not in next_response or not next_response["result"]: + break + + first_response["result"].extend(next_response["result"]) + + return first_response