mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-10 12:32:34 +02:00
satspay started;views_api refactored
This commit is contained in:
@@ -3,6 +3,10 @@ from quart import g, jsonify, url_for
|
|||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Query
|
||||||
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from lnbits.core.crud import get_user
|
from lnbits.core.crud import get_user
|
||||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||||
@@ -19,37 +23,46 @@ from .crud import (
|
|||||||
|
|
||||||
#############################CHARGES##########################
|
#############################CHARGES##########################
|
||||||
|
|
||||||
|
class CreateData(BaseModel):
|
||||||
|
onchainwallet: str
|
||||||
|
lnbitswallet: str
|
||||||
|
description: str = Query(...)
|
||||||
|
webhook: str
|
||||||
|
completelink: str
|
||||||
|
completelinktext: str
|
||||||
|
time: int = Query(..., ge=1)
|
||||||
|
amount: int = Query(..., ge=1)
|
||||||
|
|
||||||
@satspay_ext.route("/api/v1/charge", methods=["POST"])
|
@satspay_ext.post("/api/v1/charge")
|
||||||
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["PUT"])
|
@satspay_ext.put("/api/v1/charge/{charge_id}")
|
||||||
@api_check_wallet_key("admin")
|
@api_check_wallet_key("admin")
|
||||||
@api_validate_post_request(
|
# @api_validate_post_request(
|
||||||
schema={
|
# schema={
|
||||||
"onchainwallet": {"type": "string"},
|
# "onchainwallet": {"type": "string"},
|
||||||
"lnbitswallet": {"type": "string"},
|
# "lnbitswallet": {"type": "string"},
|
||||||
"description": {"type": "string", "empty": False, "required": True},
|
# "description": {"type": "string", "empty": False, "required": True},
|
||||||
"webhook": {"type": "string"},
|
# "webhook": {"type": "string"},
|
||||||
"completelink": {"type": "string"},
|
# "completelink": {"type": "string"},
|
||||||
"completelinktext": {"type": "string"},
|
# "completelinktext": {"type": "string"},
|
||||||
"time": {"type": "integer", "min": 1, "required": True},
|
# "time": {"type": "integer", "min": 1, "required": True},
|
||||||
"amount": {"type": "integer", "min": 1, "required": True},
|
# "amount": {"type": "integer", "min": 1, "required": True},
|
||||||
}
|
# }
|
||||||
)
|
# )
|
||||||
async def api_charge_create_or_update(charge_id=None):
|
async def api_charge_create_or_update(data: CreateData, charge_id=None):
|
||||||
if not charge_id:
|
if not charge_id:
|
||||||
charge = await create_charge(user=g.wallet.user, **g.data)
|
charge = await create_charge(user=g.wallet.user, **data)
|
||||||
return jsonify(charge._asdict()), HTTPStatus.CREATED
|
return charge._asdict(), HTTPStatus.CREATED
|
||||||
else:
|
else:
|
||||||
charge = await update_charge(charge_id=charge_id, **g.data)
|
charge = await update_charge(charge_id=charge_id, **data)
|
||||||
return jsonify(charge._asdict()), HTTPStatus.OK
|
return charge._asdict(), HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
@satspay_ext.route("/api/v1/charges", methods=["GET"])
|
@satspay_ext.get("/api/v1/charges")
|
||||||
@api_check_wallet_key("invoice")
|
@api_check_wallet_key("invoice")
|
||||||
async def api_charges_retrieve():
|
async def api_charges_retrieve():
|
||||||
try:
|
try:
|
||||||
return (
|
return (
|
||||||
jsonify(
|
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
**charge._asdict(),
|
**charge._asdict(),
|
||||||
@@ -58,40 +71,40 @@ async def api_charges_retrieve():
|
|||||||
}
|
}
|
||||||
for charge in await get_charges(g.wallet.user)
|
for charge in await get_charges(g.wallet.user)
|
||||||
]
|
]
|
||||||
),
|
,
|
||||||
HTTPStatus.OK,
|
HTTPStatus.OK,
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["GET"])
|
@satspay_ext.get("/api/v1/charge/{charge_id}")
|
||||||
@api_check_wallet_key("invoice")
|
@api_check_wallet_key("invoice")
|
||||||
async def api_charge_retrieve(charge_id):
|
async def api_charge_retrieve(charge_id: str):
|
||||||
charge = await get_charge(charge_id)
|
charge = await get_charge(charge_id)
|
||||||
|
|
||||||
if not charge:
|
if not charge:
|
||||||
return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND
|
return {"message": "charge does not exist"}, HTTPStatus.NOT_FOUND
|
||||||
|
|
||||||
return (
|
return (
|
||||||
jsonify(
|
|
||||||
{
|
{
|
||||||
**charge._asdict(),
|
**charge._asdict(),
|
||||||
**{"time_elapsed": charge.time_elapsed},
|
**{"time_elapsed": charge.time_elapsed},
|
||||||
**{"paid": charge.paid},
|
**{"paid": charge.paid},
|
||||||
}
|
}
|
||||||
),
|
,
|
||||||
HTTPStatus.OK,
|
HTTPStatus.OK,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["DELETE"])
|
@satspay_ext.delete("/api/v1/charge/{charge_id}")
|
||||||
@api_check_wallet_key("invoice")
|
@api_check_wallet_key("invoice")
|
||||||
async def api_charge_delete(charge_id):
|
async def api_charge_delete(charge_id: str):
|
||||||
charge = await get_charge(charge_id)
|
charge = await get_charge(charge_id)
|
||||||
|
|
||||||
if not charge:
|
if not charge:
|
||||||
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
|
return {"message": "Wallet link does not exist."}, HTTPStatus.NOT_FOUND
|
||||||
|
|
||||||
await delete_charge(charge_id)
|
await delete_charge(charge_id)
|
||||||
|
|
||||||
@@ -101,13 +114,13 @@ async def api_charge_delete(charge_id):
|
|||||||
#############################BALANCE##########################
|
#############################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):
|
async def api_charges_balance(charge_id: str):
|
||||||
|
|
||||||
charge = await check_address_balance(charge_id)
|
charge = await check_address_balance(charge_id)
|
||||||
|
|
||||||
if not charge:
|
if not charge:
|
||||||
return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND
|
return {"message": "charge does not exist"}, HTTPStatus.NOT_FOUND
|
||||||
if charge.paid and charge.webhook:
|
if charge.paid and charge.webhook:
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
try:
|
try:
|
||||||
@@ -130,28 +143,28 @@ async def api_charges_balance(charge_id):
|
|||||||
)
|
)
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
charge.webhook = None
|
charge.webhook = None
|
||||||
return jsonify(charge._asdict()), HTTPStatus.OK
|
return charge._asdict(), HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
#############################MEMPOOL##########################
|
#############################MEMPOOL##########################
|
||||||
|
|
||||||
|
|
||||||
@satspay_ext.route("/api/v1/mempool", methods=["PUT"])
|
@satspay_ext.put("/api/v1/mempool")
|
||||||
@api_check_wallet_key("invoice")
|
@api_check_wallet_key("invoice")
|
||||||
@api_validate_post_request(
|
# @api_validate_post_request(
|
||||||
schema={
|
# schema={
|
||||||
"endpoint": {"type": "string", "empty": False, "required": True},
|
# "endpoint": {"type": "string", "empty": False, "required": True},
|
||||||
}
|
# }
|
||||||
)
|
# )
|
||||||
async def api_update_mempool():
|
async def api_update_mempool(endpoint: str = Query(...)):
|
||||||
mempool = await update_mempool(user=g.wallet.user, **g.data)
|
mempool = await update_mempool(user=g.wallet.user, endpoint)
|
||||||
return jsonify(mempool._asdict()), HTTPStatus.OK
|
return mempool._asdict(), HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
@satspay_ext.route("/api/v1/mempool", methods=["GET"])
|
@satspay_ext.get("/api/v1/mempool")
|
||||||
@api_check_wallet_key("invoice")
|
@api_check_wallet_key("invoice")
|
||||||
async def api_get_mempool():
|
async def api_get_mempool():
|
||||||
mempool = await get_mempool(g.wallet.user)
|
mempool = await get_mempool(g.wallet.user)
|
||||||
if not mempool:
|
if not mempool:
|
||||||
mempool = await create_mempool(user=g.wallet.user)
|
mempool = await create_mempool(user=g.wallet.user)
|
||||||
return jsonify(mempool._asdict()), HTTPStatus.OK
|
return mempool._asdict(), HTTPStatus.OK
|
||||||
|
Reference in New Issue
Block a user