chore: use standard library's HTTP status codes
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
from datetime import datetime
|
||||
from flask import g, jsonify, request
|
||||
from http import HTTPStatus
|
||||
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
|
||||
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.core.services import pay_invoice
|
||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||
from lnbits.helpers import urlsafe_short_hash, Status
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
|
||||
from lnbits.extensions.withdraw import withdraw_ext
|
||||
from .crud import (
|
||||
@@ -29,12 +30,12 @@ def api_links():
|
||||
try:
|
||||
return (
|
||||
jsonify([{**link._asdict(), **{"lnurl": link.lnurl}} for link in get_withdraw_links(wallet_ids)]),
|
||||
Status.OK,
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
except LnurlInvalidUrl:
|
||||
return (
|
||||
jsonify({"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."}),
|
||||
Status.UPGRADE_REQUIRED,
|
||||
HTTPStatus.UPGRADE_REQUIRED,
|
||||
)
|
||||
|
||||
|
||||
@@ -44,12 +45,12 @@ def api_link_retrieve(link_id):
|
||||
link = get_withdraw_link(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Withdraw link does not exist."}), Status.NOT_FOUND
|
||||
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your withdraw link."}), Status.FORBIDDEN
|
||||
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), Status.OK
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK
|
||||
|
||||
|
||||
@withdraw_ext.route("/api/v1/links", methods=["POST"])
|
||||
@@ -67,25 +68,25 @@ def api_link_retrieve(link_id):
|
||||
)
|
||||
def api_link_create_or_update(link_id=None):
|
||||
if g.data["max_withdrawable"] < g.data["min_withdrawable"]:
|
||||
return jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), Status.BAD_REQUEST
|
||||
return jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), HTTPStatus.BAD_REQUEST
|
||||
|
||||
if (g.data["max_withdrawable"] * g.data["uses"] * 1000) > g.wallet.balance_msat:
|
||||
return jsonify({"message": "Insufficient balance."}), Status.FORBIDDEN
|
||||
return jsonify({"message": "Insufficient balance."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
if link_id:
|
||||
link = get_withdraw_link(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Withdraw link does not exist."}), Status.NOT_FOUND
|
||||
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your withdraw link."}), Status.FORBIDDEN
|
||||
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
link = update_withdraw_link(link_id, **g.data)
|
||||
else:
|
||||
link = create_withdraw_link(wallet_id=g.wallet.id, **g.data)
|
||||
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), Status.OK if link_id else Status.CREATED
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK if link_id else HTTPStatus.CREATED
|
||||
|
||||
|
||||
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["DELETE"])
|
||||
@@ -94,14 +95,14 @@ def api_link_delete(link_id):
|
||||
link = get_withdraw_link(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Withdraw link does not exist."}), Status.NOT_FOUND
|
||||
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your withdraw link."}), Status.FORBIDDEN
|
||||
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
delete_withdraw_link(link_id)
|
||||
|
||||
return "", Status.NO_CONTENT
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
|
||||
@withdraw_ext.route("/api/v1/lnurl/<unique_hash>", methods=["GET"])
|
||||
@@ -109,11 +110,11 @@ def api_lnurl_response(unique_hash):
|
||||
link = get_withdraw_link_by_hash(unique_hash)
|
||||
|
||||
if not link:
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
|
||||
|
||||
link = update_withdraw_link(link.id, k1=urlsafe_short_hash())
|
||||
|
||||
return jsonify(link.lnurl_response.dict()), Status.OK
|
||||
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK
|
||||
|
||||
|
||||
@withdraw_ext.route("/api/v1/lnurl/cb/<unique_hash>", methods=["GET"])
|
||||
@@ -124,16 +125,16 @@ def api_lnurl_callback(unique_hash):
|
||||
now = int(datetime.now().timestamp())
|
||||
|
||||
if not link:
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
|
||||
|
||||
if link.is_spent:
|
||||
return jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), HTTPStatus.OK
|
||||
|
||||
if link.k1 != k1:
|
||||
return jsonify({"status": "ERROR", "reason": "Bad request."}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": "Bad request."}), HTTPStatus.OK
|
||||
|
||||
if now < link.open_time:
|
||||
return jsonify({"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}), HTTPStatus.OK
|
||||
|
||||
try:
|
||||
pay_invoice(wallet_id=link.wallet, bolt11=payment_request, max_sat=link.max_withdrawable)
|
||||
@@ -149,10 +150,10 @@ def api_lnurl_callback(unique_hash):
|
||||
update_withdraw_link(link.id, **changes)
|
||||
|
||||
except ValueError as e:
|
||||
return jsonify({"status": "ERROR", "reason": str(e)}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK
|
||||
except PermissionError:
|
||||
return jsonify({"status": "ERROR", "reason": "Withdraw link is empty."}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": "Withdraw link is empty."}), HTTPStatus.OK
|
||||
except Exception as e:
|
||||
return jsonify({"status": "ERROR", "reason": str(e)}), Status.OK
|
||||
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK
|
||||
|
||||
return jsonify({"status": "OK"}), Status.OK
|
||||
return jsonify({"status": "OK"}), HTTPStatus.OK
|
||||
|
||||
Reference in New Issue
Block a user