ext: widthraw - views_api

This commit is contained in:
Tiago vasconcelos 2021-09-29 11:04:37 +01:00
parent 4c724e6f01
commit 65aa1b24d8

View File

@ -1,11 +1,17 @@
from quart import g, jsonify, request from fastapi.params import Depends
from fastapi.param_functions import Query
from pydantic.main import BaseModel
from http import HTTPStatus from http import HTTPStatus
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
from lnbits.core.crud import get_user from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from pydantic import BaseModel
from fastapi import FastAPI, Query, Response # from fastapi import FastAPI, Query, Response
from . import withdraw_ext from . import withdraw_ext
from .crud import ( from .crud import (
@ -19,13 +25,13 @@ from .crud import (
) )
@withdraw_ext.get("/api/v1/links", status_code=200) @withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
@api_check_wallet_key("invoice") # @api_check_wallet_key("invoice")
async def api_links(response: Response): async def api_links(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
wallet_ids = [g.wallet.id] wallet_ids = [wallet.wallet.id]
if "all_wallets" in request.args: if all_wallets:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
try: try:
return [ return [
{ {
@ -34,24 +40,36 @@ async def api_links(response: Response):
} }
for link in await get_withdraw_links(wallet_ids) for link in await get_withdraw_links(wallet_ids)
] ]
except LnurlInvalidUrl: except LnurlInvalidUrl:
response.status_code = HTTPStatus.UPGRADE_REQUIRED raise HTTPException(
return { "message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor." } status_code=HTTPStatus.UPGRADE_REQUIRED,
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
)
# response.status_code = HTTPStatus.UPGRADE_REQUIRED
# return { "message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor." }
@withdraw_ext.get("/api/v1/links/{link_id}", status_code=200) @withdraw_ext.get("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
@api_check_wallet_key("invoice") # @api_check_wallet_key("invoice")
async def api_link_retrieve(link_id, response: Response): async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
link = await get_withdraw_link(link_id, 0) link = await get_withdraw_link(link_id, 0)
if not link: if not link:
response.status_code = HTTPStatus.NOT_FOUND raise HTTPException(
return {"message": "Withdraw link does not exist."} detail="Withdraw link does not exist.",
status_code=HTTPStatus.NOT_FOUND
)
# response.status_code = HTTPStatus.NOT_FOUND
# return {"message": "Withdraw link does not exist."}
if link.wallet != g.wallet.id: if link.wallet != wallet.wallet.id:
response.status_code = HTTPStatus.FORBIDDEN raise HTTPException(
return {"message": "Not your withdraw link."} detail="Not your withdraw link.",
status_code=HTTPStatus.FORBIDDEN
)
# response.status_code = HTTPStatus.FORBIDDEN
# return {"message": "Not your withdraw link."}
return {**link, **{"lnurl": link.lnurl}} return {**link, **{"lnurl": link.lnurl}}
@ -64,14 +82,18 @@ class CreateData(BaseModel):
is_unique: bool is_unique: bool
@withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED) @withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
@withdraw_ext.put("/api/v1/links/{link_id}") @withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
async def api_link_create_or_update(data: CreateData, link_id: str = None, response: Response): async def api_link_create_or_update(data: CreateData, link_id: str = None, response: Response):
if data.max_withdrawable < data.min_withdrawable: if data.max_withdrawable < data.min_withdrawable:
response.status_code = HTTPStatus.BAD_REQUEST raise HTTPException(
return { detail="`max_withdrawable` needs to be at least `min_withdrawable`.",
"message": "`max_withdrawable` needs to be at least `min_withdrawable`." status_code=HTTPStatus.BAD_REQUEST
} )
# response.status_code = HTTPStatus.BAD_REQUEST
# return {
# "message": "`max_withdrawable` needs to be at least `min_withdrawable`."
# }
usescsv = "" usescsv = ""
for i in range(data.uses): for i in range(data.uses):
@ -84,41 +106,57 @@ async def api_link_create_or_update(data: CreateData, link_id: str = None, respo
if link_id: if link_id:
link = await get_withdraw_link(link_id, 0) link = await get_withdraw_link(link_id, 0)
if not link: if not link:
response.status_code = HTTPStatus.NOT_FOUND raise HTTPException(
return {"message": "Withdraw link does not exist."} detail="Withdraw link does not exist.",
if link.wallet != g.wallet.id: status_code=HTTPStatus.NOT_FOUND
response.status_code = HTTPStatus.FORBIDDEN )
return {"message": "Not your withdraw link."} # response.status_code = HTTPStatus.NOT_FOUND
# return {"message": "Withdraw link does not exist."}
if link.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your withdraw link.",
status_code=HTTPStatus.FORBIDDEN
)
# response.status_code = HTTPStatus.FORBIDDEN
# return {"message": "Not your withdraw link."}
link = await update_withdraw_link(link_id, **data, usescsv=usescsv, used=0) link = await update_withdraw_link(link_id, **data, usescsv=usescsv, used=0)
else: else:
link = await create_withdraw_link( link = await create_withdraw_link(
wallet_id=g.wallet.id, **data, usescsv=usescsv wallet_id=wallet.wallet.id, **data, usescsv=usescsv
) )
if link_id: # if link_id:
response.status_code = HTTPStatus.OK # response.status_code = HTTPStatus.OK
return {**link, **{"lnurl": link.lnurl}} return {**link, **{"lnurl": link.lnurl}}
@withdraw_ext.delete("/api/v1/links/{link_id}", status_code=HTTPStatus.NO_CONTENT) @withdraw_ext.delete("/api/v1/links/{link_id}")
@api_check_wallet_key("admin") # @api_check_wallet_key("admin")
async def api_link_delete(link_id, response: Response): async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
link = await get_withdraw_link(link_id) link = await get_withdraw_link(link_id)
if not link: if not link:
response.status_code = HTTPStatus.NOT_FOUND raise HTTPException(
return {"message": "Withdraw link does not exist."} detail="Withdraw link does not exist.",
status_code=HTTPStatus.NOT_FOUND
)
# response.status_code = HTTPStatus.NOT_FOUND
# return {"message": "Withdraw link does not exist."}
if link.wallet != g.wallet.id: if link.wallet != wallet.wallet.id:
response.status_code = HTTPStatus.FORBIDDEN raise HTTPException(
return {"message": "Not your withdraw link."} detail="Not your withdraw link.",
status_code=HTTPStatus.FORBIDDEN
)
# response.status_code = HTTPStatus.FORBIDDEN
# return {"message": "Not your withdraw link."}
await delete_withdraw_link(link_id) await delete_withdraw_link(link_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
return "" # return ""
@withdraw_ext.get("/api/v1/links/{the_hash}/{lnurl_id}", status_code=HTTPStatus.OK) @withdraw_ext.get("/api/v1/links/{the_hash}/{lnurl_id}", status_code=HTTPStatus.OK)
@api_check_wallet_key("invoice") # @api_check_wallet_key("invoice")
async def api_hash_retrieve(the_hash, lnurl_id): async def api_hash_retrieve(the_hash, lnurl_id, wallet: WalletTypeInfo = Depends(get_key_type)):
hashCheck = await get_hash_check(the_hash, lnurl_id) hashCheck = await get_hash_check(the_hash, lnurl_id)
return hashCheck return hashCheck