-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathynab.py
47 lines (40 loc) · 1.52 KB
/
ynab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import requests
from models import Transaction, Category, UpdatedTransaction
import pickle
BUDGET_ID = 'last-used'
YNAB_API_KEY = os.environ['YNAB_API_KEY']
DEFAULT_HEADERS = {
'accept': 'application/json',
'Authorization': f'Bearer {YNAB_API_KEY}',
'Content-Type': 'application/json',
}
def get_transactions(server_knowledge=None) -> tuple[list[Transaction], str]:
url = f'https://api.ynab.com/v1/budgets/{BUDGET_ID}/transactions?type=uncategorized'
if server_knowledge:
url += f'&last_knowledge_of_server={server_knowledge}'
resp = requests.get(url, headers=DEFAULT_HEADERS)
resp.raise_for_status()
data = resp.json()['data']
return [Transaction.model_validate(t) for t in data['transactions']], data['server_knowledge']
def get_categories() -> list[Category]:
resp = requests.get(
f'https://api.ynab.com/v1/budgets/{BUDGET_ID}/categories',
headers=DEFAULT_HEADERS,
)
resp.raise_for_status()
categories_data = resp.json()
return [
Category(cg['name'], c['name'], c['id'])
for cg in categories_data['data']['category_groups']
for c in cg['categories']
if cg['name'].startswith('[Auto]')
]
def patch_transactions(transactions: list[UpdatedTransaction]):
resp = requests.patch(
f'https://api.ynab.com/v1/budgets/{BUDGET_ID}/transactions',
headers=DEFAULT_HEADERS,
json=dict(transactions=[t.model_dump() for t in transactions]),
)
import pdb; pdb.set_trace()
resp.raise_for_status()