From 7189700eadb51ed8242d3851f0bdd3790a3206ea Mon Sep 17 00:00:00 2001 From: Paul Zwicker Date: Sat, 13 Jan 2024 13:40:58 -0600 Subject: [PATCH 1/2] added get_account_history method and updated readme --- README.md | 1 + monarchmoney/monarchmoney.py | 226 +++++++++++++++++++++++++++++++++++ 2 files changed, 227 insertions(+) diff --git a/README.md b/README.md index 732dcfa..535fe5d 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ As of writing this README, the following methods are supported: - `get_accounts` - gets all the accounts linked to Monarch Money - `get_account_holdings` - gets all of the securities in a brokerage or similar type of account - `get_account_type_options` - all account types and their subtypes available in Monarch Money- +- `get_account_history` - gets all daily account history for the specified account - `get_institutions` -- gets institutions linked to Monarch Money - `get_budgets` — all the budgets and the corresponding actual amounts - `get_subscription_details` - gets the Monarch Money account's status (e.g. paid or trial) diff --git a/monarchmoney/monarchmoney.py b/monarchmoney/monarchmoney.py index 27f65db..d69b280 100644 --- a/monarchmoney/monarchmoney.py +++ b/monarchmoney/monarchmoney.py @@ -489,6 +489,232 @@ async def get_account_holdings(self, account_id: int) -> Dict[str, Any]: variables=variables, ) + async def get_account_history(self, account_id: int) -> Dict[str, Any]: + """ + Gets historical account snapshot data for the requested account + + Args: + account_id: Monarch account ID as an integer + + Returns: + json object with all historical snapshots of requested account's balances + """ + + query = gql( + """ + query AccountDetails_getAccount($id: UUID!, $filters: TransactionFilterInput) { + account(id: $id) { + id + ...AccountFields + ...EditAccountFormFields + isLiability + credential { + id + hasSyncInProgress + canBeForceRefreshed + disconnectedFromDataProviderAt + dataProvider + institution { + id + plaidInstitutionId + url + ...InstitutionStatusFields + __typename + } + __typename + } + institution { + id + plaidInstitutionId + url + ...InstitutionStatusFields + __typename + } + __typename + } + transactions: allTransactions(filters: $filters) { + totalCount + results(limit: 20) { + id + ...TransactionsListFields + __typename + } + __typename + } + snapshots: snapshotsForAccount(accountId: $id) { + date + signedBalance + __typename + } + } + + fragment AccountFields on Account { + id + displayName + syncDisabled + deactivatedAt + isHidden + isAsset + mask + createdAt + updatedAt + displayLastUpdatedAt + currentBalance + displayBalance + includeInNetWorth + hideFromList + hideTransactionsFromReports + includeBalanceInNetWorth + includeInGoalBalance + dataProvider + dataProviderAccountId + isManual + transactionsCount + holdingsCount + manualInvestmentsTrackingMethod + order + icon + logoUrl + type { + name + display + group + __typename + } + subtype { + name + display + __typename + } + credential { + id + updateRequired + disconnectedFromDataProviderAt + dataProvider + institution { + id + plaidInstitutionId + name + status + logo + __typename + } + __typename + } + institution { + id + name + logo + primaryColor + url + __typename + } + __typename + } + + fragment EditAccountFormFields on Account { + id + displayName + deactivatedAt + displayBalance + includeInNetWorth + hideFromList + hideTransactionsFromReports + dataProvider + dataProviderAccountId + isManual + manualInvestmentsTrackingMethod + isAsset + invertSyncedBalance + canInvertBalance + type { + name + display + __typename + } + subtype { + name + display + __typename + } + __typename + } + + fragment InstitutionStatusFields on Institution { + id + hasIssuesReported + hasIssuesReportedMessage + plaidStatus + status + balanceStatus + transactionsStatus + __typename + } + + fragment TransactionsListFields on Transaction { + id + ...TransactionOverviewFields + __typename + } + + fragment TransactionOverviewFields on Transaction { + id + amount + pending + date + hideFromReports + plaidName + notes + isRecurring + reviewStatus + needsReview + dataProviderDescription + attachments { + id + __typename + } + isSplitTransaction + category { + id + name + icon + group { + id + type + __typename + } + __typename + } + merchant { + name + id + transactionsCount + __typename + } + tags { + id + name + color + order + __typename + } + __typename + } + """ + ) + + variables = {"id": str(account_id)} + + account_details = await self.gql_call( + operation="AccountDetails_getAccount", + graphql_query=query, + variables=variables, + ) + + balance_history = account_details["snapshots"] + + return balance_history + async def get_institutions(self) -> Dict[str, Any]: """ Gets institution data from the account. From e4a1e7a2bf6bd6b81221a971f3c3d43b34fc7aff Mon Sep 17 00:00:00 2001 From: Paul Zwicker Date: Sat, 13 Jan 2024 14:05:41 -0600 Subject: [PATCH 2/2] added account ID and name descriptors to returned json --- monarchmoney/monarchmoney.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/monarchmoney/monarchmoney.py b/monarchmoney/monarchmoney.py index d69b280..9ffa606 100644 --- a/monarchmoney/monarchmoney.py +++ b/monarchmoney/monarchmoney.py @@ -711,9 +711,16 @@ async def get_account_history(self, account_id: int) -> Dict[str, Any]: variables=variables, ) - balance_history = account_details["snapshots"] + # Parse JSON + account_name = account_details["account"]["displayName"] + account_balance_history = account_details["snapshots"] - return balance_history + # Append account identification data to account balance history + for i in account_balance_history: + i.update(dict(accountId=str(account_id))) + i.update(dict(accountName=account_name)) + + return account_balance_history async def get_institutions(self) -> Dict[str, Any]: """