Skip to content

Commit

Permalink
Merge pull request #66 from andrecloutier/add_balance_history_upload
Browse files Browse the repository at this point in the history
Add ability to upload account history csv
  • Loading branch information
hammem authored Jan 11, 2024
2 parents f5a12b9 + 938ffe1 commit 443e2da
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ As of writing this README, the following methods are supported:
- `update_transaction_splits` - modifies how a transaction is split (or not)
- `set_budget_amount` - sets a budget's value to the given amount (date allowed, will only apply to month specified by default). A zero amount value will "unset" or "clear" the budget for the given category.
- `create_manual_account` - creates a new manual account
- `upload_account_balance_history` - uploads account history csv file for a given account

# Contributing

Expand Down
1 change: 1 addition & 0 deletions monarchmoney/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MonarchMoneyEndpoints,
MonarchMoney,
RequireMFAException,
RequestFailedException,
)

__version__ = "0.1.8"
Expand Down
32 changes: 31 additions & 1 deletion monarchmoney/monarchmoney.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import calendar
from datetime import datetime
import json
import os
import pickle
import oathtool
import time
from typing import Any, Dict, Optional, List

from aiohttp import ClientSession
from aiohttp import ClientSession, FormData
from aiohttp.client import DEFAULT_TIMEOUT
import asyncio
from gql import gql, Client
Expand All @@ -33,6 +34,10 @@ def getLoginEndpoint(cls) -> str:
def getGraphQL(cls) -> str:
return cls.BASE_URL + "/graphql"

@classmethod
def getAccountBalanceHistoryUploadEndpoint(cls) -> str:
return cls.BASE_URL + "/account-balance-history/upload/"


class RequireMFAException(Exception):
pass
Expand Down Expand Up @@ -1864,6 +1869,31 @@ async def set_budget_amount(
graphql_query=query,
)

async def upload_account_balance_history(
self, account_id: str, csv_content: str
) -> None:
"""
Uploads the account balance history csv for a given account.
:param account_id: The account ID to apply the history to.
:param csv_content: CSV representation of the balance history.
"""
if not account_id or not csv_content:
raise RequestFailedException("account_id and csv_content cannot be empty")

filename = "upload.csv"
form = FormData()
form.add_field("files", csv_content, filename=filename, content_type="text/csv")
form.add_field("account_files_mapping", json.dumps({filename: account_id}))

async with ClientSession(headers=self._headers) as session:
resp = await session.post(
MonarchMoneyEndpoints.getAccountBalanceHistoryUploadEndpoint(),
data=form,
)
if resp.status != 200:
raise RequestFailedException(f"HTTP Code {resp.status}: {resp.reason}")

async def gql_call(
self,
operation: str,
Expand Down

0 comments on commit 443e2da

Please sign in to comment.