2021-08-21 12:14:23 +02:00
|
|
|
from typing import Optional
|
|
|
|
from pydantic.main import BaseModel
|
2021-08-20 12:44:03 +01:00
|
|
|
from quart import g, jsonify
|
|
|
|
from http import HTTPStatus
|
|
|
|
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
|
|
|
|
|
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
|
|
|
from lnbits.utils.exchange_rates import currencies
|
|
|
|
|
|
|
|
from . import offlineshop_ext
|
|
|
|
from .crud import (
|
|
|
|
get_or_create_shop_by_wallet,
|
|
|
|
set_method,
|
|
|
|
add_item,
|
|
|
|
update_item,
|
|
|
|
get_items,
|
|
|
|
delete_item_from_shop,
|
|
|
|
)
|
|
|
|
from .models import ShopCounter
|
|
|
|
|
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
@offlineshop_ext.get("/api/v1/currencies")
|
2021-08-20 12:44:03 +01:00
|
|
|
async def api_list_currencies_available():
|
|
|
|
return jsonify(list(currencies.keys()))
|
|
|
|
|
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
@offlineshop_ext.get("/api/v1/offlineshop")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_shop_from_wallet():
|
|
|
|
shop = await get_or_create_shop_by_wallet(g.wallet.id)
|
|
|
|
items = await get_items(shop.id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
**shop._asdict(),
|
|
|
|
**{
|
|
|
|
"otp_key": shop.otp_key,
|
|
|
|
"items": [item.values() for item in items],
|
|
|
|
},
|
2021-08-21 00:34:48 +01:00
|
|
|
},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
except LnurlInvalidUrl:
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
2021-08-21 00:34:48 +01:00
|
|
|
},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.UPGRADE_REQUIRED,
|
|
|
|
)
|
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
class CreateItemsData(BaseModel):
|
|
|
|
name: str
|
|
|
|
description: str
|
|
|
|
image: Optional[str]
|
|
|
|
price: int
|
|
|
|
unit: str
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
@offlineshop_ext.post("/api/v1/offlineshop/items")
|
|
|
|
@offlineshop_ext.put("/api/v1/offlineshop/items/<item_id>")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
2021-08-21 00:34:48 +01:00
|
|
|
async def api_add_or_update_item(data: CreateItemsData, item_id=None):
|
2021-08-20 12:44:03 +01:00
|
|
|
shop = await get_or_create_shop_by_wallet(g.wallet.id)
|
|
|
|
if item_id == None:
|
|
|
|
await add_item(
|
|
|
|
shop.id,
|
2021-08-21 00:34:48 +01:00
|
|
|
data.name,
|
|
|
|
data.description,
|
|
|
|
data.image,
|
|
|
|
data.price,
|
|
|
|
data.unit,
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
|
|
|
return "", HTTPStatus.CREATED
|
|
|
|
else:
|
|
|
|
await update_item(
|
|
|
|
shop.id,
|
|
|
|
item_id,
|
2021-08-21 00:34:48 +01:00
|
|
|
data.name,
|
|
|
|
data.description,
|
|
|
|
data.image,
|
|
|
|
data.price,
|
|
|
|
data.unit,
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
|
|
|
return "", HTTPStatus.OK
|
|
|
|
|
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
@offlineshop_ext.delete("/api/v1/offlineshop/items/<item_id>")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_delete_item(item_id):
|
|
|
|
shop = await get_or_create_shop_by_wallet(g.wallet.id)
|
|
|
|
await delete_item_from_shop(shop.id, item_id)
|
|
|
|
return "", HTTPStatus.NO_CONTENT
|
|
|
|
|
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
class CreateMethodData(BaseModel):
|
|
|
|
method: str
|
|
|
|
wordlist: Optional[str]
|
|
|
|
|
|
|
|
@offlineshop_ext.put("/api/v1/offlineshop/method")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
2021-08-21 00:34:48 +01:00
|
|
|
async def api_set_method(data: CreateMethodData):
|
|
|
|
method = data.method
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
wordlist = data.wordlist.split("\n") if data.wordlist else None
|
2021-08-20 12:44:03 +01:00
|
|
|
wordlist = [word.strip() for word in wordlist if word.strip()]
|
|
|
|
|
|
|
|
shop = await get_or_create_shop_by_wallet(g.wallet.id)
|
|
|
|
if not shop:
|
|
|
|
return "", HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
updated_shop = await set_method(shop.id, method, "\n".join(wordlist))
|
|
|
|
if not updated_shop:
|
|
|
|
return "", HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
ShopCounter.reset(updated_shop)
|
|
|
|
return "", HTTPStatus.OK
|