Core APIs
These are backend functions to help login, query, and store Plaid API data
Calculation Functions
calculate_end_date
calculate_end_date (start_date:datetime.datetime, budget_details:dict)
Calculates the end date based on the budget details (refresh cadence and day of the week).
Type | Details | |
---|---|---|
start_date | datetime | (datetime): The starting date for the budget batch. |
budget_details | dict | (dict): Dictionary containing budget details, including cadence and refresh day. |
Returns | datetime | datetime: The calculated end date. |
API Calls
get_account_transactions
get_account_transactions (access_token:str, start_date:str, end_date:str)
Retrieves all transactions for an account from the Plaid API.
Type | Details | |
---|---|---|
access_token | str | The Plaid access token for the user account |
start_date | str | The starting date for transactions (YYYY-MM-DD) |
end_date | str | The ending date for transactions (YYYY-MM-DD) |
Returns | List | Returns List objects containing Dictionary objects related to account transactions |
get_account
get_account (access_token:str)
Retrieves account details from the Plaid API.
Type | Details | |
---|---|---|
access_token | str | The Plaid access token for the user account |
Returns | Dict | Returns Dictionary object of accounts |
plaid_post
plaid_post (endpoint:str, payload:Dict[str,Any])
Makes a POST request to the Plaid API.
Type | Details | |
---|---|---|
endpoint | str | The specific Plaid API endpoint (e.g., “accounts/get”), refer to Plaid API Docs |
payload | Dict | The JSON payload to be sent with the request |
Returns | Dict | Returns JSON response from Plaid API |
Dataframe Transformation
get_transactions_df
get_transactions_df (access_tokens:List[Tuple[str]], start_date:str=None, end_date:str=None)
Retrieves and converts transaction data for multiple access tokens into a pandas DataFrame.
Type | Default | Details | |
---|---|---|---|
access_tokens | List | List object containing access tokens | |
start_date | str | None | Optional start date (YYYY-MM-DD) |
end_date | str | None | Optional end date (YYYY-MM-DD) |
Returns | DataFrame |
get_accounts_df
get_accounts_df (access_tokens:List[Tuple[str]])
Retrieves and merges account information for multiple access tokens into a single pandas DataFrame.
Type | Details | |
---|---|---|
access_tokens | List | List object containing access tokens |
Returns | DataFrame | Returns Dataframe of accounts |
get_account_df
get_account_df (accounts_response:dict)
Converts account information from the Plaid API response into a pandas DataFrame.
Type | Details | |
---|---|---|
accounts_response | dict | Dictionary object containing accounts |
Returns | DataFrame | Returns Dataframe of individual account |
Database Operations
upsert_dataframe_to_db
upsert_dataframe_to_db (df:pandas.core.frame.DataFrame, table_name:str, unique_key:list)
*Inserts or updates rows in a table based on the given DataFrame.
This function assumes the DataFrame schema is exactly the same as the table.*
Type | Details | |
---|---|---|
df | DataFrame | Data frame to be applied to database |
table_name | str | table name |
unique_key | list | primary key |
Returns | None |
run_sp_update_batch_balances
run_sp_update_batch_balances ()
Runs stored procedure sp_update_batch_balances()
get_budget_rules
get_budget_rules ()
Query the database and return python dictionary of the rules associated with the latest batches
insert_budgetted_transaction
insert_budgetted_transaction (batch_id:int, transaction_id:str)
Insert a new budgetted transaction into the database.
Type | Details | |
---|---|---|
batch_id | int | Integer ID of the batch |
transaction_id | str | ID associated with transaction |
Returns | None |
get_latest_batch_id
get_latest_batch_id (budget_id:int)
Lookup the latest batch_id
associated with a budget_id
Type | Details | |
---|---|---|
budget_id | int | integer of budget_id |
Returns | int | integer of batch_id |
insert_new_budget_batch
insert_new_budget_batch (budget_id:int)
Insert a new budget batch record into the database.
Type | Details | |
---|---|---|
budget_id | int | Integer ID of the budget |
Returns | None |
get_latest_budget_batch
get_latest_budget_batch (id:int)
Returns dictionary with latest budget batch information with provided budget_id
Type | Details | |
---|---|---|
id | int | budget_id |
Returns | Dict | returns a dictionary object with latest budget batch detail |
get_budget_details
get_budget_details (id:int)
Returns dictionary with budget detail information with provided budget_id
Type | Details | |
---|---|---|
id | int | budget_id |
Returns | Dict | returns a dictionary object with budgetting details |
get_all_active_budgets
get_all_active_budgets ()
Returns dataframe containig budgets that are active
insert_new_budget
insert_new_budget (name:str, description:str, limit:float, cadence:str='weekly', date_of_week:str='sun', rules:str=None)
Insert a new budget record into the database.
Type | Default | Details | |
---|---|---|---|
name | str | String Budget Name | |
description | str | String Budget Description | |
limit | float | Maximum value for Budget | |
cadence | str | weekly | String, one of [‘weekly’, ‘biweekly’, ‘monthly’, ‘yearly’] |
date_of_week | str | sun | String, one of [ ‘mon’, ‘tue’, ‘wed’, ‘thu’, ‘fri’, ‘sat’, ‘sun’] |
rules | str | None | String, representing latter half of SQL query to define the types of transactions to be automatically associated |
Returns | None |
update_last_refresh
update_last_refresh (success:bool=True, msg:str='Refresh was successful', ref_type:str=None)
Update database to log refresh time and status. If an error is thrown, the msg
committed to database will print stack trace
Type | Default | Details | |
---|---|---|---|
success | bool | True | Assumes that refresh was successful |
msg | str | Refresh was successful | Refresh message |
ref_type | str | None | Type of refresh one of [‘Transaction’, ’Balance_Hist] |
Returns | None |
get_last_successful_refresh
get_last_successful_refresh ()
Query database to check when the last successful refresh was
upsert_account_balances_df
upsert_account_balances_df (accounts_df:pandas.core.frame.DataFrame)
Inserts or updates account balance history in the database.
Type | Details | |
---|---|---|
accounts_df | DataFrame | A DataFrame containing account balance data |
Returns | None |
insert_transactions_df
insert_transactions_df (transactions_df:pandas.core.frame.DataFrame)
Inserts transaction data into the database.
Type | Details | |
---|---|---|
transactions_df | DataFrame | A DataFrame containing transaction data |
Returns | None |
insert_account_df
insert_account_df (access_token:str, accounts_response:dict, email:str, phone:str)
Inserts account information and the associated access token into the database.
Type | Details | |
---|---|---|
access_token | str | The Plaid access token for the account |
accounts_response | dict | The response from the Plaid API containing account information |
str | The user email | |
phone | str | The phone number associated with user |
Returns | None |
get_stored_public_access_tokens
get_stored_public_access_tokens ()
Retrieves distinct Plaid access tokens from the local database.
db_sql
db_sql (query:str)
Executes a defined SQL query and returns the result as a pandas DataFrame.
Type | Details | |
---|---|---|
query | str | The string representation of SQL query to execute |
Returns | DataFrame | Dataframe of executed SQL query |
db_conn
db_conn ()
Creates and returns a connection to the PostgreSQL database.
Authentication
get_and_save_public_token
get_and_save_public_token (link_token:str, email:str, phone:str)
Retrieves a public token using the link token, exchanges it for an access token, and saves the account information to the database.
Type | Details | |
---|---|---|
link_token | str | The link token generated during the Plaid Link flow |
str | The user’s email address | |
phone | str | The user’s phone number |
Returns | None |
generate_link_token
generate_link_token (email:str, phone:str)
Generates a link token to authenticate with the Plaid API.
Type | Details | |
---|---|---|
str | The user’s email address | |
phone | str | The user’s phone number |
Returns | Optional | Returns the generated link token if successful, otherwise None |
Tasks
get_and_save_balance_history
get_and_save_balance_history ()
*Retrieves account balance history for stored public access tokens and updates the balance history in the database.
Steps:
- Fetch public access tokens from the database.
- Retrieve account details for each account associated with the tokens.
- Update the account balance history in the database.
Returns: None*
get_and_save_all_account_transactions
get_and_save_all_account_transactions (first_time=False)
*Retrieves all account transactions for stored public access tokens and inserts the transactions into the database.
Steps:
- Fetch public access tokens from the database.
- Retrieve transactions data for each account associated with the tokens. (2 cases, First time load or Incremental)
- Insert the retrieved transactions into the database.
Returns: None*
run_budgetting_tasks
run_budgetting_tasks ()
run_budgetting_rules
run_budgetting_rules ()
Automatically assign transactions to budgets based on pre-determined rules. These rules are not yet ‘validated’
about
about ()
Print environmental details for this instance of jupyter-finance