Merge pull request #406 from arcbtc/FastAPI

Added some IF EXISTS
This commit is contained in:
Arc 2021-11-10 21:18:56 +00:00 committed by GitHub
commit 11ce32cf04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 43 deletions

View File

@ -21,8 +21,9 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
@livestream_ext.get("/track/{track_id}", name="livestream.track_redirect_download")
async def track_redirect_download(track_id, request: Request):
payment_hash = request.path_params["p"]
async def track_redirect_download(track_id, p: str = Query(...)):
print("BOO", track_id, p)
payment_hash = p
track = await get_track(track_id)
ls = await get_livestream_by_track(track_id)
payment: Payment = await get_wallet_payment(ls.wallet, payment_hash)

View File

@ -509,6 +509,13 @@
.then(function (response) {
self.ChargeLinks.push(mapCharge(response.data))
self.formDialogCharge.show = false
self.formDialogCharge.data = {
onchain: false,
lnbits: false,
description: '',
time: null,
amount: null
}
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)

View File

@ -2,9 +2,11 @@ from datetime import datetime
from http import HTTPStatus
import shortuuid # type: ignore
import json
from fastapi import HTTPException
from fastapi.param_functions import Query
from starlette.requests import Request
from starlette.responses import HTMLResponse # type: ignore
from lnbits.core.services import pay_invoice
@ -16,7 +18,7 @@ from .crud import get_withdraw_link_by_hash, update_withdraw_link
@withdraw_ext.get(
"/api/v1/lnurl/{unique_hash}",
status_code=HTTPStatus.OK,
response_class=HTMLResponse,
name="withdraw.api_lnurl_response",
)
async def api_lnurl_response(request: Request, unique_hash):
@ -35,17 +37,67 @@ async def api_lnurl_response(request: Request, unique_hash):
# WHAT STATUS_CODE TO USE??
detail="Withdraw is spent."
)
# return ({"status": "ERROR", "reason": "Withdraw is spent."},
# HTTPStatus.OK,
# )
url = request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash)
withdrawResponse = {
"tag": "withdrawRequest",
"callback": url,
"k1": link.k1,
"minWithdrawable": link.min_withdrawable * 1000,
"maxWithdrawable": link.max_withdrawable * 1000,
"defaultDescription": link.title,
}
return json.dumps(withdrawResponse)
return link.lnurl_response(request).dict()
# FOR LNURLs WHICH ARE UNIQUE
@withdraw_ext.get(
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
response_class=HTMLResponse,
name="withdraw.api_lnurl_multi_response",
)
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
link = await get_withdraw_link_by_hash(unique_hash)
if not link:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
if link.is_spent:
raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
useslist = link.usescsv.split(",")
found = False
for x in useslist:
tohash = link.id + link.unique_hash + str(x)
if id_unique_hash == shortuuid.uuid(name=tohash):
found = True
if not found:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
url = request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash)
withdrawResponse = {
"tag": "withdrawRequest",
"callback": url,
"k1": link.k1,
"minWithdrawable": link.min_withdrawable * 1000,
"maxWithdrawable": link.max_withdrawable * 1000,
"defaultDescription": link.title,
}
return json.dumps(withdrawResponse)
# CALLBACK
@withdraw_ext.get("/api/v1/lnurl/cb/{unique_hash}", name="withdraw.api_lnurl_callback")
@withdraw_ext.get(
"/api/v1/lnurl/cb/{unique_hash}",
status_code=HTTPStatus.OK,
name="withdraw.api_lnurl_callback"
)
async def api_lnurl_callback(
request: Request,
unique_hash: str = Query(...),
@ -102,35 +154,3 @@ async def api_lnurl_callback(
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": "Link not working"}
# FOR LNURLs WHICH ARE UNIQUE
@withdraw_ext.get(
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
status_code=HTTPStatus.OK,
name="withdraw.api_lnurl_multi_response",
)
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
link = await get_withdraw_link_by_hash(unique_hash)
if not link:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
if link.is_spent:
raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
useslist = link.usescsv.split(",")
found = False
for x in useslist:
tohash = link.id + link.unique_hash + str(x)
if id_unique_hash == shortuuid.uuid(name=tohash):
found = True
if not found:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
return link.lnurl_response(request).dict()

View File

@ -8,6 +8,7 @@ import random
from functools import partial, wraps
from os import getenv
from typing import AsyncGenerator, Optional
import time
from .base import (
InvoiceResponse,
@ -30,6 +31,10 @@ def async_wrap(func):
return run
def _pay_invoice(ln, bolt11):
return ln.pay(bolt11)
def _paid_invoices_stream(ln, last_pay_index):
return ln.waitanyinvoice(last_pay_index)
@ -99,7 +104,8 @@ class CLightningWallet(Wallet):
async def pay_invoice(self, bolt11: str) -> PaymentResponse:
try:
r = self.ln.pay(bolt11)
wrapped = async_wrap(_pay_invoice)
r = await wrapped(self.ln, bolt11)
except RpcError as exc:
return PaymentResponse(False, None, 0, None, str(exc))