diff --git a/lnbits/extensions/jukebox/views.py b/lnbits/extensions/jukebox/views.py index f439110a0..b0769cfdd 100644 --- a/lnbits/extensions/jukebox/views.py +++ b/lnbits/extensions/jukebox/views.py @@ -12,14 +12,14 @@ from .crud import get_jukebox from .views_api import api_get_jukebox_device_check -@jukebox_ext.route("/") +@jukebox_ext.get("/") @validate_uuids(["usr"], required=True) @check_user_exists() async def index(): return await render_template("jukebox/index.html", user=g.user) -@jukebox_ext.route("/") +@jukebox_ext.get("/") async def connect_to_jukebox(juke_id): jukebox = await get_jukebox(juke_id) if not jukebox: diff --git a/lnbits/extensions/jukebox/views_api.py b/lnbits/extensions/jukebox/views_api.py index 317134b15..4f303a664 100644 --- a/lnbits/extensions/jukebox/views_api.py +++ b/lnbits/extensions/jukebox/views_api.py @@ -46,7 +46,7 @@ async def api_check_credentials_callbac(juke_id): jukebox = await get_jukebox(juke_id) except: return ( - jsonable_encoder({"error": "No Jukebox"}), + {"error": "No Jukebox"}, HTTPStatus.FORBIDDEN, ) if request.args.get("code"): @@ -207,13 +207,13 @@ async def api_get_token(juke_id): ######CHECK DEVICE -@jukebox_ext.route("/api/v1/jukebox/jb/", methods=["GET"]) +@jukebox_ext.get("/api/v1/jukebox/jb/") async def api_get_jukebox_device_check(juke_id, retry=False): try: jukebox = await get_jukebox(juke_id) except: return ( - jsonable_encoder({"error": "No Jukebox"}), + {"error": "No Jukebox"}, HTTPStatus.FORBIDDEN, ) async with httpx.AsyncClient() as client: @@ -252,13 +252,13 @@ async def api_get_jukebox_device_check(juke_id, retry=False): ######GET INVOICE STUFF -@jukebox_ext.route("/api/v1/jukebox/jb/invoice//", methods=["GET"]) +@jukebox_ext.get("/api/v1/jukebox/jb/invoice//") async def api_get_jukebox_invoice(juke_id, song_id): try: jukebox = await get_jukebox(juke_id) except: return ( - jsonable_encoder({"error": "No Jukebox"}), + {"error": "No Jukebox"}, HTTPStatus.FORBIDDEN, ) try: @@ -270,12 +270,12 @@ async def api_get_jukebox_invoice(juke_id, song_id): deviceConnected = True if not deviceConnected: return ( - jsonable_encoder({"error": "No device connected"}), + {"error": "No device connected"}, HTTPStatus.NOT_FOUND, ) except: return ( - jsonable_encoder({"error": "No device connected"}), + {"error": "No device connected"}, HTTPStatus.NOT_FOUND, ) @@ -299,25 +299,25 @@ async def api_get_jukebox_invoice_check(pay_hash, juke_id): jukebox = await get_jukebox(juke_id) except: return ( - jsonable_encoder({"error": "No Jukebox"}), + {"error": "No Jukebox"}, HTTPStatus.FORBIDDEN, ) try: status = await check_invoice_status(jukebox.wallet, pay_hash) is_paid = not status.pending except Exception as exc: - return jsonable_encoder({"paid": False}), HTTPStatus.OK + return {"paid": False}, HTTPStatus.OK if is_paid: wallet = await get_wallet(jukebox.wallet) payment = await wallet.get_payment(pay_hash) await payment.set_pending(False) await update_jukebox_payment(pay_hash, paid=True) - return jsonable_encoder({"paid": True}), HTTPStatus.OK - return jsonable_encoder({"paid": False}), HTTPStatus.OK + return {"paid": True}, HTTPStatus.OK + return {"paid": False}, HTTPStatus.OK -@jukebox_ext.route( - "/api/v1/jukebox/jb/invoicep///", methods=["GET"] +@jukebox_ext.get( + "/api/v1/jukebox/jb/invoicep///" ) async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False): try: @@ -356,17 +356,17 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False): headers={"Authorization": "Bearer " + jukebox.sp_access_token}, ) if r.status_code == 204: - return jsonify(jukebox_payment), HTTPStatus.OK + return jukebox_payment, HTTPStatus.OK elif r.status_code == 401 or r.status_code == 403: token = await api_get_token(juke_id) if token == False: return ( - jsonify({"error": "Invoice not paid"}), + {"error": "Invoice not paid"}, HTTPStatus.FORBIDDEN, ) elif retry: return ( - jsonify({"error": "Failed to get auth"}), + {"error": "Failed to get auth"}, HTTPStatus.FORBIDDEN, ) else: @@ -375,7 +375,7 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False): ) else: return ( - jsonify({"error": "Invoice not paid"}), + {"error": "Invoice not paid"}, HTTPStatus.FORBIDDEN, ) elif r.status_code == 200: @@ -389,18 +389,18 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False): headers={"Authorization": "Bearer " + jukebox.sp_access_token}, ) if r.status_code == 204: - return jsonify(jukebox_payment), HTTPStatus.OK + return jukebox_payment, HTTPStatus.OK elif r.status_code == 401 or r.status_code == 403: token = await api_get_token(juke_id) if token == False: return ( - jsonify({"error": "Invoice not paid"}), + {"error": "Invoice not paid"}, HTTPStatus.OK, ) elif retry: return ( - jsonify({"error": "Failed to get auth"}), + {"error": "Failed to get auth"}, HTTPStatus.FORBIDDEN, ) else: @@ -409,38 +409,38 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False): ) else: return ( - jsonify({"error": "Invoice not paid"}), + {"error": "Invoice not paid"}, HTTPStatus.OK, ) elif r.status_code == 401 or r.status_code == 403: token = await api_get_token(juke_id) if token == False: return ( - jsonify({"error": "Invoice not paid"}), + {"error": "Invoice not paid"}, HTTPStatus.OK, ) elif retry: return ( - jsonify({"error": "Failed to get auth"}), + {"error": "Failed to get auth"}, HTTPStatus.FORBIDDEN, ) else: return await api_get_jukebox_invoice_paid( song_id, juke_id, pay_hash ) - return jsonify({"error": "Invoice not paid"}), HTTPStatus.OK + return {"error": "Invoice not paid"}, HTTPStatus.OK ############################GET TRACKS -@jukebox_ext.route("/api/v1/jukebox/jb/currently/", methods=["GET"]) +@jukebox_ext.get("/api/v1/jukebox/jb/currently/") async def api_get_jukebox_currently(juke_id, retry=False): try: jukebox = await get_jukebox(juke_id) except: return ( - jsonify({"error": "No Jukebox"}), + {"error": "No Jukebox"}, HTTPStatus.FORBIDDEN, ) async with httpx.AsyncClient() as client: @@ -451,7 +451,7 @@ async def api_get_jukebox_currently(juke_id, retry=False): headers={"Authorization": "Bearer " + jukebox.sp_access_token}, ) if r.status_code == 204: - return jsonify({"error": "Nothing"}), HTTPStatus.OK + return {"error": "Nothing"}, HTTPStatus.OK elif r.status_code == 200: try: response = r.json() @@ -463,25 +463,25 @@ async def api_get_jukebox_currently(juke_id, retry=False): "artist": response["item"]["artists"][0]["name"], "image": response["item"]["album"]["images"][0]["url"], } - return jsonify(track), HTTPStatus.OK + return track, HTTPStatus.OK except: - return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND + return "Something went wrong", HTTPStatus.NOT_FOUND elif r.status_code == 401: token = await api_get_token(juke_id) if token == False: return ( - jsonify({"error": "Invoice not paid"}), + {"error": "Invoice not paid"}, HTTPStatus.FORBIDDEN, ) elif retry: return ( - jsonify({"error": "Failed to get auth"}), + {"error": "Failed to get auth"}, HTTPStatus.FORBIDDEN, ) else: return await api_get_jukebox_currently(juke_id, retry=True) else: - return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND + return "Something went wrong", HTTPStatus.NOT_FOUND except AssertionError: - return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND + return "Something went wrong", HTTPStatus.NOT_FOUND diff --git a/lnbits/extensions/watchonly/views.py b/lnbits/extensions/watchonly/views.py index e82469680..f0c38712b 100644 --- a/lnbits/extensions/watchonly/views.py +++ b/lnbits/extensions/watchonly/views.py @@ -6,17 +6,9 @@ from lnbits.decorators import check_user_exists, validate_uuids from . import watchonly_ext -@watchonly_ext.route("/") +@watchonly_ext.get("/") @validate_uuids(["usr"], required=True) @check_user_exists() async def index(): return await render_template("watchonly/index.html", user=g.user) - -@watchonly_ext.route("/") -async def display(charge_id): - link = get_payment(charge_id) or abort( - HTTPStatus.NOT_FOUND, "Charge link does not exist." - ) - - return await render_template("watchonly/display.html", link=link) diff --git a/lnbits/extensions/watchonly/views_api.py b/lnbits/extensions/watchonly/views_api.py index 108ef2a9b..24a269bf8 100644 --- a/lnbits/extensions/watchonly/views_api.py +++ b/lnbits/extensions/watchonly/views_api.py @@ -37,71 +37,65 @@ async def api_wallets_retrieve(): return "" -@watchonly_ext.route("/api/v1/wallet/", methods=["GET"]) +@watchonly_ext.get("/api/v1/wallet/") @api_check_wallet_key("invoice") async def api_wallet_retrieve(wallet_id): wallet = await get_watch_wallet(wallet_id) if not wallet: - return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND + return {"message": "wallet does not exist"}, HTTPStatus.NOT_FOUND - return jsonify(wallet._asdict()), HTTPStatus.OK + return wallet._asdict(), HTTPStatus.OK -@watchonly_ext.route("/api/v1/wallet", methods=["POST"]) +@watchonly_ext.post("/api/v1/wallet") @api_check_wallet_key("admin") -@api_validate_post_request( - schema={ - "masterpub": {"type": "string", "empty": False, "required": True}, - "title": {"type": "string", "empty": False, "required": True}, - } -) -async def api_wallet_create_or_update(wallet_id=None): +async def api_wallet_create_or_update(masterPub: str, Title: str, wallet_id=None): try: wallet = await create_watch_wallet( - user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"] + user=g.wallet.user, masterpub=masterPub, title=Title ) except Exception as e: - return jsonify({"message": str(e)}), HTTPStatus.BAD_REQUEST + return {"message": str(e)}, HTTPStatus.BAD_REQUEST mempool = await get_mempool(g.wallet.user) if not mempool: create_mempool(user=g.wallet.user) - return jsonify(wallet._asdict()), HTTPStatus.CREATED + return wallet._asdict(), HTTPStatus.CREATED -@watchonly_ext.route("/api/v1/wallet/", methods=["DELETE"]) +@watchonly_ext.delete("/api/v1/wallet/") @api_check_wallet_key("admin") async def api_wallet_delete(wallet_id): wallet = await get_watch_wallet(wallet_id) if not wallet: - return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND + return {"message": "Wallet link does not exist."}, HTTPStatus.NOT_FOUND await delete_watch_wallet(wallet_id) - return jsonify({"deleted": "true"}), HTTPStatus.NO_CONTENT + return {"deleted": "true"}, HTTPStatus.NO_CONTENT #############################ADDRESSES########################## -@watchonly_ext.route("/api/v1/address/", methods=["GET"]) +@watchonly_ext.get("/api/v1/address/") @api_check_wallet_key("invoice") async def api_fresh_address(wallet_id): await get_fresh_address(wallet_id) addresses = await get_addresses(wallet_id) - return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK + return [address._asdict() for address in addresses], HTTPStatus.OK -@watchonly_ext.route("/api/v1/addresses/", methods=["GET"]) +@watchonly_ext.get("/api/v1/addresses/") @api_check_wallet_key("invoice") async def api_get_addresses(wallet_id): wallet = await get_watch_wallet(wallet_id) if not wallet: - return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND + return {"message": "wallet does not exist"}, HTTPStatus.NOT_FOUND addresses = await get_addresses(wallet_id) @@ -109,28 +103,23 @@ async def api_get_addresses(wallet_id): await get_fresh_address(wallet_id) addresses = await get_addresses(wallet_id) - return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK + return [address._asdict() for address in addresses], HTTPStatus.OK #############################MEMPOOL########################## -@watchonly_ext.route("/api/v1/mempool", methods=["PUT"]) +@watchonly_ext.put("/api/v1/mempool") @api_check_wallet_key("admin") -@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 +async def api_update_mempool(endpoint: str): + mempool = await update_mempool(user=g.wallet.user, **endpoint) + return mempool._asdict(), HTTPStatus.OK -@watchonly_ext.route("/api/v1/mempool", methods=["GET"]) +@watchonly_ext.get("/api/v1/mempool") @api_check_wallet_key("admin") async def api_get_mempool(): mempool = await get_mempool(g.wallet.user) if not mempool: mempool = await create_mempool(user=g.wallet.user) - return jsonify(mempool._asdict()), HTTPStatus.OK + return mempool._asdict(), HTTPStatus.OK diff --git a/lnbits/extensions/withdraw/views.py b/lnbits/extensions/withdraw/views.py index 28f25756f..1f1f6e6bb 100644 --- a/lnbits/extensions/withdraw/views.py +++ b/lnbits/extensions/withdraw/views.py @@ -8,14 +8,14 @@ from . import withdraw_ext from .crud import get_withdraw_link, chunks -@withdraw_ext.route("/") +@withdraw_ext.get("/") @validate_uuids(["usr"], required=True) @check_user_exists() async def index(): return await render_template("withdraw/index.html", user=g.user) -@withdraw_ext.route("/") +@withdraw_ext.get("/") async def display(link_id): link = await get_withdraw_link(link_id, 0) or abort( HTTPStatus.NOT_FOUND, "Withdraw link does not exist." @@ -23,7 +23,7 @@ async def display(link_id): return await render_template("withdraw/display.html", link=link, unique=True) -@withdraw_ext.route("/img/") +@withdraw_ext.get("/img/") async def img(link_id): link = await get_withdraw_link(link_id, 0) or abort( HTTPStatus.NOT_FOUND, "Withdraw link does not exist." @@ -43,7 +43,7 @@ async def img(link_id): ) -@withdraw_ext.route("/print/") +@withdraw_ext.get("/print/") async def print_qr(link_id): link = await get_withdraw_link(link_id) or abort( HTTPStatus.NOT_FOUND, "Withdraw link does not exist."