Skip to content

Commit

Permalink
Remove direct deps to satspay (#1)
Browse files Browse the repository at this point in the history
* refactor: remove dependency to `create_charge` from `satspay`
* refactor: remove dependency to `satspay.crud.delete_charge`
  • Loading branch information
motorina0 authored Feb 20, 2023
1 parent de4ee82 commit 2d3e544
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
17 changes: 10 additions & 7 deletions crud.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,))


Expand All @@ -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,))
Expand All @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -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()
41 changes: 23 additions & 18 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down Expand Up @@ -69,31 +68,31 @@ 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,
sats=int(data.sats),
tipjar=data.tipjar,
)

return {"redirect_url": f"/satspay/{charge.id}"}
return {"redirect_url": f"/satspay/{charge_id}"}


@tipjar_ext.get("/api/v1/tipjars")
Expand Down Expand Up @@ -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

Expand All @@ -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

0 comments on commit 2d3e544

Please sign in to comment.