usermanager views_api

This commit is contained in:
Tiago vasconcelos
2021-10-12 10:57:15 +01:00
parent ce4235f858
commit f4dfc1b689

View File

@@ -5,7 +5,6 @@ from fastapi import Query
from fastapi.params import Depends from fastapi.params import Depends
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 WalletTypeInfo, get_key_type from lnbits.decorators import WalletTypeInfo, get_key_type
from . import usermanager_ext from . import usermanager_ext
@@ -89,61 +88,43 @@ async def api_usermanager_activate_extension(extension: str = Query(...), userid
###Wallets ###Wallets
@usermanager_ext.route("/api/v1/wallets", methods=["POST"]) @usermanager_ext.post("/api/v1/wallets")
@api_check_wallet_key(key_type="invoice") async def api_usermanager_wallets_create(
@api_validate_post_request( wallet: WalletTypeInfo = Depends(get_key_type),
schema={ user_id: str = Query(...),
"user_id": {"type": "string", "empty": False, "required": True}, wallet_name: str = Query(...),
"wallet_name": {"type": "string", "empty": False, "required": True}, admin_id: str = Query(...)
"admin_id": {"type": "string", "empty": False, "required": True}, ):
}
)
async def api_usermanager_wallets_create():
user = await create_usermanager_wallet( user = await create_usermanager_wallet(
g.data["user_id"], g.data["wallet_name"], g.data["admin_id"] user_id, wallet_name, admin_id
) )
return jsonify(user._asdict()), HTTPStatus.CREATED return user.dict()
@usermanager_ext.route("/api/v1/wallets", methods=["GET"]) @usermanager_ext.get("/api/v1/wallets")
@api_check_wallet_key(key_type="invoice") async def api_usermanager_wallets(wallet: WalletTypeInfo = Depends(get_key_type)):
async def api_usermanager_wallets(): admin_id = wallet.wallet.user
admin_id = g.wallet.user return [wallet.dict() for wallet in await get_usermanager_wallets(admin_id)]
return (
jsonify(
[wallet._asdict() for wallet in await get_usermanager_wallets(admin_id)]
),
HTTPStatus.OK,
)
@usermanager_ext.route("/api/v1/wallets<wallet_id>", methods=["GET"]) @usermanager_ext.get("/api/v1/wallets/{wallet_id}")
@api_check_wallet_key(key_type="invoice") async def api_usermanager_wallet_transactions(wallet_id, wallet: WalletTypeInfo = Depends(get_key_type)):
async def api_usermanager_wallet_transactions(wallet_id): return await get_usermanager_wallet_transactions(wallet_id)
return jsonify(await get_usermanager_wallet_transactions(wallet_id)), HTTPStatus.OK
@usermanager_ext.route("/api/v1/wallets/<user_id>", methods=["GET"]) @usermanager_ext.get("/api/v1/wallets/{user_id}")
@api_check_wallet_key(key_type="invoice") async def api_usermanager_users_wallets(user_id, wallet: WalletTypeInfo = Depends(get_key_type)):
async def api_usermanager_users_wallets(user_id): # wallet = await get_usermanager_users_wallets(user_id)
wallet = await get_usermanager_users_wallets(user_id) return [s_wallet.dict() for s_wallet in await get_usermanager_users_wallets(user_id)]
return (
jsonify(
[
wallet._asdict()
for wallet in await get_usermanager_users_wallets(user_id)
]
),
HTTPStatus.OK,
)
@usermanager_ext.route("/api/v1/wallets/<wallet_id>", methods=["DELETE"]) @usermanager_ext.delete("/api/v1/wallets/{wallet_id}")
@api_check_wallet_key(key_type="invoice") async def api_usermanager_wallets_delete(wallet_id, wallet: WalletTypeInfo = Depends(get_key_type)):
async def api_usermanager_wallets_delete(wallet_id): get_wallet = await get_usermanager_wallet(wallet_id)
wallet = await get_usermanager_wallet(wallet_id) if not get_wallet:
if not wallet: raise HTTPException(
return jsonify({"message": "Wallet does not exist."}), HTTPStatus.NOT_FOUND status_code=HTTPStatus.NOT_FOUND,
detail="Wallet does not exist."
await delete_usermanager_wallet(wallet_id, wallet.user) )
return "", HTTPStatus.NO_CONTENT await delete_usermanager_wallet(wallet_id, get_wallet.user)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)