Changed routs to request types

This commit is contained in:
Ben Arc 2021-08-20 17:15:04 +01:00
parent 22b57d99d5
commit 9ea3c51b92
2 changed files with 29 additions and 34 deletions

View file

@ -10,8 +10,9 @@ from .crud import create_paywall, get_paywall, get_paywalls, delete_paywall
from typing import Optional from typing import Optional
from pydantic import BaseModel from pydantic import BaseModel
from fastapi import FastAPI, Query from fastapi import FastAPI, Query
from fastapi.encoders import jsonable_encoder
@paywall_ext.route("/api/v1/paywalls", methods=["GET"]) @paywall_ext.get("/api/v1/paywalls")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_paywalls(): async def api_paywalls():
wallet_ids = [g.wallet.id] wallet_ids = [g.wallet.id]
@ -20,7 +21,7 @@ async def api_paywalls():
wallet_ids = (await get_user(g.wallet.user)).wallet_ids wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return ( return (
jsonify([paywall._asdict() for paywall in await get_paywalls(wallet_ids)]), jsonable_encoder([paywall._asdict() for paywall in await get_paywalls(wallet_ids)]),
HTTPStatus.OK, HTTPStatus.OK,
) )
@ -32,45 +33,42 @@ class CreateData(BaseModel):
amount: int = Query(None), amount: int = Query(None),
remembers: bool = Query(None) remembers: bool = Query(None)
@paywall_ext.route("/api/v1/paywalls", methods=["POST"]) @paywall_ext.post("/api/v1/paywalls")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_paywall_create(data: CreateData): async def api_paywall_create(data: CreateData):
paywall = await create_paywall(wallet_id=g.wallet.id, **data) paywall = await create_paywall(wallet_id=g.wallet.id, **data)
return paywall, HTTPStatus.CREATED return paywall, HTTPStatus.CREATED
@paywall_ext.route("/api/v1/paywalls/<paywall_id>", methods=["DELETE"]) @paywall_ext.delete("/api/v1/paywalls/<paywall_id>")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_paywall_delete(paywall_id): async def api_paywall_delete(paywall_id):
paywall = await get_paywall(paywall_id) paywall = await get_paywall(paywall_id)
if not paywall: if not paywall:
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND return jsonable_encoder({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
if paywall.wallet != g.wallet.id: if paywall.wallet != g.wallet.id:
return jsonify({"message": "Not your paywall."}), HTTPStatus.FORBIDDEN return jsonable_encoder({"message": "Not your paywall."}), HTTPStatus.FORBIDDEN
await delete_paywall(paywall_id) await delete_paywall(paywall_id)
return "", HTTPStatus.NO_CONTENT return "", HTTPStatus.NO_CONTENT
@paywall_ext.route("/api/v1/paywalls/<paywall_id>/invoice", methods=["POST"]) @paywall_ext.post("/api/v1/paywalls/<paywall_id>/invoice")
@api_validate_post_request( async def api_paywall_create_invoice(amount: int = Query(..., ge=1), paywall_id = None):
schema={"amount": {"type": "integer", "min": 1, "required": True}}
)
async def api_paywall_create_invoice(paywall_id):
paywall = await get_paywall(paywall_id) paywall = await get_paywall(paywall_id)
if g.data["amount"] < paywall.amount: if amount < paywall.amount:
return ( return (
jsonify({"message": f"Minimum amount is {paywall.amount} sat."}), jsonable_encoder({"message": f"Minimum amount is {paywall.amount} sat."}),
HTTPStatus.BAD_REQUEST, HTTPStatus.BAD_REQUEST,
) )
try: try:
amount = ( amount = (
g.data["amount"] if g.data["amount"] > paywall.amount else paywall.amount amount if amount > paywall.amount else paywall.amount
) )
payment_hash, payment_request = await create_invoice( payment_hash, payment_request = await create_invoice(
wallet_id=paywall.wallet, wallet_id=paywall.wallet,
@ -79,38 +77,35 @@ async def api_paywall_create_invoice(paywall_id):
extra={"tag": "paywall"}, extra={"tag": "paywall"},
) )
except Exception as e: except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR return jsonable_encoder({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
return ( return (
jsonify({"payment_hash": payment_hash, "payment_request": payment_request}), jsonable_encoder({"payment_hash": payment_hash, "payment_request": payment_request}),
HTTPStatus.CREATED, HTTPStatus.CREATED,
) )
@paywall_ext.route("/api/v1/paywalls/<paywall_id>/check_invoice", methods=["POST"]) @paywall_ext.post("/api/v1/paywalls/<paywall_id>/check_invoice")
@api_validate_post_request( async def api_paywal_check_invoice(payment_hash: str = Query(...), paywall_id = None):
schema={"payment_hash": {"type": "string", "empty": False, "required": True}}
)
async def api_paywal_check_invoice(paywall_id):
paywall = await get_paywall(paywall_id) paywall = await get_paywall(paywall_id)
if not paywall: if not paywall:
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND return jsonable_encoder({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
try: try:
status = await check_invoice_status(paywall.wallet, g.data["payment_hash"]) status = await check_invoice_status(paywall.wallet, payment_hash)
is_paid = not status.pending is_paid = not status.pending
except Exception: except Exception:
return jsonify({"paid": False}), HTTPStatus.OK return jsonable_encoder({"paid": False}), HTTPStatus.OK
if is_paid: if is_paid:
wallet = await get_wallet(paywall.wallet) wallet = await get_wallet(paywall.wallet)
payment = await wallet.get_payment(g.data["payment_hash"]) payment = await wallet.get_payment(payment_hash)
await payment.set_pending(False) await payment.set_pending(False)
return ( return (
jsonify({"paid": True, "url": paywall.url, "remembers": paywall.remembers}), jsonable_encoder({"paid": True, "url": paywall.url, "remembers": paywall.remembers}),
HTTPStatus.OK, HTTPStatus.OK,
) )
return jsonify({"paid": False}), HTTPStatus.OK return jsonable_encoder({"paid": False}), HTTPStatus.OK

View file

@ -20,7 +20,7 @@ from .crud import (
) )
@withdraw_ext.route("/api/v1/links", methods=["GET"]) @withdraw_ext.get("/api/v1/links")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_links(): async def api_links():
wallet_ids = [g.wallet.id] wallet_ids = [g.wallet.id]
@ -51,7 +51,7 @@ async def api_links():
) )
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["GET"]) @withdraw_ext.get("/api/v1/links/<link_id>")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_link_retrieve(link_id): async def api_link_retrieve(link_id):
link = await get_withdraw_link(link_id, 0) link = await get_withdraw_link(link_id, 0)
@ -75,8 +75,8 @@ class CreateData(BaseModel):
wait_time: int = Query(..., ge=1), wait_time: int = Query(..., ge=1),
is_unique: bool is_unique: bool
@withdraw_ext.route("/api/v1/links", methods=["POST"]) @withdraw_ext.post("/api/v1/links")
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["PUT"]) @withdraw_ext.put("/api/v1/links/<link_id>")
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
async def api_link_create_or_update(data: CreateData, link_id: str = None): async def api_link_create_or_update(data: CreateData, link_id: str = None):
if data.max_withdrawable < data.min_withdrawable: if data.max_withdrawable < data.min_withdrawable:
@ -118,7 +118,7 @@ async def api_link_create_or_update(data: CreateData, link_id: str = None):
) )
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["DELETE"]) @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): async def api_link_delete(link_id):
link = await get_withdraw_link(link_id) link = await get_withdraw_link(link_id)
@ -137,8 +137,8 @@ async def api_link_delete(link_id):
return "", HTTPStatus.NO_CONTENT return "", HTTPStatus.NO_CONTENT
@withdraw_ext.route("/api/v1/links/<the_hash>/<lnurl_id>", methods=["GET"]) @withdraw_ext.get("/api/v1/links/<the_hash>/<lnurl_id>")
@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):
hashCheck = await get_hash_check(the_hash, lnurl_id) hashCheck = await get_hash_check(the_hash, lnurl_id)
return jsonify(hashCheck), HTTPStatus.OK return jsonable_encoder(hashCheck), HTTPStatus.OK