views converted
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
from quart import g, jsonify, request
|
||||
from http import HTTPStatus
|
||||
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
|
||||
|
||||
from http import HTTPStatus
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||
|
||||
from lnbits.decorators import api_validate_post_request
|
||||
from .models import CreateSatsDiceLink, CreateSatsDiceWithdraws
|
||||
from . import satsdice_ext
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.params import Depends
|
||||
from .crud import (
|
||||
create_satsdice_pay,
|
||||
get_satsdice_pay,
|
||||
@@ -19,105 +22,105 @@ from .crud import (
|
||||
delete_satsdice_withdraw,
|
||||
create_withdraw_hash_check,
|
||||
)
|
||||
from lnbits.decorators import (
|
||||
check_user_exists,
|
||||
WalletTypeInfo,
|
||||
get_key_type,
|
||||
api_validate_post_request,
|
||||
)
|
||||
|
||||
################LNURL pay
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/links", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_links():
|
||||
wallet_ids = [g.wallet.id]
|
||||
@satsdice_ext.get("/api/v1/links")
|
||||
async def api_links(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
|
||||
try:
|
||||
return (
|
||||
jsonify(
|
||||
[
|
||||
{**link._asdict(), **{"lnurl": link.lnurl}}
|
||||
for link in await get_satsdice_pays(wallet_ids)
|
||||
]
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
return [
|
||||
{**link._dict(), **{"lnurl": link.lnurl}}
|
||||
for link in await get_satsdice_pays(wallet_ids)
|
||||
]
|
||||
except LnurlInvalidUrl:
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
||||
}
|
||||
),
|
||||
HTTPStatus.UPGRADE_REQUIRED,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.UPGRADE_REQUIRED,
|
||||
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
|
||||
)
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/links/<link_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_link_retrieve(link_id):
|
||||
@satsdice_ext.get("/api/v1/links/{link_id}")
|
||||
async def api_link_retrieve(data: CreateSatsDiceLink, link_id: str = Query(None)):
|
||||
link = await get_satsdice_pay(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Pay link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Pay link does not exist.",
|
||||
)
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your pay link."}), HTTPStatus.FORBIDDEN
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Not your pay link.",
|
||||
)
|
||||
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK
|
||||
return {**link._asdict(), **{"lnurl": link.lnurl}}
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/links", methods=["POST"])
|
||||
@satsdice_ext.route("/api/v1/links/<link_id>", methods=["PUT"])
|
||||
@api_check_wallet_key("invoice")
|
||||
@api_validate_post_request(
|
||||
schema={
|
||||
"title": {"type": "string", "empty": False, "required": True},
|
||||
"base_url": {"type": "string", "empty": False, "required": True},
|
||||
"min_bet": {"type": "number", "required": True},
|
||||
"max_bet": {"type": "number", "required": True},
|
||||
"multiplier": {"type": "number", "required": True},
|
||||
"chance": {"type": "float", "required": True},
|
||||
"haircut": {"type": "number", "required": True},
|
||||
}
|
||||
)
|
||||
async def api_link_create_or_update(link_id=None):
|
||||
if g.data["min_bet"] > g.data["max_bet"]:
|
||||
return jsonify({"message": "Min is greater than max."}), HTTPStatus.BAD_REQUEST
|
||||
@satsdice_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
|
||||
@satsdice_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
||||
async def api_link_create_or_update(
|
||||
data: CreateSatsDiceLink,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
link_id: str = Query(None),
|
||||
):
|
||||
if data.min_bet > data.max_bet:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail="Bad request",
|
||||
)
|
||||
if link_id:
|
||||
link = await get_satsdice_pay(link_id)
|
||||
|
||||
if not link:
|
||||
return (
|
||||
jsonify({"message": "Satsdice does not exist."}),
|
||||
HTTPStatus.NOT_FOUND,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Satsdice does not exist",
|
||||
)
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return (
|
||||
jsonify({"message": "Come on, seriously, this isn't your satsdice!"}),
|
||||
HTTPStatus.FORBIDDEN,
|
||||
if link.wallet != wallet.wallet.id:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Come on, seriously, this isn't your satsdice!",
|
||||
)
|
||||
|
||||
link = await update_satsdice_pay(link_id, **g.data)
|
||||
link = await update_satsdice_pay(data, link_id)
|
||||
else:
|
||||
link = await create_satsdice_pay(wallet_id=g.wallet.id, **g.data)
|
||||
link = await create_satsdice_pay(data, wallet_id=wallet.wallet.id)
|
||||
|
||||
return (
|
||||
jsonify({**link._asdict(), **{"lnurl": link.lnurl}}),
|
||||
HTTPStatus.OK if link_id else HTTPStatus.CREATED,
|
||||
)
|
||||
return {**link.dict(), **{"lnurl": link.lnurl}}
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/links/<link_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_link_delete(link_id):
|
||||
@satsdice_ext.delete("/api/v1/links/{link_id}")
|
||||
async def api_link_delete(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type), link_id: str = Query(None)
|
||||
):
|
||||
link = await get_satsdice_pay(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Pay link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Pay link does not exist.",
|
||||
)
|
||||
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your pay link."}), HTTPStatus.FORBIDDEN
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Not your pay link.",
|
||||
)
|
||||
|
||||
await delete_satsdice_pay(link_id)
|
||||
|
||||
@@ -127,13 +130,12 @@ async def api_link_delete(link_id):
|
||||
##########LNURL withdraw
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/withdraws", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_withdraws():
|
||||
wallet_ids = [g.wallet.id]
|
||||
@satsdice_ext.get("/api/v1/withdraws")
|
||||
async def api_withdraws(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
try:
|
||||
return (
|
||||
jsonify(
|
||||
@@ -148,58 +150,44 @@ async def api_withdraws():
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
except LnurlInvalidUrl:
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
||||
}
|
||||
),
|
||||
HTTPStatus.UPGRADE_REQUIRED,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.UPGRADE_REQUIRED,
|
||||
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
|
||||
)
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/withdraws/<withdraw_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_withdraw_retrieve(withdraw_id):
|
||||
@satsdice_ext.get("/api/v1/withdraws/{withdraw_id}")
|
||||
async def api_withdraw_retrieve(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type), withdraw_id: str = Query(None)
|
||||
):
|
||||
withdraw = await get_satsdice_withdraw(withdraw_id, 0)
|
||||
|
||||
if not withdraw:
|
||||
return (
|
||||
jsonify({"message": "satsdice withdraw does not exist."}),
|
||||
HTTPStatus.NOT_FOUND,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="satsdice withdraw does not exist.",
|
||||
)
|
||||
|
||||
if withdraw.wallet != g.wallet.id:
|
||||
return (
|
||||
jsonify({"message": "Not your satsdice withdraw."}),
|
||||
HTTPStatus.FORBIDDEN,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Not your satsdice withdraw.",
|
||||
)
|
||||
|
||||
return jsonify({**withdraw._asdict(), **{"lnurl": withdraw.lnurl}}), HTTPStatus.OK
|
||||
return {**withdraw._asdict(), **{"lnurl": withdraw.lnurl}}, HTTPStatus.OK
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/withdraws", methods=["POST"])
|
||||
@satsdice_ext.route("/api/v1/withdraws/<withdraw_id>", methods=["PUT"])
|
||||
@api_check_wallet_key("admin")
|
||||
@api_validate_post_request(
|
||||
schema={
|
||||
"title": {"type": "string", "empty": False, "required": True},
|
||||
"min_satsdiceable": {"type": "integer", "min": 1, "required": True},
|
||||
"max_satsdiceable": {"type": "integer", "min": 1, "required": True},
|
||||
"uses": {"type": "integer", "min": 1, "required": True},
|
||||
"wait_time": {"type": "integer", "min": 1, "required": True},
|
||||
"is_unique": {"type": "boolean", "required": True},
|
||||
}
|
||||
)
|
||||
async def api_withdraw_create_or_update(withdraw_id=None):
|
||||
if g.data["max_satsdiceable"] < g.data["min_satsdiceable"]:
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"message": "`max_satsdiceable` needs to be at least `min_satsdiceable`."
|
||||
}
|
||||
),
|
||||
HTTPStatus.BAD_REQUEST,
|
||||
@satsdice_ext.post("/api/v1/withdraws", status_code=HTTPStatus.CREATED)
|
||||
@satsdice_ext.put("/api/v1/withdraws/{withdraw_id}", status_code=HTTPStatus.OK)
|
||||
async def api_withdraw_create_or_update(
|
||||
data: CreateSatsDiceWithdraws,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
withdraw_id: str = Query(None),
|
||||
):
|
||||
if data.max_satsdiceable < data.min_satsdiceable:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail="`max_satsdiceable` needs to be at least `min_satsdiceable`.",
|
||||
)
|
||||
|
||||
usescsv = ""
|
||||
@@ -213,15 +201,16 @@ async def api_withdraw_create_or_update(withdraw_id=None):
|
||||
if withdraw_id:
|
||||
withdraw = await get_satsdice_withdraw(withdraw_id, 0)
|
||||
if not withdraw:
|
||||
return (
|
||||
jsonify({"message": "satsdice withdraw does not exist."}),
|
||||
HTTPStatus.NOT_FOUND,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="satsdice withdraw does not exist.",
|
||||
)
|
||||
if withdraw.wallet != g.wallet.id:
|
||||
return (
|
||||
jsonify({"message": "Not your satsdice withdraw."}),
|
||||
HTTPStatus.FORBIDDEN,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Not your satsdice withdraw.",
|
||||
)
|
||||
|
||||
withdraw = await update_satsdice_withdraw(
|
||||
withdraw_id, **g.data, usescsv=usescsv, used=0
|
||||
)
|
||||
@@ -230,27 +219,27 @@ async def api_withdraw_create_or_update(withdraw_id=None):
|
||||
wallet_id=g.wallet.id, **g.data, usescsv=usescsv
|
||||
)
|
||||
|
||||
return (
|
||||
jsonify({**withdraw._asdict(), **{"lnurl": withdraw.lnurl}}),
|
||||
HTTPStatus.OK if withdraw_id else HTTPStatus.CREATED,
|
||||
)
|
||||
return {**withdraw._asdict(), **{"lnurl": withdraw.lnurl}}
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/withdraws/<withdraw_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("admin")
|
||||
async def api_withdraw_delete(withdraw_id):
|
||||
@satsdice_ext.delete("/api/v1/withdraws/{withdraw_id}")
|
||||
async def api_withdraw_delete(
|
||||
data: CreateSatsDiceWithdraws,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
withdraw_id: str = Query(None),
|
||||
):
|
||||
withdraw = await get_satsdice_withdraw(withdraw_id)
|
||||
|
||||
if not withdraw:
|
||||
return (
|
||||
jsonify({"message": "satsdice withdraw does not exist."}),
|
||||
HTTPStatus.NOT_FOUND,
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="satsdice withdraw does not exist.",
|
||||
)
|
||||
|
||||
if withdraw.wallet != g.wallet.id:
|
||||
return (
|
||||
jsonify({"message": "Not your satsdice withdraw."}),
|
||||
HTTPStatus.FORBIDDEN,
|
||||
if withdraw.wallet != wallet.wallet.id:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Not your satsdice withdraw.",
|
||||
)
|
||||
|
||||
await delete_satsdice_withdraw(withdraw_id)
|
||||
@@ -258,8 +247,11 @@ async def api_withdraw_delete(withdraw_id):
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
|
||||
@satsdice_ext.route("/api/v1/withdraws/<the_hash>/<lnurl_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_withdraw_hash_retrieve(the_hash, lnurl_id):
|
||||
@satsdice_ext.get("/api/v1/withdraws/{the_hash}/{lnurl_id}")
|
||||
async def api_withdraw_hash_retrieve(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
lnurl_id: str = Query(None),
|
||||
the_hash: str = Query(None),
|
||||
):
|
||||
hashCheck = await get_withdraw_hash_check(the_hash, lnurl_id)
|
||||
return jsonify(hashCheck), HTTPStatus.OK
|
||||
return hashCheck
|
||||
|
||||
Reference in New Issue
Block a user