lnbits-legend/lnbits/extensions/satsdice/views_api.py

130 lines
3.7 KiB
Python
Raw Normal View History

2021-10-13 17:08:48 +01:00
from http import HTTPStatus
2021-10-18 16:23:51 +01:00
from fastapi import Depends, Query, Request
2021-10-13 17:08:48 +01:00
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
2021-10-13 21:51:21 +01:00
from starlette.exceptions import HTTPException
2021-10-18 16:23:51 +01:00
2021-10-13 17:08:48 +01:00
from lnbits.core.crud import get_user
from lnbits.decorators import WalletTypeInfo, get_key_type
2021-10-18 16:23:51 +01:00
2021-10-13 17:08:48 +01:00
from . import satsdice_ext
from .crud import (
create_satsdice_pay,
2021-10-18 16:23:51 +01:00
delete_satsdice_pay,
2021-10-13 17:08:48 +01:00
get_satsdice_pay,
get_satsdice_pays,
get_withdraw_hash_checkw,
2021-10-18 16:23:51 +01:00
update_satsdice_pay,
2021-10-13 21:51:21 +01:00
)
from .models import CreateSatsDiceLink
2021-10-13 17:08:48 +01:00
################LNURL pay
2021-10-13 21:51:21 +01:00
@satsdice_ext.get("/api/v1/links")
2021-10-15 14:28:41 +01:00
async def api_links(
request: Request,
wallet: WalletTypeInfo = Depends(get_key_type),
2021-10-25 11:54:58 +01:00
all_wallets: bool = Query(False),
2021-10-15 14:28:41 +01:00
):
2021-10-13 21:51:21 +01:00
wallet_ids = [wallet.wallet.id]
2021-10-13 17:08:48 +01:00
2021-10-15 14:28:41 +01:00
if all_wallets:
user = await get_user(wallet.wallet.user)
if user:
wallet_ids = user.wallet_ids
2021-10-13 17:08:48 +01:00
try:
2021-10-15 14:28:41 +01:00
links = await get_satsdice_pays(wallet_ids)
2021-10-19 20:54:02 +01:00
return [{**link.dict(), **{"lnurl": link.lnurl(request)}} for link in links]
2021-10-13 17:08:48 +01:00
except LnurlInvalidUrl:
2021-10-13 21:51:21 +01:00
raise HTTPException(
status_code=HTTPStatus.UPGRADE_REQUIRED,
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
2021-10-13 17:08:48 +01:00
)
2021-10-13 21:51:21 +01:00
@satsdice_ext.get("/api/v1/links/{link_id}")
2021-10-15 14:28:41 +01:00
async def api_link_retrieve(
link_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
2021-10-15 14:28:41 +01:00
):
2021-10-13 17:08:48 +01:00
link = await get_satsdice_pay(link_id)
if not link:
2021-10-13 21:51:21 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Pay link does not exist."
2021-10-13 21:51:21 +01:00
)
2021-10-13 17:08:48 +01:00
2021-10-15 14:28:41 +01:00
if link.wallet != wallet.wallet.id:
2021-10-13 21:51:21 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.FORBIDDEN, detail="Not your pay link."
2021-10-13 21:51:21 +01:00
)
2021-10-20 12:13:03 +01:00
return {**link.dict(), **{"lnurl": link.lnurl}}
2021-10-13 21:51:21 +01:00
@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),
2021-10-13 21:51:21 +01:00
link_id: str = Query(None),
):
if data.min_bet > data.max_bet:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request")
2021-10-13 17:08:48 +01:00
if link_id:
link = await get_satsdice_pay(link_id)
if not link:
2021-10-13 21:51:21 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Satsdice does not exist"
2021-10-13 17:08:48 +01:00
)
2021-10-13 21:51:21 +01:00
if link.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Come on, seriously, this isn't your satsdice!",
2021-10-13 17:08:48 +01:00
)
2021-10-25 11:54:58 +01:00
2021-11-04 16:41:23 +00:00
data.wallet = wallet.wallet.id
2021-10-25 11:54:58 +01:00
link = await update_satsdice_pay(link_id, **data.dict())
else:
link = await create_satsdice_pay(wallet_id=wallet.wallet.id, data=data)
2021-10-13 17:08:48 +01:00
2021-10-19 20:54:02 +01:00
return {**link.dict(), **{"lnurl": link.lnurl}}
2021-10-13 17:08:48 +01:00
2021-10-13 21:51:21 +01:00
@satsdice_ext.delete("/api/v1/links/{link_id}")
async def api_link_delete(
wallet: WalletTypeInfo = Depends(get_key_type),
link_id: str = Query(None),
2021-10-13 21:51:21 +01:00
):
2021-10-13 17:08:48 +01:00
link = await get_satsdice_pay(link_id)
if not link:
2021-10-13 21:51:21 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Pay link does not exist."
2021-10-13 21:51:21 +01:00
)
2021-10-13 17:08:48 +01:00
2021-10-20 12:13:03 +01:00
if link.wallet != wallet.wallet.id:
2021-10-13 21:51:21 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.FORBIDDEN, detail="Not your pay link."
2021-10-13 21:51:21 +01:00
)
2021-10-13 17:08:48 +01:00
await delete_satsdice_pay(link_id)
return "", HTTPStatus.NO_CONTENT
##########LNURL withdraw
@satsdice_ext.get(
"/api/v1/withdraws/{the_hash}/{lnurl_id}", dependencies=[Depends(get_key_type)]
)
2021-10-13 21:51:21 +01:00
async def api_withdraw_hash_retrieve(
lnurl_id: str = Query(None),
the_hash: str = Query(None),
):
hashCheck = await get_withdraw_hash_checkw(the_hash, lnurl_id)
2021-10-13 21:51:21 +01:00
return hashCheck