All extensions semi-switched
This commit is contained in:
@@ -15,13 +15,12 @@ from .crud import (
|
||||
delete_pay_link,
|
||||
)
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/currencies", methods=["GET"])
|
||||
@lnurlp_ext.get("/api/v1/currencies")
|
||||
async def api_list_currencies_available():
|
||||
return jsonify(list(currencies.keys()))
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/links", methods=["GET"])
|
||||
@lnurlp_ext.get("/api/v1/links")
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_links():
|
||||
wallet_ids = [g.wallet.id]
|
||||
@@ -31,66 +30,59 @@ async def api_links():
|
||||
|
||||
try:
|
||||
return (
|
||||
jsonify(
|
||||
[
|
||||
{**link._asdict(), **{"lnurl": link.lnurl}}
|
||||
for link in await get_pay_links(wallet_ids)
|
||||
]
|
||||
),
|
||||
],
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
except LnurlInvalidUrl:
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
||||
}
|
||||
),
|
||||
},
|
||||
HTTPStatus.UPGRADE_REQUIRED,
|
||||
)
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/links/<link_id>", methods=["GET"])
|
||||
@lnurlp_ext.get("/api/v1/links/<link_id>")
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_link_retrieve(link_id):
|
||||
link = await get_pay_link(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Pay link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
return {"message": "Pay link does not exist."}, HTTPStatus.NOT_FOUND
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your pay link."}), HTTPStatus.FORBIDDEN
|
||||
return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
|
||||
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK
|
||||
return {**link._asdict(), **{"lnurl": link.lnurl}}, HTTPStatus.OK
|
||||
|
||||
class CreateData(BaseModel):
|
||||
description: str
|
||||
min: int = Query(ge=0.01)
|
||||
max: int = Query(ge=0.01)
|
||||
currency: Optional[str]
|
||||
comment_chars: int = Query(ge=0, lt=800)
|
||||
webhook_url: Optional[str]
|
||||
success_text: Optional[str]
|
||||
success_url: Optional[str]
|
||||
|
||||
@lnurlp_ext.route("/api/v1/links", methods=["POST"])
|
||||
@lnurlp_ext.route("/api/v1/links/<link_id>", methods=["PUT"])
|
||||
@lnurlp_ext.post("/api/v1/links")
|
||||
@lnurlp_ext.put("/api/v1/links/<link_id>")
|
||||
@api_check_wallet_key("invoice")
|
||||
@api_validate_post_request(
|
||||
schema={
|
||||
"description": {"type": "string", "empty": False, "required": True},
|
||||
"min": {"type": "number", "min": 0.01, "required": True},
|
||||
"max": {"type": "number", "min": 0.01, "required": True},
|
||||
"currency": {"type": "string", "nullable": True, "required": False},
|
||||
"comment_chars": {"type": "integer", "required": True, "min": 0, "max": 800},
|
||||
"webhook_url": {"type": "string", "required": False},
|
||||
"success_text": {"type": "string", "required": False},
|
||||
"success_url": {"type": "string", "required": False},
|
||||
}
|
||||
)
|
||||
async def api_link_create_or_update(link_id=None):
|
||||
if g.data["min"] > g.data["max"]:
|
||||
return jsonify({"message": "Min is greater than max."}), HTTPStatus.BAD_REQUEST
|
||||
async def api_link_create_or_update(data: CreateData, link_id=None):
|
||||
if data.min > data.max:
|
||||
return {"message": "Min is greater than max."}, HTTPStatus.BAD_REQUEST
|
||||
|
||||
if g.data.get("currency") == None and (
|
||||
round(g.data["min"]) != g.data["min"] or round(g.data["max"]) != g.data["max"]
|
||||
if data.currency == None and (
|
||||
round(data.min) != data.min or round(data.max) != data.max
|
||||
):
|
||||
return jsonify({"message": "Must use full satoshis."}), HTTPStatus.BAD_REQUEST
|
||||
return {"message": "Must use full satoshis."}, HTTPStatus.BAD_REQUEST
|
||||
|
||||
if "success_url" in g.data and g.data["success_url"][:8] != "https://":
|
||||
if "success_url" in data and data.success_url[:8] != "https://":
|
||||
return (
|
||||
jsonify({"message": "Success URL must be secure https://..."}),
|
||||
{"message": "Success URL must be secure https://..."},
|
||||
HTTPStatus.BAD_REQUEST,
|
||||
)
|
||||
|
||||
@@ -99,44 +91,44 @@ async def api_link_create_or_update(link_id=None):
|
||||
|
||||
if not link:
|
||||
return (
|
||||
jsonify({"message": "Pay link does not exist."}),
|
||||
{"message": "Pay link does not exist."},
|
||||
HTTPStatus.NOT_FOUND,
|
||||
)
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your pay link."}), HTTPStatus.FORBIDDEN
|
||||
return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
|
||||
|
||||
link = await update_pay_link(link_id, **g.data)
|
||||
link = await update_pay_link(link_id, **data)
|
||||
else:
|
||||
link = await create_pay_link(wallet_id=g.wallet.id, **g.data)
|
||||
link = await create_pay_link(wallet_id=g.wallet.id, **data)
|
||||
|
||||
return (
|
||||
jsonify({**link._asdict(), **{"lnurl": link.lnurl}}),
|
||||
{**link._asdict(), **{"lnurl": link.lnurl}},
|
||||
HTTPStatus.OK if link_id else HTTPStatus.CREATED,
|
||||
)
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/links/<link_id>", methods=["DELETE"])
|
||||
@lnurlp_ext.delete("/api/v1/links/<link_id>")
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_link_delete(link_id):
|
||||
link = await get_pay_link(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Pay link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
return {"message": "Pay link does not exist."}, HTTPStatus.NOT_FOUND
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your pay link."}), HTTPStatus.FORBIDDEN
|
||||
return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
|
||||
|
||||
await delete_pay_link(link_id)
|
||||
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
|
||||
@lnurlp_ext.route("/api/v1/rate/<currency>", methods=["GET"])
|
||||
@lnurlp_ext.get("/api/v1/rate/<currency>")
|
||||
async def api_check_fiat_rate(currency):
|
||||
try:
|
||||
rate = await get_fiat_rate_satoshis(currency)
|
||||
except AssertionError:
|
||||
rate = None
|
||||
|
||||
return jsonify({"rate": rate}), HTTPStatus.OK
|
||||
return {"rate": rate}, HTTPStatus.OK
|
||||
|
||||
Reference in New Issue
Block a user