From 083f7a0a8db5e330b9cf36b2650b6a4647b13a26 Mon Sep 17 00:00:00 2001 From: Eneko Illarramendi Date: Sun, 3 May 2020 15:57:05 +0200 Subject: [PATCH] chore: use standard library's HTTP status codes --- lnbits/core/views/api.py | 26 +++++----- lnbits/core/views/generic.py | 10 ++-- lnbits/core/views/lnurl.py | 8 +-- lnbits/decorators.py | 16 +++--- lnbits/extensions/amilk/views.py | 4 +- lnbits/extensions/amilk/views_api.py | 24 ++++----- lnbits/extensions/diagonalley/views.py | 5 +- lnbits/extensions/diagonalley/views_api.py | 57 ++++++++++------------ lnbits/extensions/example/views_api.py | 4 +- lnbits/extensions/paywall/views.py | 4 +- lnbits/extensions/paywall/views_api.py | 24 ++++----- lnbits/extensions/tpos/views.py | 4 +- lnbits/extensions/tpos/views_api.py | 26 +++++----- lnbits/extensions/usermanager/views.py | 1 - lnbits/extensions/usermanager/views_api.py | 25 +++++----- lnbits/extensions/withdraw/views.py | 6 +-- lnbits/extensions/withdraw/views_api.py | 49 ++++++++++--------- lnbits/helpers.py | 15 ------ 18 files changed, 145 insertions(+), 163 deletions(-) diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index f2728b25a..b6c47b0b7 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -1,8 +1,8 @@ from flask import g, jsonify, request +from http import HTTPStatus from lnbits.core import core_app from lnbits.decorators import api_check_wallet_key, api_validate_post_request -from lnbits.helpers import Status from lnbits.settings import WALLET from ..services import create_invoice, pay_invoice @@ -20,7 +20,7 @@ def api_payments(): else: payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending) - return jsonify(g.wallet.get_payments()), Status.OK + return jsonify(g.wallet.get_payments()), HTTPStatus.OK @api_check_wallet_key("invoice") @@ -36,9 +36,9 @@ def api_payments_create_invoice(): wallet_id=g.wallet.id, amount=g.data["amount"], memo=g.data["memo"] ) except Exception as e: - return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR + return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR - return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.CREATED + return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.CREATED @api_check_wallet_key("admin") @@ -47,13 +47,13 @@ def api_payments_pay_invoice(): try: checking_id = pay_invoice(wallet_id=g.wallet.id, bolt11=g.data["bolt11"]) except ValueError as e: - return jsonify({"message": str(e)}), Status.BAD_REQUEST + return jsonify({"message": str(e)}), HTTPStatus.BAD_REQUEST except PermissionError as e: - return jsonify({"message": str(e)}), Status.FORBIDDEN + return jsonify({"message": str(e)}), HTTPStatus.FORBIDDEN except Exception as e: - return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR + return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR - return jsonify({"checking_id": checking_id}), Status.CREATED + return jsonify({"checking_id": checking_id}), HTTPStatus.CREATED @core_app.route("/api/v1/payments", methods=["POST"]) @@ -70,9 +70,9 @@ def api_payment(checking_id): payment = g.wallet.get_payment(checking_id) if not payment: - return jsonify({"message": "Payment does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Payment does not exist."}), HTTPStatus.NOT_FOUND elif not payment.pending: - return jsonify({"paid": True}), Status.OK + return jsonify({"paid": True}), HTTPStatus.OK try: if payment.is_out: @@ -80,10 +80,10 @@ def api_payment(checking_id): elif payment.is_in: is_paid = not WALLET.get_invoice_status(checking_id).pending except Exception: - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK if is_paid: payment.set_pending(False) - return jsonify({"paid": True}), Status.OK + return jsonify({"paid": True}), HTTPStatus.OK - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK diff --git a/lnbits/core/views/generic.py b/lnbits/core/views/generic.py index d7da5b82e..4e1a9571d 100644 --- a/lnbits/core/views/generic.py +++ b/lnbits/core/views/generic.py @@ -1,9 +1,9 @@ from flask import g, abort, redirect, request, render_template, send_from_directory, url_for +from http import HTTPStatus from os import path from lnbits.core import core_app from lnbits.decorators import check_user_exists, validate_uuids -from lnbits.helpers import Status from lnbits.settings import SERVICE_FEE from ..crud import ( @@ -33,7 +33,7 @@ def extensions(): extension_to_disable = request.args.get("disable", type=str) if extension_to_enable and extension_to_disable: - abort(Status.BAD_REQUEST, "You can either `enable` or `disable` an extension.") + abort(HTTPStatus.BAD_REQUEST, "You can either `enable` or `disable` an extension.") if extension_to_enable: update_user_extension(user_id=g.user.id, extension=extension_to_enable, active=1) @@ -60,7 +60,7 @@ def wallet(): if not user_id: user = get_user(create_account().id) else: - user = get_user(user_id) or abort(Status.NOT_FOUND, "User does not exist.") + user = get_user(user_id) or abort(HTTPStatus.NOT_FOUND, "User does not exist.") if not wallet_id: if user.wallets and not wallet_name: @@ -71,7 +71,7 @@ def wallet(): return redirect(url_for("core.wallet", usr=user.id, wal=wallet.id)) if wallet_id not in user.wallet_ids: - abort(Status.FORBIDDEN, "Not your wallet.") + abort(HTTPStatus.FORBIDDEN, "Not your wallet.") return render_template("core/wallet.html", user=user, wallet=user.get_wallet(wallet_id), service_fee=service_fee) @@ -84,7 +84,7 @@ def deletewallet(): user_wallet_ids = g.user.wallet_ids if wallet_id not in user_wallet_ids: - abort(Status.FORBIDDEN, "Not your wallet.") + abort(HTTPStatus.FORBIDDEN, "Not your wallet.") else: delete_wallet(user_id=g.user.id, wallet_id=wallet_id) user_wallet_ids.remove(wallet_id) diff --git a/lnbits/core/views/lnurl.py b/lnbits/core/views/lnurl.py index 929649e9b..172b11c2c 100644 --- a/lnbits/core/views/lnurl.py +++ b/lnbits/core/views/lnurl.py @@ -1,12 +1,12 @@ import requests from flask import abort, redirect, request, url_for +from http import HTTPStatus from lnurl import LnurlWithdrawResponse, handle as handle_lnurl # type: ignore from lnurl.exceptions import LnurlException # type: ignore from time import sleep from lnbits.core import core_app -from lnbits.helpers import Status from lnbits.settings import WALLET from ..crud import create_account, get_user, create_wallet, create_payment @@ -19,7 +19,7 @@ def lnurlwallet(): try: withdraw_res = handle_lnurl(request.args.get("lightning"), response_class=LnurlWithdrawResponse) except LnurlException: - abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") + abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") try: ok, checking_id, payment_request, error_message = WALLET.create_invoice(withdraw_res.max_sats, memo) @@ -27,7 +27,7 @@ def lnurlwallet(): ok, error_message = False, str(e) if not ok: - abort(Status.INTERNAL_SERVER_ERROR, error_message) + abort(HTTPStatus.INTERNAL_SERVER_ERROR, error_message) r = requests.get( withdraw_res.callback.base, @@ -35,7 +35,7 @@ def lnurlwallet(): ) if not r.ok: - abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") + abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") for i in range(10): invoice_status = WALLET.get_invoice_status(checking_id) diff --git a/lnbits/decorators.py b/lnbits/decorators.py index fd62f0e52..31924f0c2 100644 --- a/lnbits/decorators.py +++ b/lnbits/decorators.py @@ -1,11 +1,11 @@ from cerberus import Validator # type: ignore from flask import g, abort, jsonify, request from functools import wraps +from http import HTTPStatus from typing import List, Union from uuid import UUID from lnbits.core.crud import get_user, get_wallet_for_key -from .helpers import Status def api_check_wallet_key(key_type: str = "invoice"): @@ -15,10 +15,10 @@ def api_check_wallet_key(key_type: str = "invoice"): try: g.wallet = get_wallet_for_key(request.headers["X-Api-Key"], key_type) except KeyError: - return jsonify({"message": "`X-Api-Key` header missing."}), Status.BAD_REQUEST + return jsonify({"message": "`X-Api-Key` header missing."}), HTTPStatus.BAD_REQUEST if not g.wallet: - return jsonify({"message": "Wrong keys."}), Status.UNAUTHORIZED + return jsonify({"message": "Wrong keys."}), HTTPStatus.UNAUTHORIZED return view(**kwargs) @@ -32,13 +32,13 @@ def api_validate_post_request(*, schema: dict): @wraps(view) def wrapped_view(**kwargs): if "application/json" not in request.headers["Content-Type"]: - return jsonify({"message": "Content-Type must be `application/json`."}), Status.BAD_REQUEST + return jsonify({"message": "Content-Type must be `application/json`."}), HTTPStatus.BAD_REQUEST v = Validator(schema) g.data = {key: request.json[key] for key in schema.keys()} if not v.validate(g.data): - return jsonify({"message": f"Errors in request data: {v.errors}"}), Status.BAD_REQUEST + return jsonify({"message": f"Errors in request data: {v.errors}"}), HTTPStatus.BAD_REQUEST return view(**kwargs) @@ -51,7 +51,7 @@ def check_user_exists(param: str = "usr"): def wrap(view): @wraps(view) def wrapped_view(**kwargs): - g.user = get_user(request.args.get(param, type=str)) or abort(Status.NOT_FOUND, "User not found.") + g.user = get_user(request.args.get(param, type=str)) or abort(HTTPStatus.NOT_FOUND, "User not found.") return view(**kwargs) return wrapped_view @@ -67,13 +67,13 @@ def validate_uuids(params: List[str], *, required: Union[bool, List[str]] = Fals for param, value in query_params.items(): if not value and (required is True or (required and param in required)): - abort(Status.BAD_REQUEST, f"`{param}` is required.") + abort(HTTPStatus.BAD_REQUEST, f"`{param}` is required.") if value: try: UUID(value, version=version) except ValueError: - abort(Status.BAD_REQUEST, f"`{param}` is not a valid UUID.") + abort(HTTPStatus.BAD_REQUEST, f"`{param}` is not a valid UUID.") return view(**kwargs) diff --git a/lnbits/extensions/amilk/views.py b/lnbits/extensions/amilk/views.py index 23cd5b141..11c86f0b8 100644 --- a/lnbits/extensions/amilk/views.py +++ b/lnbits/extensions/amilk/views.py @@ -1,8 +1,8 @@ from flask import g, abort, render_template +from http import HTTPStatus from lnbits.decorators import check_user_exists, validate_uuids from lnbits.extensions.amilk import amilk_ext -from lnbits.helpers import Status from .crud import get_amilk @@ -16,6 +16,6 @@ def index(): @amilk_ext.route("/") def wall(amilk_id): - amilk = get_amilk(amilk_id) or abort(Status.NOT_FOUND, "AMilk does not exist.") + amilk = get_amilk(amilk_id) or abort(HTTPStatus.NOT_FOUND, "AMilk does not exist.") return render_template("amilk/wall.html", amilk=amilk) diff --git a/lnbits/extensions/amilk/views_api.py b/lnbits/extensions/amilk/views_api.py index 2aaa29dfb..4b0f96e4c 100644 --- a/lnbits/extensions/amilk/views_api.py +++ b/lnbits/extensions/amilk/views_api.py @@ -1,8 +1,8 @@ from flask import g, jsonify, request +from http import HTTPStatus from lnbits.core.crud import get_user from lnbits.decorators import api_check_wallet_key, api_validate_post_request -from lnbits.helpers import Status from lnbits.extensions.amilk import amilk_ext from .crud import create_amilk, get_amilk, get_amilks, delete_amilk @@ -23,7 +23,7 @@ def api_amilks(): if "all_wallets" in request.args: wallet_ids = get_user(g.wallet.user).wallet_ids - return jsonify([amilk._asdict() for amilk in get_amilks(wallet_ids)]), Status.OK + return jsonify([amilk._asdict() for amilk in get_amilks(wallet_ids)]), HTTPStatus.OK @amilk_ext.route("/api/v1/amilk/milk/", methods=["GET"]) @@ -34,10 +34,10 @@ def api_amilkit(amilk_id): try: withdraw_res = handle_lnurl(milk.lnurl, response_class=LnurlWithdrawResponse) except LnurlException: - abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") + abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") print(withdraw_res.max_sats) - + try: checking_id, payment_request = create_invoice(wallet_id=milk.wallet, amount=withdraw_res.max_sats, memo=memo) #print(payment_request) @@ -48,10 +48,10 @@ def api_amilkit(amilk_id): withdraw_res.callback.base, params={**withdraw_res.callback.query_params, **{"k1": withdraw_res.k1, "pr": payment_request}}, ) - + if not r.ok: - abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") + abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") for i in range(10): invoice_status = WALLET.get_invoice_status(checking_id) @@ -59,10 +59,10 @@ def api_amilkit(amilk_id): if not invoice_status.paid: continue else: - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK break - return jsonify({"paid": True}), Status.OK + return jsonify({"paid": True}), HTTPStatus.OK @amilk_ext.route("/api/v1/amilk", methods=["POST"]) @@ -75,7 +75,7 @@ def api_amilkit(amilk_id): def api_amilk_create(): amilk = create_amilk(wallet_id=g.wallet.id, lnurl=g.data["lnurl"], atime=g.data["atime"], amount=g.data["amount"]) - return jsonify(amilk._asdict()), Status.CREATED + return jsonify(amilk._asdict()), HTTPStatus.CREATED @amilk_ext.route("/api/v1/amilk/", methods=["DELETE"]) @@ -84,11 +84,11 @@ def api_amilk_delete(amilk_id): amilk = get_amilk(amilk_id) if not amilk: - return jsonify({"message": "Paywall does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND if amilk.wallet != g.wallet.id: - return jsonify({"message": "Not your amilk."}), Status.FORBIDDEN + return jsonify({"message": "Not your amilk."}), HTTPStatus.FORBIDDEN delete_amilk(amilk_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT diff --git a/lnbits/extensions/diagonalley/views.py b/lnbits/extensions/diagonalley/views.py index 646fa7054..573c7966c 100644 --- a/lnbits/extensions/diagonalley/views.py +++ b/lnbits/extensions/diagonalley/views.py @@ -1,8 +1,9 @@ -from flask import g, abort, render_template, jsonify import json + +from flask import g, abort, render_template, jsonify + from lnbits.decorators import check_user_exists, validate_uuids from lnbits.extensions.diagonalley import diagonalley_ext -from lnbits.helpers import Status from lnbits.db import open_ext_db diff --git a/lnbits/extensions/diagonalley/views_api.py b/lnbits/extensions/diagonalley/views_api.py index 5e43a6dbe..c551ac7a7 100644 --- a/lnbits/extensions/diagonalley/views_api.py +++ b/lnbits/extensions/diagonalley/views_api.py @@ -1,8 +1,8 @@ from flask import g, jsonify, request +from http import HTTPStatus from lnbits.core.crud import get_user from lnbits.decorators import api_check_wallet_key, api_validate_post_request -from lnbits.helpers import Status from lnbits.extensions.diagonalley import diagonalley_ext from .crud import create_diagonalleys_product,get_diagonalleys_product,get_diagonalleys_products,delete_diagonalleys_product,create_diagonalleys_indexer,update_diagonalleys_indexer,get_diagonalleys_indexer,get_diagonalleys_indexers,delete_diagonalleys_indexer,create_diagonalleys_order,get_diagonalleys_order,get_diagonalleys_orders,delete_diagonalleys_order @@ -21,7 +21,7 @@ def api_diagonalley_products(): if "all_wallets" in request.args: wallet_ids = get_user(g.wallet.user).wallet_ids - return jsonify([product._asdict() for product in get_diagonalleys_products(wallet_ids)]), Status.OK + return jsonify([product._asdict() for product in get_diagonalleys_products(wallet_ids)]), HTTPStatus.OK @diagonalley_ext.route("/api/v1/diagonalley/products", methods=["POST"]) @@ -41,16 +41,16 @@ def api_diagonalley_product_create(product_id=None): product = get_diagonalleys_indexer(product_id) if not product: - return jsonify({"message": "Withdraw product does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Withdraw product does not exist."}), HTTPStatus.NOT_FOUND if product.wallet != g.wallet.id: - return jsonify({"message": "Not your withdraw product."}), Status.FORBIDDEN + return jsonify({"message": "Not your withdraw product."}), HTTPStatus.FORBIDDEN product = update_diagonalleys_product(product_id, **g.data) else: product = create_diagonalleys_product(wallet_id=g.wallet.id, **g.data) - return jsonify(product._asdict()), Status.OK if product_id else Status.CREATED + return jsonify(product._asdict()), HTTPStatus.OK if product_id else HTTPStatus.CREATED @@ -60,14 +60,14 @@ def api_diagonalley_products_delete(product_id): product = get_diagonalleys_product(product_id) if not product: - return jsonify({"message": "Product does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Product does not exist."}), HTTPStatus.NOT_FOUND if product.wallet != g.wallet.id: - return jsonify({"message": "Not your Diagon Alley."}), Status.FORBIDDEN + return jsonify({"message": "Not your Diagon Alley."}), HTTPStatus.FORBIDDEN delete_diagonalleys_product(product_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT @@ -81,7 +81,7 @@ def api_diagonalley_indexers(): if "all_wallets" in request.args: wallet_ids = get_user(g.wallet.user).wallet_ids - return jsonify([indexer._asdict() for indexer in get_diagonalleys_indexers(wallet_ids)]), Status.OK + return jsonify([indexer._asdict() for indexer in get_diagonalleys_indexers(wallet_ids)]), HTTPStatus.OK @diagonalley_ext.route("/api/v1/diagonalley/indexers", methods=["POST"]) @@ -102,16 +102,16 @@ def api_diagonalley_indexer_create(indexer_id=None): indexer = get_diagonalleys_indexer(indexer_id) if not indexer: - return jsonify({"message": "Withdraw indexer does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Withdraw indexer does not exist."}), HTTPStatus.NOT_FOUND if indexer.wallet != g.wallet.id: - return jsonify({"message": "Not your withdraw indexer."}), Status.FORBIDDEN + return jsonify({"message": "Not your withdraw indexer."}), HTTPStatus.FORBIDDEN indexer = update_diagonalleys_indexer(indexer_id, **g.data) else: indexer = create_diagonalleys_indexer(wallet_id=g.wallet.id, **g.data) - return jsonify(indexer._asdict()), Status.OK if indexer_id else Status.CREATED + return jsonify(indexer._asdict()), HTTPStatus.OK if indexer_id else HTTPStatus.CREATED @diagonalley_ext.route("/api/v1/diagonalley/indexers/", methods=["DELETE"]) @@ -120,14 +120,14 @@ def api_diagonalley_indexer_delete(indexer_id): indexer = get_diagonalleys_indexer(indexer_id) if not indexer: - return jsonify({"message": "Indexer does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND if indexer.wallet != g.wallet.id: - return jsonify({"message": "Not your Indexer."}), Status.FORBIDDEN + return jsonify({"message": "Not your Indexer."}), HTTPStatus.FORBIDDEN delete_diagonalleys_indexer(indexer_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT ###Orders @@ -140,7 +140,7 @@ def api_diagonalley_orders(): if "all_wallets" in request.args: wallet_ids = get_user(g.wallet.user).wallet_ids - return jsonify([order._asdict() for order in get_diagonalleys_orders(wallet_ids)]), Status.OK + return jsonify([order._asdict() for order in get_diagonalleys_orders(wallet_ids)]), HTTPStatus.OK @diagonalley_ext.route("/api/v1/diagonalley/orders", methods=["POST"]) @@ -154,7 +154,7 @@ def api_diagonalley_orders(): }) def api_diagonalley_order_create(): order = create_diagonalleys_order(wallet_id=g.wallet.id, **g.data) - return jsonify(order._asdict()), Status.CREATED + return jsonify(order._asdict()), HTTPStatus.CREATED @diagonalley_ext.route("/api/v1/diagonalley/orders/", methods=["DELETE"]) @@ -163,14 +163,14 @@ def api_diagonalley_order_delete(order_id): order = get_diagonalleys_order(order_id) if not order: - return jsonify({"message": "Indexer does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND if order.wallet != g.wallet.id: - return jsonify({"message": "Not your Indexer."}), Status.FORBIDDEN + return jsonify({"message": "Not your Indexer."}), HTTPStatus.FORBIDDEN delete_diagonalleys_indexer(order_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT @diagonalley_ext.route("/api/v1/diagonalley/orders/paid/", methods=["GET"]) @@ -178,7 +178,7 @@ def api_diagonalley_order_delete(order_id): def api_diagonalleys_order_paid(order_id): with open_ext_db("diagonalley") as db: db.execute("UPDATE orders SET paid = ? WHERE id = ?", (True, order_id,)) - return "", Status.OK + return "", HTTPStatus.OK @diagonalley_ext.route("/api/v1/diagonalley/orders/shipped/", methods=["GET"]) @@ -188,7 +188,7 @@ def api_diagonalleys_order_shipped(order_id): db.execute("UPDATE orders SET shipped = ? WHERE id = ?", (True, order_id,)) order = db.fetchone("SELECT * FROM orders WHERE id = ?", (order_id,)) - return jsonify([order._asdict() for order in get_diagonalleys_orders(order["wallet"])]), Status.OK + return jsonify([order._asdict() for order in get_diagonalleys_orders(order["wallet"])]), HTTPStatus.OK ###List products based on indexer id @@ -199,13 +199,13 @@ def api_diagonalleys_stall_products(indexer_id): rows = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,)) print(rows[1]) if not rows: - return jsonify({"message": "Indexer does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND products = db.fetchone("SELECT * FROM products WHERE wallet = ?", (rows[1],)) if not products: - return jsonify({"message": "No products"}), Status.NOT_FOUND + return jsonify({"message": "No products"}), HTTPStatus.NOT_FOUND - return jsonify([products._asdict() for products in get_diagonalleys_products(rows[1])]), Status.OK + return jsonify([products._asdict() for products in get_diagonalleys_products(rows[1])]), HTTPStatus.OK ###Check a product has been shipped @@ -214,7 +214,7 @@ def api_diagonalleys_stall_checkshipped(checking_id): with open_ext_db("diagonalley") as db: rows = db.fetchone("SELECT * FROM orders WHERE invoiceid = ?", (checking_id,)) - return jsonify({"shipped": rows["shipped"]}), Status.OK + return jsonify({"shipped": rows["shipped"]}), HTTPStatus.OK ###Place order @@ -245,7 +245,4 @@ def api_diagonalley_stall_order(indexer_id): """, (selling_id ,g.data["id"] , product.wallet, product.product, g.data["quantity"], g.data["shippingzone"], g.data["address"], g.data["email"], checking_id, False, False), ) - return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.OK - - - + return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK diff --git a/lnbits/extensions/example/views_api.py b/lnbits/extensions/example/views_api.py index 03aca16b7..7de52fd1f 100644 --- a/lnbits/extensions/example/views_api.py +++ b/lnbits/extensions/example/views_api.py @@ -6,8 +6,8 @@ # import requests from flask import jsonify +from http import HTTPStatus -from lnbits.helpers import Status from lnbits.extensions.example import example_ext @@ -34,4 +34,4 @@ def api_example(): } ] - return jsonify(tools), Status.OK + return jsonify(tools), HTTPStatus.OK diff --git a/lnbits/extensions/paywall/views.py b/lnbits/extensions/paywall/views.py index 3849ce720..82b5d5cfc 100644 --- a/lnbits/extensions/paywall/views.py +++ b/lnbits/extensions/paywall/views.py @@ -1,7 +1,7 @@ from flask import g, abort, render_template +from http import HTTPStatus from lnbits.decorators import check_user_exists, validate_uuids -from lnbits.helpers import Status from lnbits.extensions.paywall import paywall_ext from .crud import get_paywall @@ -16,6 +16,6 @@ def index(): @paywall_ext.route("/") def display(paywall_id): - paywall = get_paywall(paywall_id) or abort(Status.NOT_FOUND, "Paywall does not exist.") + paywall = get_paywall(paywall_id) or abort(HTTPStatus.NOT_FOUND, "Paywall does not exist.") return render_template("paywall/display.html", paywall=paywall) diff --git a/lnbits/extensions/paywall/views_api.py b/lnbits/extensions/paywall/views_api.py index 0ff32f602..decad300c 100644 --- a/lnbits/extensions/paywall/views_api.py +++ b/lnbits/extensions/paywall/views_api.py @@ -1,9 +1,9 @@ from flask import g, jsonify, request +from http import HTTPStatus from lnbits.core.crud import get_user, get_wallet from lnbits.core.services import create_invoice from lnbits.decorators import api_check_wallet_key, api_validate_post_request -from lnbits.helpers import Status from lnbits.settings import WALLET from lnbits.extensions.paywall import paywall_ext @@ -18,7 +18,7 @@ def api_paywalls(): if "all_wallets" in request.args: wallet_ids = get_user(g.wallet.user).wallet_ids - return jsonify([paywall._asdict() for paywall in get_paywalls(wallet_ids)]), Status.OK + return jsonify([paywall._asdict() for paywall in get_paywalls(wallet_ids)]), HTTPStatus.OK @paywall_ext.route("/api/v1/paywalls", methods=["POST"]) @@ -33,7 +33,7 @@ def api_paywalls(): def api_paywall_create(): paywall = create_paywall(wallet_id=g.wallet.id, **g.data) - return jsonify(paywall._asdict()), Status.CREATED + return jsonify(paywall._asdict()), HTTPStatus.CREATED @paywall_ext.route("/api/v1/paywalls/", methods=["DELETE"]) @@ -42,14 +42,14 @@ def api_paywall_delete(paywall_id): paywall = get_paywall(paywall_id) if not paywall: - return jsonify({"message": "Paywall does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND if paywall.wallet != g.wallet.id: - return jsonify({"message": "Not your paywall."}), Status.FORBIDDEN + return jsonify({"message": "Not your paywall."}), HTTPStatus.FORBIDDEN delete_paywall(paywall_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT @paywall_ext.route("/api/v1/paywalls//invoice", methods=["GET"]) @@ -61,9 +61,9 @@ def api_paywall_get_invoice(paywall_id): wallet_id=paywall.wallet, amount=paywall.amount, memo=f"#paywall {paywall.memo}" ) except Exception as e: - return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR + return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR - return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.OK + return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK @paywall_ext.route("/api/v1/paywalls//check_invoice", methods=["POST"]) @@ -72,18 +72,18 @@ def api_paywal_check_invoice(paywall_id): paywall = get_paywall(paywall_id) if not paywall: - return jsonify({"message": "Paywall does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND try: is_paid = not WALLET.get_invoice_status(g.data["checking_id"]).pending except Exception: - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK if is_paid: wallet = get_wallet(paywall.wallet) payment = wallet.get_payment(g.data["checking_id"]) payment.set_pending(False) - return jsonify({"paid": True, "url": paywall.url}), Status.OK + return jsonify({"paid": True, "url": paywall.url}), HTTPStatus.OK - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK diff --git a/lnbits/extensions/tpos/views.py b/lnbits/extensions/tpos/views.py index 5edf1d31f..2d1ea6e9e 100644 --- a/lnbits/extensions/tpos/views.py +++ b/lnbits/extensions/tpos/views.py @@ -1,7 +1,7 @@ from flask import g, abort, render_template +from http import HTTPStatus from lnbits.decorators import check_user_exists, validate_uuids -from lnbits.helpers import Status from lnbits.extensions.tpos import tpos_ext from .crud import get_tpos @@ -16,6 +16,6 @@ def index(): @tpos_ext.route("/") def tpos(tpos_id): - tpos = get_tpos(tpos_id) or abort(Status.NOT_FOUND, "TPoS does not exist.") + tpos = get_tpos(tpos_id) or abort(HTTPStatus.NOT_FOUND, "TPoS does not exist.") return render_template("tpos/tpos.html", tpos=tpos) diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index 745d42033..c273ab155 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -1,9 +1,9 @@ from flask import g, jsonify, request +from http import HTTPStatus from lnbits.core.crud import get_user, get_wallet from lnbits.core.services import create_invoice from lnbits.decorators import api_check_wallet_key, api_validate_post_request -from lnbits.helpers import Status from lnbits.settings import WALLET from lnbits.extensions.tpos import tpos_ext @@ -18,7 +18,7 @@ def api_tposs(): if "all_wallets" in request.args: wallet_ids = get_user(g.wallet.user).wallet_ids - return jsonify([tpos._asdict() for tpos in get_tposs(wallet_ids)]), Status.OK + return jsonify([tpos._asdict() for tpos in get_tposs(wallet_ids)]), HTTPStatus.OK @tpos_ext.route("/api/v1/tposs", methods=["POST"]) @@ -32,7 +32,7 @@ def api_tposs(): def api_tpos_create(): tpos = create_tpos(wallet_id=g.wallet.id, **g.data) - return jsonify(tpos._asdict()), Status.CREATED + return jsonify(tpos._asdict()), HTTPStatus.CREATED @tpos_ext.route("/api/v1/tposs/", methods=["DELETE"]) @@ -41,14 +41,14 @@ def api_tpos_delete(tpos_id): tpos = get_tpos(tpos_id) if not tpos: - return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND + return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND if tpos.wallet != g.wallet.id: - return jsonify({"message": "Not your TPoS."}), Status.FORBIDDEN + return jsonify({"message": "Not your TPoS."}), HTTPStatus.FORBIDDEN delete_tpos(tpos_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT @tpos_ext.route("/api/v1/tposs//invoices/", methods=["POST"]) @@ -57,16 +57,16 @@ def api_tpos_create_invoice(tpos_id): tpos = get_tpos(tpos_id) if not tpos: - return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND + return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND try: checking_id, payment_request = create_invoice( wallet_id=tpos.wallet, amount=g.data["amount"], memo=f"#tpos {tpos.name}" ) except Exception as e: - return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR + return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR - return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.CREATED + return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.CREATED @tpos_ext.route("/api/v1/tposs//invoices/", methods=["GET"]) @@ -74,18 +74,18 @@ def api_tpos_check_invoice(tpos_id, checking_id): tpos = get_tpos(tpos_id) if not tpos: - return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND + return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND try: is_paid = not WALLET.get_invoice_status(checking_id).pending except Exception: - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK if is_paid: wallet = get_wallet(tpos.wallet) payment = wallet.get_payment(checking_id) payment.set_pending(False) - return jsonify({"paid": True}), Status.OK + return jsonify({"paid": True}), HTTPStatus.OK - return jsonify({"paid": False}), Status.OK + return jsonify({"paid": False}), HTTPStatus.OK diff --git a/lnbits/extensions/usermanager/views.py b/lnbits/extensions/usermanager/views.py index 625d7d3f6..a4de11118 100644 --- a/lnbits/extensions/usermanager/views.py +++ b/lnbits/extensions/usermanager/views.py @@ -2,7 +2,6 @@ from flask import g, abort, render_template, jsonify import json from lnbits.decorators import check_user_exists, validate_uuids from lnbits.extensions.usermanager import usermanager_ext -from lnbits.helpers import Status from lnbits.db import open_ext_db diff --git a/lnbits/extensions/usermanager/views_api.py b/lnbits/extensions/usermanager/views_api.py index d845ec2d2..d64c54d74 100644 --- a/lnbits/extensions/usermanager/views_api.py +++ b/lnbits/extensions/usermanager/views_api.py @@ -1,8 +1,8 @@ from flask import g, jsonify, request +from http import HTTPStatus from lnbits.core.crud import get_user from lnbits.decorators import api_check_wallet_key, api_validate_post_request -from lnbits.helpers import Status from lnbits.extensions.usermanager import usermanager_ext from .crud import create_usermanager_user, get_usermanager_user, get_usermanager_users, get_usermanager_wallet_transactions, get_usermanager_wallet_balances, delete_usermanager_user, create_usermanager_wallet, get_usermanager_wallet, get_usermanager_wallets, delete_usermanager_wallet @@ -19,7 +19,7 @@ from lnbits.db import open_ext_db @api_check_wallet_key(key_type="invoice") def api_usermanager_users(): user_id = g.wallet.user - return jsonify([user._asdict() for user in get_usermanager_users(user_id)]), Status.OK + return jsonify([user._asdict() for user in get_usermanager_users(user_id)]), HTTPStatus.OK @usermanager_ext.route("/api/v1/users", methods=["POST"]) @@ -31,7 +31,7 @@ def api_usermanager_users(): }) def api_usermanager_users_create(): user = create_usermanager_user(g.data["user_name"], g.data["wallet_name"], g.data["admin_id"]) - return jsonify(user._asdict()), Status.CREATED + return jsonify(user._asdict()), HTTPStatus.CREATED @usermanager_ext.route("/api/v1/users/", methods=["DELETE"]) @@ -40,9 +40,9 @@ def api_usermanager_users_delete(user_id): print("cunt") user = get_usermanager_user(user_id) if not user: - return jsonify({"message": "User does not exist."}), Status.NOT_FOUND + return jsonify({"message": "User does not exist."}), HTTPStatus.NOT_FOUND delete_usermanager_user(user_id) - return "", Status.NO_CONTENT + return "", HTTPStatus.NO_CONTENT ###Wallets @@ -51,7 +51,7 @@ def api_usermanager_users_delete(user_id): @api_check_wallet_key(key_type="invoice") def api_usermanager_wallets(): user_id = g.wallet.user - return jsonify([wallet._asdict() for wallet in get_usermanager_wallets(user_id)]), Status.OK + return jsonify([wallet._asdict() for wallet in get_usermanager_wallets(user_id)]), HTTPStatus.OK @usermanager_ext.route("/api/v1/wallets", methods=["POST"]) @@ -60,23 +60,23 @@ def api_usermanager_wallets(): "user_id": {"type": "string", "empty": False, "required": True}, "wallet_name": {"type": "string", "empty": False, "required": True}, "admin_id": {"type": "string", "empty": False, "required": True} - + }) def api_usermanager_wallets_create(): user = create_usermanager_wallet(g.data["user_id"], g.data["wallet_name"], g.data["admin_id"]) - return jsonify(user._asdict()), Status.CREATED + return jsonify(user._asdict()), HTTPStatus.CREATED @usermanager_ext.route("/api/v1/wallets", methods=["GET"]) @api_check_wallet_key(key_type="invoice") def api_usermanager_wallet_transactions(wallet_id): - return jsonify(get_usermanager_wallet_transactions(wallet_id)), Status.OK + return jsonify(get_usermanager_wallet_transactions(wallet_id)), HTTPStatus.OK @usermanager_ext.route("/api/v1/wallets/", methods=["GET"]) @api_check_wallet_key(key_type="invoice") def api_usermanager_wallet_balances(user_id): - return jsonify(get_usermanager_wallet_balances(user_id)), Status.OK + return jsonify(get_usermanager_wallet_balances(user_id)), HTTPStatus.OK @usermanager_ext.route("/api/v1/wallets/", methods=["DELETE"]) @@ -85,9 +85,8 @@ def api_usermanager_wallets_delete(wallet_id): wallet = get_usermanager_wallet(wallet_id) print(wallet.id) if not wallet: - return jsonify({"message": "Wallet does not exist."}), Status.NOT_FOUND + return jsonify({"message": "Wallet does not exist."}), HTTPStatus.NOT_FOUND delete_usermanager_wallet(wallet_id, wallet.user) - return "", Status.NO_CONTENT - + return "", HTTPStatus.NO_CONTENT diff --git a/lnbits/extensions/withdraw/views.py b/lnbits/extensions/withdraw/views.py index 5c9bc4345..e7c61601e 100644 --- a/lnbits/extensions/withdraw/views.py +++ b/lnbits/extensions/withdraw/views.py @@ -1,7 +1,7 @@ from flask import g, abort, render_template +from http import HTTPStatus from lnbits.decorators import check_user_exists, validate_uuids -from lnbits.helpers import Status from lnbits.extensions.withdraw import withdraw_ext from .crud import get_withdraw_link @@ -16,13 +16,13 @@ def index(): @withdraw_ext.route("/") def display(link_id): - link = get_withdraw_link(link_id) or abort(Status.NOT_FOUND, "Withdraw link does not exist.") + link = get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.") return render_template("withdraw/display.html", link=link) @withdraw_ext.route("/print/") def print_qr(link_id): - link = get_withdraw_link(link_id) or abort(Status.NOT_FOUND, "Withdraw link does not exist.") + link = get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.") return render_template("withdraw/print_qr.html", link=link) diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index 219976a1c..57d0359f3 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -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/", 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/", 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/", 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 diff --git a/lnbits/helpers.py b/lnbits/helpers.py index daa5dbd2c..62d8d587d 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -46,20 +46,5 @@ class ExtensionManager: return output -class Status: - OK = 200 - CREATED = 201 - NO_CONTENT = 204 - BAD_REQUEST = 400 - UNAUTHORIZED = 401 - PAYMENT_REQUIRED = 402 - FORBIDDEN = 403 - NOT_FOUND = 404 - METHOD_NOT_ALLOWED = 405 - UPGRADE_REQUIRED = 426 - TOO_MANY_REQUESTS = 429 - INTERNAL_SERVER_ERROR = 500 - - def urlsafe_short_hash() -> str: return shortuuid.uuid()