Skip to content

Commit

Permalink
Add gocardless banks cli
Browse files Browse the repository at this point in the history
  • Loading branch information
axeoman committed Aug 13, 2023
1 parent cb322e2 commit d1b2ef4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
30 changes: 26 additions & 4 deletions ynab_sync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
handlers=[logging.StreamHandler(sys.stdout)],
)

from .logic import (get_gocardless_transactions, get_ynab_budget,
get_ynab_budgets, prepare_ynab_transactions,
upload_to_ynab)
from .logic import (get_gocardless_banks, get_gocardless_transactions,
get_ynab_budget, get_ynab_budgets,
prepare_ynab_transactions, upload_to_ynab)


@app.command()
Expand Down Expand Up @@ -85,7 +85,7 @@ def upload(

@app.command()
def ynab():
pass
...


@app.command("ynab").command()
Expand Down Expand Up @@ -123,3 +123,25 @@ def accounts(*, ynab_token: str = "", ynab_budget_id: str = ""):
tablefmt="github",
)
print(table)


@app.command()
def gocardless():
...


@app.command("gocardless").command()
def banks(
country: str,
*,
gocardless_secret_id: str = "",
gocardless_secret_key: str = "",
):
banks = get_gocardless_banks(
secret_id=gocardless_secret_id,
secret_key=gocardless_secret_key,
country=country,
)
table = tabulate([(bank.id, bank.name) for bank in banks], headers=["ID", "NAME"])

print(table)
10 changes: 9 additions & 1 deletion ynab_sync/gocardless/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import requests

from .models import GoCardlessBankAccountData
from .models import GoCardlessBankAccountData, GoCardlessInstitution

BASE_URL = "https://bankaccountdata.gocardless.com/api/v2"

Expand Down Expand Up @@ -67,3 +67,11 @@ def get_transactions(
response.raise_for_status()
json_data = response.json()
return GoCardlessBankAccountData(**json_data)

def get_banks(self, country: str) -> list[GoCardlessInstitution]:
response = self._requests_session.get(
f"{BASE_URL}/institutions/?country={country}"
)
response.raise_for_status()
json_data = response.json()
return [GoCardlessInstitution(**institution) for institution in json_data]
5 changes: 5 additions & 0 deletions ynab_sync/gocardless/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ class GoCardlessTransactions(BaseModel):

class GoCardlessBankAccountData(BaseModel):
transactions: GoCardlessTransactions


class GoCardlessInstitution(BaseModel):
id: str
name: str
5 changes: 5 additions & 0 deletions ynab_sync/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ def get_ynab_budgets(token: str) -> list[YNABBudget]:
def get_ynab_budget(token: str, budget_id: UUID) -> YNABBudget:
ynab_api = YnabAPI(access_token=token)
return ynab_api.get_budget(budget_id=budget_id)


def get_gocardless_banks(secret_id: str, secret_key: str, country: str):
gocardless_api = GoCardLessAPI(secret_id=secret_id, secret_key=secret_key)
return gocardless_api.get_banks(country=country)

0 comments on commit d1b2ef4

Please sign in to comment.