lnbits-legend/lnbits/extensions/satspay/views_api.py

122 lines
3.5 KiB
Python
Raw Normal View History

2021-04-04 18:20:52 +01:00
import hashlib
from quart import g, jsonify, url_for
from http import HTTPStatus
import httpx
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.extensions.satspay import satspay_ext
from .crud import (
create_charge,
2021-04-05 01:48:35 +01:00
update_charge,
2021-04-04 18:20:52 +01:00
get_charge,
get_charges,
delete_charge,
check_address_balance,
)
#############################CHARGES##########################
2021-04-05 00:10:54 +01:00
@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", "empty": False, "required": True},
"time": {"type": "integer", "min": 1, "required": True},
"amount": {"type": "integer", "min": 1, "required": True},
}
)
async def api_charge_create_or_update(charge_id=None):
if not charge_id:
charge = await create_charge(user=g.wallet.user, **g.data)
return jsonify(charge._asdict()), HTTPStatus.CREATED
2021-04-04 18:20:52 +01:00
else:
2021-04-05 01:48:35 +01:00
charge = await update_charge(charge_id=charge_id, **g.data)
2021-04-04 18:20:52 +01:00
return jsonify(charge._asdict()), HTTPStatus.OK
2021-04-05 00:10:54 +01:00
2021-04-04 18:20:52 +01:00
@satspay_ext.route("/api/v1/charges", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_charges_retrieve():
charges = await get_charges(g.wallet.user)
if not charges:
return (
jsonify(""),
HTTPStatus.OK
)
else:
return jsonify([charge._asdict() for charge in charges]), HTTPStatus.OK
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_charge_retrieve(charge_id):
2021-04-05 01:48:35 +01:00
charge = await get_charge(charge_id)
print(charge)
2021-04-05 00:10:54 +01:00
2021-04-04 18:20:52 +01:00
if not charge:
return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND
2021-04-05 01:48:35 +01:00
return jsonify(charge._asdict()), HTTPStatus.OK
2021-04-04 18:20:52 +01:00
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["DELETE"])
@api_check_wallet_key("invoice")
async def api_charge_delete(charge_id):
charge = await get_watch_wallet(charge_id)
if not charge:
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
await delete_watch_wallet(charge_id)
return "", HTTPStatus.NO_CONTENT
2021-04-05 00:10:54 +01:00
#############################BALANCE##########################
@satspay_ext.route("/api/v1/charges/balance/<charge_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_charges_balance(charge_id):
charge = await check_address_balance(charge_id)
if not charge:
return (
jsonify(""),
HTTPStatus.OK
)
else:
return jsonify(charge._asdict()), HTTPStatus.OK
2021-04-04 18:20:52 +01:00
#############################MEMPOOL##########################
2021-04-05 00:10:54 +01:00
2021-04-04 18:20:52 +01:00
@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)
2021-04-05 00:10:54 +01:00
return jsonify(mempool._asdict()), HTTPStatus.OK
2021-04-04 18:20:52 +01:00
@satspay_ext.route("/api/v1/mempool", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_get_mempool():
2021-04-05 00:10:54 +01:00
mempool = await get_mempool(g.wallet.user)
2021-04-04 18:20:52 +01:00
if not mempool:
mempool = await create_mempool(user=g.wallet.user)
2021-04-05 00:10:54 +01:00
return jsonify(mempool._asdict()), HTTPStatus.OK