Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add start_Date, end_date and checked to transactions, add unpaged transactions retrieval #227

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions finary_uapi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
finary_uapi institution_connections
finary_uapi organizations
finary_uapi timeseries <period> <type>
finary_uapi checking_accounts transactions [--page=<page>] [--perpage=<perpage>] [--account=<account_ids>] [--institution=<institution_ids>] [--query=<query>]
finary_uapi checking_accounts transactions [--page=<page>] [--perpage=<perpage>] [--account=<account_ids>] [--institution=<institution_ids>] [--query=<query>] [--start-date=<start_date>] [--end-date=<end_date>] [--marked=<marked>]
finary_uapi fonds_euro
finary_uapi startups
finary_uapi investments
finary_uapi investments dividends
finary_uapi investments transactions [--page=<page>] [--perpage=<perpage>] [--account=<account_ids>] [--institution=<institution_ids>] [--query=<query>]
finary_uapi investments transactions [--page=<page>] [--perpage=<perpage>] [--account=<account_ids>] [--institution=<institution_ids>] [--query=<query>] [--start-date=<start_date>] [--end-date=<end_date>] [--marked=<marked>]
finary_uapi crowdlendings
finary_uapi crowdlendings distribution
finary_uapi crowdlendings add <account_name> <name> <annual_yield> <month_duration> <initial_investment> <current_price> <currency_code> <start_date>
Expand Down Expand Up @@ -45,7 +45,7 @@
finary_uapi securities delete <security_id>
finary_uapi insights
finary_uapi loans
finary_uapi credit_accounts transactions [--page=<page>] [--perpage=<perpage>] [--account=<account_ids>] [--institution=<institution_ids>] [--query=<query>]
finary_uapi credit_accounts transactions [--page=<page>] [--perpage=<perpage>] [--account=<account_ids>] [--institution=<institution_ids>] [--query=<query>] [--start-date=<start_date>] [--end-date=<end_date>] [--marked=<marked>]
finary_uapi real_estates
finary_uapi real_estates add rent <address> <user_estimated_value> <description> <surface> <buying_price> <building_type> <ownership_percentage> <monthly_charges> <monthly_rent> <yearly_taxes> <rental_period> <rental_type> [<currency_code>]
finary_uapi real_estates add <category> <address> <user_estimated_value> <description> <surface> <buying_price> <building_type> <ownership_percentage> [<currency_code>]
Expand All @@ -72,6 +72,9 @@
--account=<account_ids> Account ids, comma separated if necessary
--institution=<institution_ids> Institutions (banks) ids, comma separated if necessary
--query=<query> Full text search
--start-date=<start_date> Start date for transactions (format: YYYY-MM-DD)
--end-date=<end_date> End date for transactions (format: YYYY-MM-DD)
--marked=<marked> Filter marked transactions (true or false)


""" # noqa
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
123 changes: 120 additions & 3 deletions finary_uapi/user_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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,
)


Expand All @@ -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,
)


Expand All @@ -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
Loading