satspay initial converstion

This commit is contained in:
Tiago vasconcelos
2021-10-14 11:45:56 +01:00
parent ec89244d7f
commit e939666107
6 changed files with 130 additions and 115 deletions
+54 -69
View File
@@ -1,13 +1,21 @@
import hashlib
from quart import g, jsonify, url_for
from http import HTTPStatus
import httpx
from fastapi import Query
from fastapi.params import Depends
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.decorators import WalletTypeInfo, get_key_type
from lnbits.extensions.satspay import satspay_ext
from .models import CreateCharge
from .crud import (
create_charge,
update_charge,
@@ -20,94 +28,78 @@ from .crud import (
#############################CHARGES##########################
@satspay_ext.route("/api/v1/charge", methods=["POST"])
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["PUT"])
@api_check_wallet_key("admin")
@api_validate_post_request(
schema={
"onchainwallet": {"type": "string"},
"lnbitswallet": {"type": "string"},
"description": {"type": "string", "empty": False, "required": True},
"webhook": {"type": "string"},
"completelink": {"type": "string"},
"completelinktext": {"type": "string"},
"time": {"type": "integer", "min": 1, "required": True},
"amount": {"type": "integer", "min": 1, "required": True},
}
)
async def api_charge_create_or_update(charge_id=None):
@satspay_ext.post("/api/v1/charge")
@satspay_ext.put("/api/v1/charge/{charge_id}")
async def api_charge_create_or_update(data: CreateCharge, wallet: WalletTypeInfo = Depends(get_key_type), charge_id=None):
if not charge_id:
charge = await create_charge(user=g.wallet.user, **g.data)
return jsonify(charge._asdict()), HTTPStatus.CREATED
charge = await create_charge(user=wallet.wallet.user, **data)
return charge.dict()
else:
charge = await update_charge(charge_id=charge_id, **g.data)
return jsonify(charge._asdict()), HTTPStatus.OK
charge = await update_charge(charge_id=charge_id, **data)
return charge.dict()
@satspay_ext.route("/api/v1/charges", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_charges_retrieve():
@satspay_ext.get("/api/v1/charges")
async def api_charges_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
try:
return (
jsonify(
[
return [
{
**charge._asdict(),
**charge.dict(),
**{"time_elapsed": charge.time_elapsed},
**{"paid": charge.paid},
}
for charge in await get_charges(g.wallet.user)
for charge in await get_charges(wallet.wallet.user)
]
),
HTTPStatus.OK,
)
except:
return ""
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_charge_retrieve(charge_id):
@satspay_ext.get("/api/v1/charge/{charge_id}")
async def api_charge_retrieve(charge_id, wallet: WalletTypeInfo = Depends(get_key_type)):
charge = await get_charge(charge_id)
if not charge:
return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Charge does not exist."
)
return (
jsonify(
{
**charge._asdict(),
return {
**charge.dict(),
**{"time_elapsed": charge.time_elapsed},
**{"paid": charge.paid},
}
),
HTTPStatus.OK,
)
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["DELETE"])
@api_check_wallet_key("invoice")
async def api_charge_delete(charge_id):
@satspay_ext.delete("/api/v1/charge/{charge_id}")
async def api_charge_delete(charge_id, wallet: WalletTypeInfo = Depends(get_key_type)):
charge = await get_charge(charge_id)
if not charge:
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Charge does not exist."
)
await delete_charge(charge_id)
return "", HTTPStatus.NO_CONTENT
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
#############################BALANCE##########################
@satspay_ext.route("/api/v1/charges/balance/<charge_id>", methods=["GET"])
@satspay_ext.get("/api/v1/charges/balance/{charge_id}")
async def api_charges_balance(charge_id):
charge = await check_address_balance(charge_id)
if not charge:
return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="Charge does not exist."
)
if charge.paid and charge.webhook:
async with httpx.AsyncClient() as client:
try:
@@ -130,28 +122,21 @@ async def api_charges_balance(charge_id):
)
except AssertionError:
charge.webhook = None
return jsonify(charge._asdict()), HTTPStatus.OK
return charge.dict()
#############################MEMPOOL##########################
@satspay_ext.route("/api/v1/mempool", methods=["PUT"])
@api_check_wallet_key("invoice")
@api_validate_post_request(
schema={
"endpoint": {"type": "string", "empty": False, "required": True},
}
)
async def api_update_mempool():
mempool = await update_mempool(user=g.wallet.user, **g.data)
return jsonify(mempool._asdict()), HTTPStatus.OK
@satspay_ext.put("/api/v1/mempool")
async def api_update_mempool(endpoint: str = Query(...), wallet: WalletTypeInfo = Depends(get_key_type)):
mempool = await update_mempool(endpoint, user=wallet.wallet.user)
return mempool.dict()
@satspay_ext.route("/api/v1/mempool", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_get_mempool():
mempool = await get_mempool(g.wallet.user)
@satspay_ext.route("/api/v1/mempool")
async def api_get_mempool(wallet: WalletTypeInfo = Depends(get_key_type)):
mempool = await get_mempool(wallet.wallet.user)
if not mempool:
mempool = await create_mempool(user=g.wallet.user)
return jsonify(mempool._asdict()), HTTPStatus.OK
mempool = await create_mempool(user=wallet.wallet.user)
return mempool.dict()