From fcb0f1edd7f7ebd556ee31912ba25ee0b01d7edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elisi=C3=A1rio=20Couto?= Date: Fri, 8 Mar 2024 00:03:11 +0000 Subject: [PATCH] feat(commands): Add new `leggen bank delete` command to delete a bank connection. --- leggen/commands/bank/delete.py | 26 ++++++++++++++++++++++++++ leggen/commands/status.py | 1 + leggen/utils/network.py | 15 +++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 leggen/commands/bank/delete.py diff --git a/leggen/commands/bank/delete.py b/leggen/commands/bank/delete.py new file mode 100644 index 0000000..66f116b --- /dev/null +++ b/leggen/commands/bank/delete.py @@ -0,0 +1,26 @@ +import click + +from leggen.main import cli +from leggen.utils.network import delete as http_delete +from leggen.utils.text import info, success + + +@cli.command() +@click.argument("requisition_id", type=str, required=True, metavar="REQUISITION_ID") +@click.pass_context +def delete(ctx, requisition_id: str): + """ + Delete bank connection + + REQUISITION_ID: The ID of the Bank Requisition to delete + + Check `leggen status` to get the REQUISITION_ID + """ + info(f"Deleting Bank Requisition: {requisition_id}") + + _ = http_delete( + ctx, + f"/requisitions/{requisition_id}", + ) + + success(f"Bank Requisition {requisition_id} deleted") diff --git a/leggen/commands/status.py b/leggen/commands/status.py index 76cc001..998727c 100644 --- a/leggen/commands/status.py +++ b/leggen/commands/status.py @@ -22,6 +22,7 @@ def status(ctx: click.Context): "Bank": r["institution_id"], "Status": REQUISITION_STATUS.get(r["status"], "UNKNOWN"), "Created at": datefmt(r["created"]), + "Requisition ID": r["id"], } ) accounts.update(r.get("accounts", [])) diff --git a/leggen/utils/network.py b/leggen/utils/network.py index 3337a54..4e673d0 100644 --- a/leggen/utils/network.py +++ b/leggen/utils/network.py @@ -47,3 +47,18 @@ def put(ctx: click.Context, path: str, data: dict = {}): error(f"Error: {e}\n{res.text}") ctx.abort() return res.json() + + +def delete(ctx: click.Context, path: str): + """ + DELETE request to the GoCardless API + """ + + url = f"{ctx.obj['api_url']}{path}" + res = requests.delete(url, headers=ctx.obj["headers"]) + try: + res.raise_for_status() + except Exception as e: + error(f"Error: {e}\n{res.text}") + ctx.abort() + return res.json()