syntax refactoring up to diagonalley

This commit is contained in:
Tiago vasconcelos
2021-08-23 10:18:52 +01:00
parent e03eb3c82b
commit 83a9dc2a3e
3 changed files with 87 additions and 84 deletions
+53 -44
View File
@@ -4,6 +4,11 @@ from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from fastapi import FastAPI, Query
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from . import livestream_ext
from .crud import (
get_or_create_livestream_by_wallet,
@@ -18,7 +23,7 @@ from .crud import (
)
@livestream_ext.route("/api/v1/livestream", methods=["GET"])
@livestream_ext.get("/api/v1/livestream")
@api_check_wallet_key("invoice")
async def api_livestream_from_wallet():
ls = await get_or_create_livestream_by_wallet(g.wallet.id)
@@ -26,8 +31,7 @@ async def api_livestream_from_wallet():
producers = await get_producers(ls.id)
try:
return (
jsonify(
return
{
**ls._asdict(),
**{
@@ -39,21 +43,19 @@ async def api_livestream_from_wallet():
"producers": [producer._asdict() for producer in producers],
},
}
),
HTTPStatus.OK,
)
,
HTTPStatus.OK
except LnurlInvalidUrl:
return (
jsonify(
return
{
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
}
),
HTTPStatus.UPGRADE_REQUIRED,
)
,
HTTPStatus.UPGRADE_REQUIRED
@livestream_ext.route("/api/v1/livestream/track/<track_id>", methods=["PUT"])
@livestream_ext.put("/api/v1/livestream/track/{track_id}")
@api_check_wallet_key("invoice")
async def api_update_track(track_id):
try:
@@ -68,7 +70,7 @@ async def api_update_track(track_id):
return "", HTTPStatus.NO_CONTENT
@livestream_ext.route("/api/v1/livestream/fee/<fee_pct>", methods=["PUT"])
@livestream_ext.put("/api/v1/livestream/fee/{fee_pct}")
@api_check_wallet_key("invoice")
async def api_update_fee(fee_pct):
ls = await get_or_create_livestream_by_wallet(g.wallet.id)
@@ -76,33 +78,40 @@ async def api_update_fee(fee_pct):
return "", HTTPStatus.NO_CONTENT
@livestream_ext.route("/api/v1/livestream/tracks", methods=["POST"])
@livestream_ext.route("/api/v1/livestream/tracks/<id>", methods=["PUT"])
class CreateData(BaseModel):
name: str
download_url: str = Query(None)
price_msat: int = Query(None, ge=0)
producer_id: int #missing the exclude thing
producer_name: str #missing the exclude thing
@livestream_ext.post("/api/v1/livestream/tracks")
@livestream_ext.put("/api/v1/livestream/tracks/{id}")
@api_check_wallet_key("invoice")
@api_validate_post_request(
schema={
"name": {"type": "string", "empty": False, "required": True},
"download_url": {"type": "string", "empty": False, "required": False},
"price_msat": {"type": "number", "min": 0, "required": False},
"producer_id": {
"type": "number",
"required": True,
"excludes": "producer_name",
},
"producer_name": {
"type": "string",
"required": True,
"excludes": "producer_id",
},
}
)
async def api_add_track(id=None):
# @api_validate_post_request(
# schema={
# "name": {"type": "string", "empty": False, "required": True},
# "download_url": {"type": "string", "empty": False, "required": False},
# "price_msat": {"type": "number", "min": 0, "required": False},
# "producer_id": {
# "type": "number",
# "required": True,
# "excludes": "producer_name",
# },
# "producer_name": {
# "type": "string",
# "required": True,
# "excludes": "producer_id",
# },
# }
# )
async def api_add_track(data: CreateData, id=None):
ls = await get_or_create_livestream_by_wallet(g.wallet.id)
if "producer_id" in g.data:
p_id = g.data["producer_id"]
elif "producer_name" in g.data:
p_id = await add_producer(ls.id, g.data["producer_name"])
if "producer_id" in data:
p_id = data["producer_id"]
elif "producer_name" in data:
p_id = await add_producer(ls.id, data["producer_name"])
else:
raise TypeError("need either producer_id or producer_name arguments")
@@ -110,24 +119,24 @@ async def api_add_track(id=None):
await update_track(
ls.id,
id,
g.data["name"],
g.data.get("download_url"),
g.data.get("price_msat", 0),
data["name"],
data.get("download_url"),
data.get("price_msat", 0),
p_id,
)
return "", HTTPStatus.OK
else:
await add_track(
ls.id,
g.data["name"],
g.data.get("download_url"),
g.data.get("price_msat", 0),
data["name"],
data.get("download_url"),
data.get("price_msat", 0),
p_id,
)
return "", HTTPStatus.CREATED
@livestream_ext.route("/api/v1/livestream/tracks/<track_id>", methods=["DELETE"])
@livestream_ext.delete("/api/v1/livestream/tracks/{track_id}")
@api_check_wallet_key("invoice")
async def api_delete_track(track_id):
ls = await get_or_create_livestream_by_wallet(g.wallet.id)