Skip to content

Commit

Permalink
feat: update to lnbits 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Sep 5, 2024
1 parent 5615863 commit 81800a3
Show file tree
Hide file tree
Showing 5 changed files with 784 additions and 708 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"short_description": "Connect a Bleskomat ATM to an lnbits",
"tile": "/bleskomat/static/image/bleskomat.png",
"contributors": ["chill117"],
"min_lnbits_version": "0.12.6"
"min_lnbits_version": "1.0.0"
}
83 changes: 45 additions & 38 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import List, Optional, Union
from uuid import uuid4

from lnbits.helpers import update_query

from .db import db
from .helpers import generate_bleskomat_lnurl_hash
from .models import Bleskomat, BleskomatLnurl, CreateBleskomat
Expand All @@ -20,19 +22,20 @@ async def create_bleskomat(data: CreateBleskomat, wallet_id: str) -> Bleskomat:
id, wallet, api_key_id, api_key_secret, api_key_encoding,
name, fiat_currency, exchange_rate_provider, fee
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (:id, :wallet, :api_key_id, :api_key_secret, :api_key_encoding,
:name, :fiat_currency, :exchange_rate_provider, :fee)
""",
(
bleskomat_id,
wallet_id,
api_key_id,
api_key_secret,
api_key_encoding,
data.name,
data.fiat_currency,
data.exchange_rate_provider,
data.fee,
),
{
"id": bleskomat_id,
"wallet": wallet_id,
"api_key_id": api_key_id,
"api_key_secret": api_key_secret,
"api_key_encoding": api_key_encoding,
"name": data.name,
"fiat_currency": data.fiat_currency,
"exchange_rate_provider": data.exchange_rate_provider,
"fee": data.fee,
},
)
bleskomat = await get_bleskomat(bleskomat_id)
assert bleskomat, "Newly created bleskomat couldn't be retrieved"
Expand All @@ -41,42 +44,44 @@ async def create_bleskomat(data: CreateBleskomat, wallet_id: str) -> Bleskomat:

async def get_bleskomat(bleskomat_id: str) -> Optional[Bleskomat]:
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,)
"SELECT * FROM bleskomat.bleskomats WHERE id = :id", {"id": bleskomat_id}
)
return Bleskomat(**row) if row else None


async def get_bleskomat_by_api_key_id(api_key_id: str) -> Optional[Bleskomat]:
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE api_key_id = ?", (api_key_id,)
"SELECT * FROM bleskomat.bleskomats WHERE api_key_id = :id", {"id": api_key_id}
)
return Bleskomat(**row) if row else None


async def get_bleskomats(wallet_ids: Union[str, List[str]]) -> List[Bleskomat]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
rows = await db.fetchall(
f"SELECT * FROM bleskomat.bleskomats WHERE wallet IN ({q})", (*wallet_ids,)
f"SELECT * FROM bleskomat.bleskomats WHERE wallet IN ({q})"
)
return [Bleskomat(**row) for row in rows]


async def update_bleskomat(bleskomat_id: str, **kwargs) -> Optional[Bleskomat]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
async def update_bleskomat(bleskomat_id: str, data: CreateBleskomat) -> Bleskomat:
await db.execute(
f"UPDATE bleskomat.bleskomats SET {q} WHERE id = ?",
(*kwargs.values(), bleskomat_id),
update_query("bleskomat.bleskomats", data),
{"id": bleskomat_id, **data.dict()}
)
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,)
"SELECT * FROM bleskomat.bleskomats WHERE id = :id", {"id": bleskomat_id}
)
return Bleskomat(**row) if row else None
assert row, "Bleskomat not found after update"
return Bleskomat(**row)


async def delete_bleskomat(bleskomat_id: str) -> None:
await db.execute("DELETE FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,))
await db.execute(
"DELETE FROM bleskomat.bleskomats WHERE id = :id", {"id": bleskomat_id}
)


async def create_bleskomat_lnurl(
Expand All @@ -91,21 +96,22 @@ async def create_bleskomat_lnurl(
id, bleskomat, wallet, hash, tag, params, api_key_id,
initial_uses, remaining_uses, created_time, updated_time
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (:id, :bleskomat, :wallet, :hash, :tag, :params, :api_key_id,
:initial_uses, :remaining_uses, :created_time, :updated_time)
""",
(
bleskomat_lnurl_id,
bleskomat.id,
bleskomat.wallet,
lnurl_hash,
tag,
params,
bleskomat.api_key_id,
uses,
uses,
now,
now,
),
{
"id": bleskomat_lnurl_id,
"bleskomat": bleskomat.id,
"wallet": bleskomat.wallet,
"hash": lnurl_hash,
"tag": tag,
"params": params,
"api_key_id": bleskomat.api_key_id,
"initial_uses": uses,
"remaining_uses": uses,
"created_time": now,
"updated_time": now,
},
)
bleskomat_lnurl = await get_bleskomat_lnurl(secret)
assert bleskomat_lnurl, "Newly created bleskomat LNURL couldn't be retrieved"
Expand All @@ -115,6 +121,7 @@ async def create_bleskomat_lnurl(
async def get_bleskomat_lnurl(secret: str) -> Optional[BleskomatLnurl]:
lnurl_hash = generate_bleskomat_lnurl_hash(secret)
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = ?", (lnurl_hash,)
"SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = :hash",
{"hash": lnurl_hash},
)
return BleskomatLnurl(**row) if row else None
Loading

0 comments on commit 81800a3

Please sign in to comment.