Work on api
This commit is contained in:
parent
55de179730
commit
b5157ad8dc
1 changed files with 78 additions and 25 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
|
import math
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import json
|
import json
|
||||||
import httpx
|
import httpx
|
||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
from fastapi import Query
|
from fastapi import Query
|
||||||
from fastapi.params import Depends
|
from fastapi.params import Depends
|
||||||
from lnurl import decode as decode_lnurl
|
from lnurl import decode as decode_lnurl
|
||||||
|
|
@ -26,7 +28,7 @@ from ...settings import LNBITS_PATH
|
||||||
|
|
||||||
@gerty_ext.get("/api/v1/gerty", status_code=HTTPStatus.OK)
|
@gerty_ext.get("/api/v1/gerty", status_code=HTTPStatus.OK)
|
||||||
async def api_gertys(
|
async def api_gertys(
|
||||||
all_wallets: bool = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)
|
all_wallets: bool = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)
|
||||||
):
|
):
|
||||||
wallet_ids = [wallet.wallet.id]
|
wallet_ids = [wallet.wallet.id]
|
||||||
if all_wallets:
|
if all_wallets:
|
||||||
|
|
@ -38,13 +40,12 @@ async def api_gertys(
|
||||||
@gerty_ext.post("/api/v1/gerty", status_code=HTTPStatus.CREATED)
|
@gerty_ext.post("/api/v1/gerty", status_code=HTTPStatus.CREATED)
|
||||||
@gerty_ext.put("/api/v1/gerty/{gerty_id}", status_code=HTTPStatus.OK)
|
@gerty_ext.put("/api/v1/gerty/{gerty_id}", status_code=HTTPStatus.OK)
|
||||||
async def api_link_create_or_update(
|
async def api_link_create_or_update(
|
||||||
data: Gerty,
|
data: Gerty,
|
||||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||||
gerty_id: str = Query(None),
|
gerty_id: str = Query(None),
|
||||||
):
|
):
|
||||||
if gerty_id:
|
if gerty_id:
|
||||||
gerty = await get_gerty(gerty_id)
|
gerty = await get_gerty(gerty_id)
|
||||||
logger.debug(gerty)
|
|
||||||
if not gerty:
|
if not gerty:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist"
|
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist"
|
||||||
|
|
@ -63,9 +64,10 @@ async def api_link_create_or_update(
|
||||||
|
|
||||||
return {**gerty.dict()}
|
return {**gerty.dict()}
|
||||||
|
|
||||||
|
|
||||||
@gerty_ext.delete("/api/v1/gerty/{gerty_id}")
|
@gerty_ext.delete("/api/v1/gerty/{gerty_id}")
|
||||||
async def api_gerty_delete(
|
async def api_gerty_delete(
|
||||||
gerty_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
|
gerty_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||||
):
|
):
|
||||||
gerty = await get_gerty(gerty_id)
|
gerty = await get_gerty(gerty_id)
|
||||||
|
|
||||||
|
|
@ -84,44 +86,50 @@ async def api_gerty_delete(
|
||||||
#######################
|
#######################
|
||||||
|
|
||||||
with open(os.path.join(LNBITS_PATH, 'extensions/gerty/static/satoshi.json')) as fd:
|
with open(os.path.join(LNBITS_PATH, 'extensions/gerty/static/satoshi.json')) as fd:
|
||||||
satoshiQuotes = json.load(fd)
|
satoshiQuotes = json.load(fd)
|
||||||
|
|
||||||
|
|
||||||
@gerty_ext.get("/api/v1/gerty/satoshiquote", status_code=HTTPStatus.OK)
|
@gerty_ext.get("/api/v1/gerty/satoshiquote", status_code=HTTPStatus.OK)
|
||||||
async def api_gerty_satoshi():
|
async def api_gerty_satoshi():
|
||||||
return satoshiQuotes[random.randint(0, 100)]
|
return satoshiQuotes[random.randint(0, 100)]
|
||||||
|
|
||||||
|
|
||||||
@gerty_ext.get("/api/v1/gerty/{gerty_id}")
|
@gerty_ext.get("/api/v1/gerty/{gerty_id}")
|
||||||
async def api_gerty_json(
|
async def api_gerty_json(
|
||||||
gerty_id: str
|
gerty_id: str,
|
||||||
|
p: int = None # page number
|
||||||
):
|
):
|
||||||
gerty = await get_gerty(gerty_id)
|
gerty = await get_gerty(gerty_id)
|
||||||
logger.debug(gerty.wallet)
|
|
||||||
if not gerty:
|
if not gerty:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist."
|
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist."
|
||||||
)
|
)
|
||||||
gertyReturn = []
|
|
||||||
|
|
||||||
# Get Wallet info
|
display_preferences = json.loads(gerty.display_preferences)
|
||||||
wallets = []
|
|
||||||
if gerty.lnbits_wallets != "":
|
|
||||||
for lnbits_wallet in json.loads(gerty.lnbits_wallets):
|
|
||||||
wallet = await get_wallet_for_key(key=lnbits_wallet)
|
|
||||||
if wallet:
|
|
||||||
wallets.append({
|
|
||||||
"name": wallet.name,
|
|
||||||
"balance": wallet.balance_msat,
|
|
||||||
"inkey": wallet.inkey,
|
|
||||||
})
|
|
||||||
|
|
||||||
#Get Satoshi quotes
|
enabled_screen_count = 0
|
||||||
|
|
||||||
|
enabled_screens = []
|
||||||
|
|
||||||
|
for screen_slug in display_preferences:
|
||||||
|
is_screen_enabled = display_preferences[screen_slug]
|
||||||
|
if is_screen_enabled:
|
||||||
|
enabled_screen_count += 1
|
||||||
|
enabled_screens.append(screen_slug)
|
||||||
|
|
||||||
|
get_screen_text(p, enabled_screens)
|
||||||
|
|
||||||
|
next_screen_number = 0 if ((p + 1) >= enabled_screen_count) else p + 1;
|
||||||
|
|
||||||
|
# Get Satoshi quotes
|
||||||
satoshi = []
|
satoshi = []
|
||||||
# if gerty.sats_quote:
|
# if gerty.sats_quote:
|
||||||
# quote = await api_gerty_satoshi()
|
# quote = await api_gerty_satoshi()
|
||||||
# if quote:
|
# if quote:
|
||||||
# satoshi.append(await api_gerty_satoshi())
|
# satoshi.append(await api_gerty_satoshi())
|
||||||
|
|
||||||
#Get Exchange Value
|
# Get Exchange Value
|
||||||
exchange = []
|
exchange = []
|
||||||
# if gerty.exchange != "":
|
# if gerty.exchange != "":
|
||||||
# try:
|
# try:
|
||||||
|
|
@ -160,6 +168,51 @@ async def api_gerty_json(
|
||||||
# if r:
|
# if r:
|
||||||
# ln.append(r.json())
|
# ln.append(r.json())
|
||||||
|
|
||||||
return {"name":gerty.name}
|
return {
|
||||||
|
"settings": {
|
||||||
|
"refreshTime": gerty.refresh_time,
|
||||||
|
"requestTimestamp": math.ceil(time.time()),
|
||||||
|
"nextScreenNumber": next_screen_number,
|
||||||
|
"name": gerty.name
|
||||||
|
},
|
||||||
|
"screen": {
|
||||||
|
"slug": "x",
|
||||||
|
"group": "x",
|
||||||
|
"text": [
|
||||||
|
{
|
||||||
|
"value": "Craig Steven Wright is a liar and\na fraud",
|
||||||
|
"size": 20,
|
||||||
|
"x": 20,
|
||||||
|
"y": 70
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_screen_text(screen_num: int, display_preferences: dict):
|
||||||
|
# first get the relevant slug from the display_preferences
|
||||||
|
screen_slug = list(display_preferences)[screen_num]
|
||||||
|
# logger.debug('screen_slug')
|
||||||
|
# logger.debug(screen_slug)
|
||||||
|
if screen_slug == "lnbits_wallets_balance":
|
||||||
|
|
||||||
|
return screen_slug
|
||||||
|
|
||||||
|
def get_lnbits_wallet_balances(gerty):
|
||||||
|
# Get Wallet info
|
||||||
|
wallets = []
|
||||||
|
if gerty.lnbits_wallets != "":
|
||||||
|
logger.debug("wallets")
|
||||||
|
logger.debug(gerty.lnbits_wallets)
|
||||||
|
for lnbits_wallet in json.loads(gerty.lnbits_wallets):
|
||||||
|
wallet = await get_wallet_for_key(key=lnbits_wallet)
|
||||||
|
if wallet:
|
||||||
|
wallets.append({
|
||||||
|
"name": wallet.name,
|
||||||
|
"balance": wallet.balance_msat,
|
||||||
|
"inkey": wallet.inkey,
|
||||||
|
})
|
||||||
|
logger.debug(lnbits_wallet)
|
||||||
|
return wallets
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue