lnbits-legend/lnbits/extensions/offlineshop/views_api.py

121 lines
3.5 KiB
Python
Raw Normal View History

2021-10-20 12:03:11 +01:00
from http import HTTPStatus
from typing import Optional
2021-09-16 19:42:05 +02:00
2022-07-27 22:49:23 +01:00
from fastapi import Query
2021-09-16 19:42:05 +02:00
from fastapi.params import Depends
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
2021-10-20 12:03:11 +01:00
from pydantic.main import BaseModel
2021-09-16 19:42:05 +02:00
from starlette.exceptions import HTTPException
from starlette.requests import Request
2021-10-20 12:03:11 +01:00
from starlette.responses import HTMLResponse # type: ignore
2021-09-16 19:42:05 +02:00
from lnbits.decorators import WalletTypeInfo, get_key_type
2021-10-20 12:03:11 +01:00
from lnbits.utils.exchange_rates import currencies
from . import offlineshop_ext
from .crud import (
2021-10-20 12:03:11 +01:00
add_item,
delete_item_from_shop,
get_items,
get_or_create_shop_by_wallet,
set_method,
update_item,
)
from .models import ShopCounter
2021-08-21 00:34:48 +01:00
@offlineshop_ext.get("/api/v1/currencies")
async def api_list_currencies_available():
2021-11-09 10:41:59 +00:00
return list(currencies.keys())
2021-08-21 00:34:48 +01:00
@offlineshop_ext.get("/api/v1/offlineshop")
2021-10-17 18:33:29 +01:00
async def api_shop_from_wallet(
r: Request, wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-09-16 19:42:05 +02:00
shop = await get_or_create_shop_by_wallet(wallet.wallet.id)
items = await get_items(shop.id)
try:
2021-09-16 19:42:05 +02:00
return {
**shop.dict(),
2021-10-17 18:33:29 +01:00
**{"otp_key": shop.otp_key, "items": [item.values(r) for item in items]},
2021-09-16 19:42:05 +02:00
}
except LnurlInvalidUrl:
2021-09-16 19:42:05 +02:00
raise HTTPException(
status_code=HTTPStatus.UPGRADE_REQUIRED,
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
)
2021-09-16 19:42:05 +02:00
2021-08-21 00:34:48 +01:00
class CreateItemsData(BaseModel):
2021-09-16 19:42:05 +02:00
name: str
2021-08-21 00:34:48 +01:00
description: str
2021-09-16 19:42:05 +02:00
image: Optional[str]
2022-07-27 22:49:23 +01:00
price: float
2021-09-16 19:42:05 +02:00
unit: str
2022-07-27 22:49:23 +01:00
fiat_base_multiplier: int = Query(100, ge=1)
2021-09-16 19:42:05 +02:00
2021-08-21 00:34:48 +01:00
@offlineshop_ext.post("/api/v1/offlineshop/items")
2021-08-22 12:16:31 +01:00
@offlineshop_ext.put("/api/v1/offlineshop/items/{item_id}")
2021-10-17 18:33:29 +01:00
async def api_add_or_update_item(
data: CreateItemsData, item_id=None, wallet: WalletTypeInfo = Depends(get_key_type)
):
shop = await get_or_create_shop_by_wallet(wallet.wallet.id)
2022-07-27 22:49:23 +01:00
if data.unit != "sat":
data.price = data.price * 100
if item_id == None:
2022-07-27 22:49:23 +01:00
await add_item(
2022-07-27 22:49:23 +01:00
shop.id,
data.name,
data.description,
data.image,
data.price,
data.unit,
)
2021-10-17 18:33:29 +01:00
return HTMLResponse(status_code=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,
2022-07-27 22:49:23 +01:00
data.fiat_base_multiplier,
)
2021-08-22 12:16:31 +01:00
@offlineshop_ext.delete("/api/v1/offlineshop/items/{item_id}")
2021-09-16 19:42:05 +02:00
async def api_delete_item(item_id, wallet: WalletTypeInfo = Depends(get_key_type)):
shop = await get_or_create_shop_by_wallet(wallet.wallet.id)
await delete_item_from_shop(shop.id, item_id)
2021-09-16 19:42:05 +02:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
2021-08-21 00:34:48 +01:00
class CreateMethodData(BaseModel):
2021-09-16 19:42:05 +02:00
method: str
2021-08-21 00:34:48 +01:00
wordlist: Optional[str]
2021-09-16 19:42:05 +02:00
2021-08-21 00:34:48 +01:00
@offlineshop_ext.put("/api/v1/offlineshop/method")
2021-10-17 18:33:29 +01:00
async def api_set_method(
data: CreateMethodData, wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-08-21 00:34:48 +01:00
method = data.method
2021-08-21 00:34:48 +01:00
wordlist = data.wordlist.split("\n") if data.wordlist else None
wordlist = [word.strip() for word in wordlist if word.strip()]
2021-09-16 19:42:05 +02:00
shop = await get_or_create_shop_by_wallet(wallet.wallet.id)
if not shop:
2021-09-16 19:42:05 +02:00
raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
updated_shop = await set_method(shop.id, method, "\n".join(wordlist))
if not updated_shop:
2021-09-16 19:42:05 +02:00
raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
ShopCounter.reset(updated_shop)