From 3e0fd39175af556870a333a6b9a976c6c9d60ef2 Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Sun, 22 Aug 2021 12:16:31 +0100 Subject: [PATCH] some syntax refactoring --- lnbits/extensions/lnurlp/views_api.py | 18 ++++++++--------- lnbits/extensions/offlineshop/views_api.py | 8 ++++---- lnbits/extensions/paywall/views_api.py | 22 ++++++++++----------- lnbits/extensions/streamalerts/views_api.py | 12 +++++------ lnbits/extensions/subdomains/views_api.py | 14 ++++++------- lnbits/extensions/watchonly/views_api.py | 8 ++++---- lnbits/extensions/withdraw/views_api.py | 10 +++++----- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/lnbits/extensions/lnurlp/views_api.py b/lnbits/extensions/lnurlp/views_api.py index b08a21625..a41e510e7 100644 --- a/lnbits/extensions/lnurlp/views_api.py +++ b/lnbits/extensions/lnurlp/views_api.py @@ -48,7 +48,7 @@ async def api_links(): ) -@lnurlp_ext.get("/api/v1/links/") +@lnurlp_ext.get("/api/v1/links/{link_id}") @api_check_wallet_key("invoice") async def api_link_retrieve(link_id): link = await get_pay_link(link_id) @@ -65,14 +65,14 @@ class CreateData(BaseModel): description: str min: int = Query(0.01, ge=0.01) max: int = Query(0.01, ge=0.01) - currency: Optional[str] - comment_chars: int = Query(0, ge=0, lt=800) - webhook_url: Optional[str] - success_text: Optional[str] - success_url: Optional[str] + currency: Optional[str] + comment_chars: int = Query(0, ge=0, lt=800) + webhook_url: Optional[str] + success_text: Optional[str] + success_url: Optional[str] @lnurlp_ext.post("/api/v1/links") -@lnurlp_ext.put("/api/v1/links/") +@lnurlp_ext.put("/api/v1/links/{link_id}") @api_check_wallet_key("invoice") async def api_link_create_or_update(data: CreateData, link_id=None): if data.min > data.max: @@ -111,7 +111,7 @@ async def api_link_create_or_update(data: CreateData, link_id=None): ) -@lnurlp_ext.delete("/api/v1/links/") +@lnurlp_ext.delete("/api/v1/links/{link_id}") @api_check_wallet_key("invoice") async def api_link_delete(link_id): link = await get_pay_link(link_id) @@ -127,7 +127,7 @@ async def api_link_delete(link_id): return "", HTTPStatus.NO_CONTENT -@lnurlp_ext.get("/api/v1/rate/") +@lnurlp_ext.get("/api/v1/rate/{currency}") async def api_check_fiat_rate(currency): try: rate = await get_fiat_rate_satoshis(currency) diff --git a/lnbits/extensions/offlineshop/views_api.py b/lnbits/extensions/offlineshop/views_api.py index af2150cb4..be860bc0f 100644 --- a/lnbits/extensions/offlineshop/views_api.py +++ b/lnbits/extensions/offlineshop/views_api.py @@ -53,11 +53,11 @@ class CreateItemsData(BaseModel): name: str description: str image: Optional[str] - price: int - unit: str + price: int + unit: str @offlineshop_ext.post("/api/v1/offlineshop/items") -@offlineshop_ext.put("/api/v1/offlineshop/items/") +@offlineshop_ext.put("/api/v1/offlineshop/items/{item_id}") @api_check_wallet_key("invoice") async def api_add_or_update_item(data: CreateItemsData, item_id=None): shop = await get_or_create_shop_by_wallet(g.wallet.id) @@ -84,7 +84,7 @@ async def api_add_or_update_item(data: CreateItemsData, item_id=None): return "", HTTPStatus.OK -@offlineshop_ext.delete("/api/v1/offlineshop/items/") +@offlineshop_ext.delete("/api/v1/offlineshop/items/{item_id}") @api_check_wallet_key("invoice") async def api_delete_item(item_id): shop = await get_or_create_shop_by_wallet(g.wallet.id) diff --git a/lnbits/extensions/paywall/views_api.py b/lnbits/extensions/paywall/views_api.py index 941038995..0d4b181fa 100644 --- a/lnbits/extensions/paywall/views_api.py +++ b/lnbits/extensions/paywall/views_api.py @@ -29,8 +29,8 @@ class CreateData(BaseModel): url: Optional[str] = Query(...) memo: Optional[str] = Query(...) description: str - amount: int - remembers: bool + amount: int + remembers: bool @paywall_ext.post("/api/v1/paywalls") @api_check_wallet_key("invoice") @@ -39,7 +39,7 @@ async def api_paywall_create(data: CreateData): return paywall, HTTPStatus.CREATED -@paywall_ext.delete("/api/v1/paywalls/") +@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) @@ -55,7 +55,7 @@ async def api_paywall_delete(paywall_id): return "", HTTPStatus.NO_CONTENT -@paywall_ext.post("/api/v1/paywalls//invoice") +@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) @@ -76,26 +76,26 @@ async def api_paywall_create_invoice(amount: int = Query(..., ge=1), paywall_id extra={"tag": "paywall"}, ) except Exception as e: - return jsonable_encoder({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR + return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR return ( - jsonable_encoder({"payment_hash": payment_hash, "payment_request": payment_request}), + {"payment_hash": payment_hash, "payment_request": payment_request}, HTTPStatus.CREATED, ) -@paywall_ext.post("/api/v1/paywalls//check_invoice") +@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 jsonable_encoder({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND + return {"message": "Paywall does not exist."}, HTTPStatus.NOT_FOUND try: status = await check_invoice_status(paywall.wallet, payment_hash) is_paid = not status.pending except Exception: - return jsonable_encoder({"paid": False}), HTTPStatus.OK + return {"paid": False}, HTTPStatus.OK if is_paid: wallet = await get_wallet(paywall.wallet) @@ -103,8 +103,8 @@ async def api_paywal_check_invoice(payment_hash: str = Query(...), paywall_id = await payment.set_pending(False) return ( - jsonable_encoder({"paid": True, "url": paywall.url, "remembers": paywall.remembers}), + {"paid": True, "url": paywall.url, "remembers": paywall.remembers}, HTTPStatus.OK, ) - return jsonable_encoder({"paid": False}), HTTPStatus.OK + return {"paid": False}, HTTPStatus.OK diff --git a/lnbits/extensions/streamalerts/views_api.py b/lnbits/extensions/streamalerts/views_api.py index 9e34ff36a..992e51882 100644 --- a/lnbits/extensions/streamalerts/views_api.py +++ b/lnbits/extensions/streamalerts/views_api.py @@ -46,7 +46,7 @@ async def api_create_service(data: CreateServicesData): return service._asdict(), HTTPStatus.CREATED -@streamalerts_ext.get("/api/v1/getaccess/") +@streamalerts_ext.get("/api/v1/getaccess/{service_id}") async def api_get_access(service_id): """Redirect to Streamlabs' Approve/Decline page for API access for Service with service_id @@ -69,7 +69,7 @@ async def api_get_access(service_id): return ({"message": "Service does not exist!"}, HTTPStatus.BAD_REQUEST) -@streamalerts_ext.get("/api/v1/authenticate/") +@streamalerts_ext.get("/api/v1/authenticate/{service_id}") async def api_authenticate_service(Code: str, State: str, service_id): """Endpoint visited via redirect during third party API authentication @@ -183,7 +183,7 @@ async def api_get_donations(): ) -@streamalerts_ext.put("/api/v1/donations/") +@streamalerts_ext.put("/api/v1/donations/{donation_id}") @api_check_wallet_key("invoice") async def api_update_donation(donation_id=None): """Update a donation with the data given in the request""" @@ -208,7 +208,7 @@ async def api_update_donation(donation_id=None): return donation._asdict(), HTTPStatus.CREATED -@streamalerts_ext.put("/api/v1/services/") +@streamalerts_ext.put("/api/v1/services/{service_id}") @api_check_wallet_key("invoice") async def api_update_service(service_id=None): """Update a service with the data given in the request""" @@ -229,7 +229,7 @@ async def api_update_service(service_id=None): return service._asdict(), HTTPStatus.CREATED -@streamalerts_ext.delete("/api/v1/donations/") +@streamalerts_ext.delete("/api/v1/donations/{donation_id}") @api_check_wallet_key("invoice") async def api_delete_donation(donation_id): """Delete the donation with the given donation_id""" @@ -245,7 +245,7 @@ async def api_delete_donation(donation_id): return "", HTTPStatus.NO_CONTENT -@streamalerts_ext.delete("/api/v1/services/") +@streamalerts_ext.delete("/api/v1/services/{service_id}") @api_check_wallet_key("invoice") async def api_delete_service(service_id): """Delete the service with the given service_id""" diff --git a/lnbits/extensions/subdomains/views_api.py b/lnbits/extensions/subdomains/views_api.py index 3684c4ad5..720002380 100644 --- a/lnbits/extensions/subdomains/views_api.py +++ b/lnbits/extensions/subdomains/views_api.py @@ -51,7 +51,7 @@ class CreateDomainsData(BaseModel): allowed_record_types: str @subdomains_ext.post("/api/v1/domains") -@subdomains_ext.put("/api/v1/domains/") +@subdomains_ext.put("/api/v1/domains/{domain_id}") @api_check_wallet_key("invoice") async def api_domain_create(data: CreateDomainsData, domain_id=None): if domain_id: @@ -66,10 +66,10 @@ async def api_domain_create(data: CreateDomainsData, domain_id=None): domain = await update_domain(domain_id, **data) else: domain = await create_domain(**data) - return jsonify(domain._asdict()), HTTPStatus.CREATED + return domain._asdict(), HTTPStatus.CREATED -@subdomains_ext.delete("/api/v1/domains/") +@subdomains_ext.delete("/api/v1/domains/{domain_id}") @api_check_wallet_key("invoice") async def api_domain_delete(domain_id): domain = await get_domain(domain_id) @@ -110,14 +110,14 @@ class CreateDomainsData(BaseModel): duration: int record_type: str -@subdomains_ext.post("/api/v1/subdomains/") +@subdomains_ext.post("/api/v1/subdomains/{domain_id}") async def api_subdomain_make_subdomain(data: CreateDomainsData, domain_id): domain = await get_domain(domain_id) # If the request is coming for the non-existant domain if not domain: - return jsonify({"message": "LNsubdomain does not exist."}), HTTPStatus.NOT_FOUND + return {"message": "LNsubdomain does not exist."}, HTTPStatus.NOT_FOUND ## If record_type is not one of the allowed ones reject the request if data.record_type not in domain.allowed_record_types: @@ -184,7 +184,7 @@ async def api_subdomain_make_subdomain(data: CreateDomainsData, domain_id): ) -@subdomains_ext.get("/api/v1/subdomains/") +@subdomains_ext.get("/api/v1/subdomains/{payment_hash}") async def api_subdomain_send_subdomain(payment_hash): subdomain = await get_subdomain(payment_hash) try: @@ -199,7 +199,7 @@ async def api_subdomain_send_subdomain(payment_hash): return {"paid": False}, HTTPStatus.OK -@subdomains_ext.delete("/api/v1/subdomains/") +@subdomains_ext.delete("/api/v1/subdomains/{subdomain_id}") @api_check_wallet_key("invoice") async def api_subdomain_delete(subdomain_id): subdomain = await get_subdomain(subdomain_id) diff --git a/lnbits/extensions/watchonly/views_api.py b/lnbits/extensions/watchonly/views_api.py index 24a269bf8..e4598a973 100644 --- a/lnbits/extensions/watchonly/views_api.py +++ b/lnbits/extensions/watchonly/views_api.py @@ -37,7 +37,7 @@ async def api_wallets_retrieve(): return "" -@watchonly_ext.get("/api/v1/wallet/") +@watchonly_ext.get("/api/v1/wallet/{wallet_id}") @api_check_wallet_key("invoice") async def api_wallet_retrieve(wallet_id): wallet = await get_watch_wallet(wallet_id) @@ -63,7 +63,7 @@ async def api_wallet_create_or_update(masterPub: str, Title: str, wallet_id=None return wallet._asdict(), HTTPStatus.CREATED -@watchonly_ext.delete("/api/v1/wallet/") +@watchonly_ext.delete("/api/v1/wallet/{wallet_id}") @api_check_wallet_key("admin") async def api_wallet_delete(wallet_id): wallet = await get_watch_wallet(wallet_id) @@ -79,7 +79,7 @@ async def api_wallet_delete(wallet_id): #############################ADDRESSES########################## -@watchonly_ext.get("/api/v1/address/") +@watchonly_ext.get("/api/v1/address/{wallet_id}") @api_check_wallet_key("invoice") async def api_fresh_address(wallet_id): await get_fresh_address(wallet_id) @@ -89,7 +89,7 @@ async def api_fresh_address(wallet_id): return [address._asdict() for address in addresses], HTTPStatus.OK -@watchonly_ext.get("/api/v1/addresses/") +@watchonly_ext.get("/api/v1/addresses/{wallet_id}") @api_check_wallet_key("invoice") async def api_get_addresses(wallet_id): wallet = await get_watch_wallet(wallet_id) diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index 97d7a9de1..b34b9fbbc 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -46,7 +46,7 @@ async def api_links(): ) -@withdraw_ext.get("/api/v1/links/") +@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) @@ -70,7 +70,7 @@ class CreateData(BaseModel): is_unique: bool @withdraw_ext.post("/api/v1/links") -@withdraw_ext.put("/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: @@ -97,7 +97,7 @@ async def api_link_create_or_update(data: CreateData, link_id: str = None): HTTPStatus.NOT_FOUND, ) if link.wallet != g.wallet.id: - return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN + return {"message": "Not your withdraw link."}, HTTPStatus.FORBIDDEN link = await update_withdraw_link(link_id, **data, usescsv=usescsv, used=0) else: link = await create_withdraw_link( @@ -109,7 +109,7 @@ async def api_link_create_or_update(data: CreateData, link_id: str = None): ) -@withdraw_ext.delete("/api/v1/links/") +@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) @@ -127,7 +127,7 @@ async def api_link_delete(link_id): return "", HTTPStatus.NO_CONTENT -@withdraw_ext.get("/api/v1/links//") +@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)