Merge pull request #395 from arcbtc/FastAPI

cln sse should be working
This commit is contained in:
Arc
2021-11-04 12:41:59 +00:00
committed by GitHub
4 changed files with 37 additions and 79 deletions

View File

@@ -56,12 +56,7 @@ async def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
if not row: if not row:
return None return None
# link = [] return WithdrawLink(**row) if row else None
# for item in row:
# link.append(item)
# link.append(num)
# print("GET_LINK", WithdrawLink.from_row(row))
return WithdrawLink.from_row(row)
async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]: async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
@@ -70,12 +65,7 @@ async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[Withdra
) )
if not row: if not row:
return None return None
return WithdrawLink(**row) if row else None
# link = []
# for item in row:
# link.append(item)
# link.append(num)
return WithdrawLink.from_row(row)
async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]: async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
@@ -86,14 +76,12 @@ async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[Withdraw
rows = await db.fetchall( rows = await db.fetchall(
f"SELECT * FROM withdraw.withdraw_link WHERE wallet IN ({q})", (*wallet_ids,) f"SELECT * FROM withdraw.withdraw_link WHERE wallet IN ({q})", (*wallet_ids,)
) )
return [WithdrawLink(**row) for row in rows]
return [WithdrawLink.from_row(row) for row in rows]
async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]: async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
if "is_unique" in kwargs: if "is_unique" in kwargs:
kwargs["is_unique"] = int(kwargs["is_unique"]) kwargs["is_unique"] = int(kwargs["is_unique"])
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute( await db.execute(
f"UPDATE withdraw.withdraw_link SET {q} WHERE id = ?", f"UPDATE withdraw.withdraw_link SET {q} WHERE id = ?",
@@ -102,7 +90,7 @@ async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]
row = await db.fetchone( row = await db.fetchone(
"SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,) "SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,)
) )
return WithdrawLink.from_row(row) if row else None return WithdrawLink(**row) if row else None
async def delete_withdraw_link(link_id: str) -> None: async def delete_withdraw_link(link_id: str) -> None:

View File

@@ -54,25 +54,22 @@ async def api_lnurl_callback(
): ):
link = await get_withdraw_link_by_hash(unique_hash) link = await get_withdraw_link_by_hash(unique_hash)
now = int(datetime.now().timestamp()) now = int(datetime.now().timestamp())
print("link")
if not link: if not link:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found." status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
) )
print("link")
if link.is_spent: if link.is_spent:
raise HTTPException( raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
# WHAT STATUS_CODE TO USE?? print("link")
detail="Withdraw is spent."
)
if link.k1 != k1: if link.k1 != k1:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request.") raise HTTPException(status_code=HTTPStatus.OK, detail="Bad request.")
print("link")
if now < link.open_time: if now < link.open_time:
return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."} return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}
print("link")
try: try:
usescsv = "" usescsv = ""
for x in range(1, link.uses - link.used): for x in range(1, link.uses - link.used):
@@ -92,7 +89,6 @@ async def api_lnurl_callback(
"used": link.used + 1, "used": link.used + 1,
"usescsv": usescsv, "usescsv": usescsv,
} }
await update_withdraw_link(link.id, **changes) await update_withdraw_link(link.id, **changes)
await pay_invoice( await pay_invoice(
@@ -101,19 +97,12 @@ async def api_lnurl_callback(
max_sat=link.max_withdrawable, max_sat=link.max_withdrawable,
extra={"tag": "withdraw"}, extra={"tag": "withdraw"},
) )
# should these be "raise" instead of the "return" ??
except ValueError as e:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": str(e)}
except PermissionError:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": "Withdraw link is empty."}
except Exception as e:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": str(e)}
return {"status": "OK"} return {"status": "OK"}
except Exception as e:
wibble = await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": str(e)}
# FOR LNURLs WHICH ARE UNIQUE # FOR LNURLs WHICH ARE UNIQUE
@@ -128,22 +117,11 @@ async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash
if not link: if not link:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found." status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
) )
# return (
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
# HTTPStatus.OK,
# )
if link.is_spent: if link.is_spent:
raise HTTPException( raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
# WHAT STATUS_CODE TO USE??
detail="Withdraw is spent."
)
# return (
# {"status": "ERROR", "reason": "Withdraw is spent."},
# HTTPStatus.OK,
# )
useslist = link.usescsv.split(",") useslist = link.usescsv.split(",")
found = False found = False
@@ -153,11 +131,7 @@ async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash
found = True found = True
if not found: if not found:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found." status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
) )
# return (
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
# HTTPStatus.OK,
# )
return link.lnurl_response(request).dict() return link.lnurl_response(request).dict()

View File

@@ -19,26 +19,19 @@ class CreateWithdrawData(BaseModel):
class WithdrawLink(BaseModel): class WithdrawLink(BaseModel):
id: str id: str
wallet: str wallet: str = Query(None)
title: str title: str = Query(None)
min_withdrawable: int min_withdrawable: int = Query(0)
max_withdrawable: int max_withdrawable: int = Query(0)
uses: int uses: int = Query(0)
wait_time: int wait_time: int = Query(0)
is_unique: bool is_unique: bool = Query(False)
unique_hash: str unique_hash: str = Query(0)
k1: str k1: str = Query(None)
open_time: int open_time: int = Query(0)
used: int used: int = Query(0)
usescsv: str usescsv: str = Query(None)
number: int number: int = Query(0)
@classmethod
def from_row(cls, row: Row) -> "WithdrawLink":
data = dict(row)
data["is_unique"] = bool(data["is_unique"])
data["number"] = 0
return cls(**data)
@property @property
def is_spent(self) -> bool: def is_spent(self) -> bool:

View File

@@ -150,14 +150,16 @@
dense dense
v-model.number="formDialog.data.min_withdrawable" v-model.number="formDialog.data.min_withdrawable"
type="number" type="number"
label="Min withdrawable (sat) *" min="10"
label="Min withdrawable (sat, at least 10) *"
></q-input> ></q-input>
<q-input <q-input
filled filled
dense dense
v-model.number="formDialog.data.max_withdrawable" v-model.number="formDialog.data.max_withdrawable"
type="number" type="number"
label="Max withdrawable (sat) *" min="10"
label="Max withdrawable (sat, at least 10) *"
></q-input> ></q-input>
<q-input <q-input
filled filled
@@ -264,7 +266,8 @@
dense dense
v-model.number="simpleformDialog.data.max_withdrawable" v-model.number="simpleformDialog.data.max_withdrawable"
type="number" type="number"
label="Withdraw amount per voucher" min="10"
label="Withdraw amount per voucher (sat, at least 10)"
></q-input> ></q-input>
<q-input <q-input
filled filled