From 2d3e544f21e55cf2b6cd55550b89050c3f3c68cc Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Mon, 20 Feb 2023 15:24:31 +0200 Subject: [PATCH] Remove direct deps to satspay (#1) * refactor: remove dependency to `create_charge` from `satspay` * refactor: remove dependency to `satspay.crud.delete_charge` --- crud.py | 17 ++++++++++------- helpers.py | 25 +++++++++++++++++++++++++ views_api.py | 41 +++++++++++++++++++++++------------------ 3 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 helpers.py diff --git a/crud.py b/crud.py index 3ea45d0..4c7c2de 100644 --- a/crud.py +++ b/crud.py @@ -1,9 +1,7 @@ -from typing import Optional +from typing import List, Optional from lnbits.db import SQLITE -# todo: use the API, not direct import -from ..satspay.crud import delete_charge # type: ignore from . import db from .models import Tip, TipJar, createTipJar @@ -77,9 +75,9 @@ async def get_tipjars(wallet_id: str) -> Optional[list]: async def delete_tipjar(tipjar_id: int) -> None: """Delete a TipJar and all corresponding Tips""" - rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE tipjar = ?", (tipjar_id,)) - for row in rows: - await delete_tip(row["id"]) + tips = await get_tipjar_tips(tipjar_id) + for tip in tips: + await delete_tip(tip.id) await db.execute("DELETE FROM tipjar.TipJars WHERE id = ?", (tipjar_id,)) @@ -89,6 +87,12 @@ async def get_tip(tip_id: str) -> Optional[Tip]: return Tip(**row) if row else None +async def get_tipjar_tips(tipjar_id: int) -> List[Tip]: + """Return all Tips for a tipjar""" + rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE tipjar = ?", (tipjar_id,)) + return [Tip(**row) for row in rows] + + async def get_tips(wallet_id: str) -> Optional[list]: """Return all Tips assigned to wallet_id""" rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE wallet = ?", (wallet_id,)) @@ -98,7 +102,6 @@ async def get_tips(wallet_id: str) -> Optional[list]: async def delete_tip(tip_id: str) -> None: """Delete a Tip and its corresponding statspay charge""" await db.execute("DELETE FROM tipjar.Tips WHERE id = ?", (tip_id,)) - await delete_charge(tip_id) async def update_tip(tip_id: str, **kwargs) -> Tip: diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..3e0c82d --- /dev/null +++ b/helpers.py @@ -0,0 +1,25 @@ +import httpx + +from lnbits.app import settings + + +async def create_charge(data: dict, api_key: str) -> str: + async with httpx.AsyncClient() as client: + headers = {"X-API-KEY": api_key} + r = await client.post( + url=f"http://{settings.host}:{settings.port}/satspay/api/v1/charge", + headers=headers, + json=data, + ) + r.raise_for_status() + return r.json()["id"] + + +async def delete_charge(charge_id: str, api_key: str): + async with httpx.AsyncClient() as client: + headers = {"X-API-KEY": api_key} + r = await client.delete( + url=f"http://{settings.host}:{settings.port}/satspay/api/v1/charge/{charge_id}", + headers=headers, + ) + r.raise_for_status() diff --git a/views_api.py b/views_api.py index 7d420fa..5e63254 100644 --- a/views_api.py +++ b/views_api.py @@ -6,9 +6,6 @@ from lnbits.core.crud import get_user, get_wallet from lnbits.decorators import WalletTypeInfo, get_key_type -# todo: use the API, not direct import -from ..satspay.crud import create_charge # type: ignore -from ..satspay.models import CreateCharge # type: ignore from . import tipjar_ext from .crud import ( create_tip, @@ -17,11 +14,13 @@ delete_tipjar, get_tip, get_tipjar, + get_tipjar_tips, get_tipjars, get_tips, update_tip, update_tipjar, ) +from .helpers import create_charge, delete_charge from .models import createTip, createTipJar, createTips @@ -69,23 +68,23 @@ async def api_create_tip(data: createTips): name = "Anonymous" description = f"{name}: {message}" - charge = await create_charge( - user=wallet.user, - data=CreateCharge( - amount=sats, - webhook=tipjar.webhook or "", - description=description, - onchainwallet=tipjar.onchain or "", - lnbitswallet=tipjar.wallet, - completelink="/tipjar/" + str(tipjar_id), - completelinktext="Thanks for the tip!", - time=1440, - custom_css="", - ), + charge_id = await create_charge( + data={ + "amount": sats, + "webhook": tipjar.webhook or "", + "description": description, + "onchainwallet": tipjar.onchain or "", + "lnbitswallet": tipjar.wallet, + "completelink": "/tipjar/" + str(tipjar_id), + "completelinktext": "Thanks for the tip!", + "time": 1440, + "custom_css": "", + }, + api_key=wallet.inkey, ) await create_tip( - id=charge.id, + id=charge_id, wallet=tipjar.wallet, message=message, name=name, @@ -93,7 +92,7 @@ async def api_create_tip(data: createTips): tipjar=data.tipjar, ) - return {"redirect_url": f"/satspay/{charge.id}"} + return {"redirect_url": f"/satspay/{charge_id}"} @tipjar_ext.get("/api/v1/tipjars") @@ -195,6 +194,7 @@ async def api_delete_tip( detail="Not authorized to delete this tip!", ) await delete_tip(tip_id) + await delete_charge(tip_id, wallet.wallet.inkey) return "", HTTPStatus.NO_CONTENT @@ -215,6 +215,11 @@ async def api_delete_tipjar( status_code=HTTPStatus.FORBIDDEN, detail="Not authorized to delete this tipjar!", ) + + tips = await get_tipjar_tips(tipjar_id) + for tip in tips: + await delete_charge(tip.id, wallet.wallet.inkey) + await delete_tipjar(tipjar_id) return "", HTTPStatus.NO_CONTENT