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 pydantic import BaseModel
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")
async def api_paywalls():
wallet_ids = [g.wallet.id]
@ -20,7 +21,7 @@ async def api_paywalls():
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
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,
)
@ -32,45 +33,42 @@ class CreateData(BaseModel):
amount: int = 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")
async def api_paywall_create(data: CreateData):
paywall = await create_paywall(wallet_id=g.wallet.id, **data)
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")
async def api_paywall_delete(paywall_id):
paywall = await get_paywall(paywall_id)
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:
return jsonify({"message": "Not your paywall."}), HTTPStatus.FORBIDDEN
return jsonable_encoder({"message": "Not your paywall."}), HTTPStatus.FORBIDDEN
await delete_paywall(paywall_id)
return "", HTTPStatus.NO_CONTENT
@paywall_ext.route("/api/v1/paywalls/<paywall_id>/invoice", methods=["POST"])
@api_validate_post_request(
schema={"amount": {"type": "integer", "min": 1, "required": True}}
)
async def api_paywall_create_invoice(paywall_id):
@paywall_ext.post("/api/v1/paywalls/<paywall_id>/invoice")
async def api_paywall_create_invoice(amount: int = Query(..., ge=1), paywall_id = None):
paywall = await get_paywall(paywall_id)
if g.data["amount"] < paywall.amount:
if amount < paywall.amount:
return (
jsonify({"message": f"Minimum amount is {paywall.amount} sat."}),
jsonable_encoder({"message": f"Minimum amount is {paywall.amount} sat."}),
HTTPStatus.BAD_REQUEST,
)
try:
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(
wallet_id=paywall.wallet,
@ -79,38 +77,35 @@ async def api_paywall_create_invoice(paywall_id):
extra={"tag": "paywall"},
)
except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
return jsonable_encoder({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
return (
jsonify({"payment_hash": payment_hash, "payment_request": payment_request}),
jsonable_encoder({"payment_hash": payment_hash, "payment_request": payment_request}),
HTTPStatus.CREATED,
)
@paywall_ext.route("/api/v1/paywalls/<paywall_id>/check_invoice", methods=["POST"])
@api_validate_post_request(
schema={"payment_hash": {"type": "string", "empty": False, "required": True}}
)
async def api_paywal_check_invoice(paywall_id):
@paywall_ext.post("/api/v1/paywalls/<paywall_id>/check_invoice")
async def api_paywal_check_invoice(payment_hash: str = Query(...), paywall_id = None):
paywall = await get_paywall(paywall_id)
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:
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
except Exception:
return jsonify({"paid": False}), HTTPStatus.OK
return jsonable_encoder({"paid": False}), HTTPStatus.OK
if is_paid:
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)
return (
jsonify({"paid": True, "url": paywall.url, "remembers": paywall.remembers}),
jsonable_encoder({"paid": True, "url": paywall.url, "remembers": paywall.remembers}),
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")
async def api_links():
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")
async def api_link_retrieve(link_id):
link = await get_withdraw_link(link_id, 0)
@ -75,8 +75,8 @@ class CreateData(BaseModel):
wait_time: int = Query(..., ge=1),
is_unique: bool
@withdraw_ext.route("/api/v1/links", methods=["POST"])
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["PUT"])
@withdraw_ext.post("/api/v1/links")
@withdraw_ext.put("/api/v1/links/<link_id>")
@api_check_wallet_key("admin")
async def api_link_create_or_update(data: CreateData, link_id: str = None):
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")
async def api_link_delete(link_id):
link = await get_withdraw_link(link_id)
@ -137,8 +137,8 @@ async def api_link_delete(link_id):
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")
async def api_hash_retrieve(the_hash, lnurl_id):
hashCheck = await get_hash_check(the_hash, lnurl_id)
return jsonify(hashCheck), HTTPStatus.OK
return jsonable_encoder(hashCheck), HTTPStatus.OK