improve on openapi metadata (#1795)

This commit is contained in:
dni ⚡ 2023-08-24 11:52:12 +02:00 committed by GitHub
parent 4e6f229db2
commit 39d717e34c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 25 deletions

View file

@ -62,7 +62,7 @@ from .tasks import (
def create_app() -> FastAPI: def create_app() -> FastAPI:
configure_logger() configure_logger()
app = FastAPI( app = FastAPI(
title="LNbits API", title=settings.lnbits_title,
description=( description=(
"API for LNbits, the free and open source bitcoin wallet and " "API for LNbits, the free and open source bitcoin wallet and "
"accounts system with plugins." "accounts system with plugins."

View file

@ -5,7 +5,7 @@ from lnbits.db import Database
db = Database("database") db = Database("database")
core_app: APIRouter = APIRouter() core_app: APIRouter = APIRouter(tags=["Core"])
core_app_extra: CoreAppExtra = CoreAppExtra() core_app_extra: CoreAppExtra = CoreAppExtra()

View file

@ -319,3 +319,12 @@ class CreateInvoice(BaseModel):
extra: Optional[dict] = None extra: Optional[dict] = None
webhook: Optional[str] = None webhook: Optional[str] = None
bolt11: Optional[str] = None bolt11: Optional[str] = None
class CreateTopup(BaseModel):
id: str
amount: int
class CreateLnurlAuth(BaseModel):
callback: str

View file

@ -6,12 +6,12 @@ from subprocess import Popen
from typing import Optional from typing import Optional
from urllib.parse import urlparse from urllib.parse import urlparse
from fastapi import Body, Depends from fastapi import Depends
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from lnbits.core.crud import get_wallet from lnbits.core.crud import get_wallet
from lnbits.core.models import User from lnbits.core.models import CreateTopup, User
from lnbits.core.services import ( from lnbits.core.services import (
get_balance_delta, get_balance_delta,
update_cached_settings, update_cached_settings,
@ -25,7 +25,12 @@ from .. import core_app, core_app_extra
from ..crud import delete_admin_settings, get_admin_settings, update_admin_settings from ..crud import delete_admin_settings, get_admin_settings, update_admin_settings
@core_app.get("/admin/api/v1/audit", dependencies=[Depends(check_admin)]) @core_app.get(
"/admin/api/v1/audit",
name="Audit",
description="show the current balance of the node and the LNbits database",
dependencies=[Depends(check_admin)],
)
async def api_auditor(): async def api_auditor():
try: try:
delta, node_balance, total_balance = await get_balance_delta() delta, node_balance, total_balance = await get_balance_delta()
@ -86,14 +91,13 @@ async def api_restart_server() -> dict[str, str]:
@core_app.put( @core_app.put(
"/admin/api/v1/topup/", "/admin/api/v1/topup/",
name="Topup",
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
dependencies=[Depends(check_super_user)], dependencies=[Depends(check_super_user)],
) )
async def api_topup_balance( async def api_topup_balance(data: CreateTopup) -> dict[str, str]:
id: str = Body(...), amount: int = Body(...)
) -> dict[str, str]:
try: try:
await get_wallet(id) await get_wallet(data.id)
except Exception: except Exception:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="wallet does not exist." status_code=HTTPStatus.FORBIDDEN, detail="wallet does not exist."
@ -104,7 +108,7 @@ async def api_topup_balance(
status_code=HTTPStatus.FORBIDDEN, detail="VoidWallet active" status_code=HTTPStatus.FORBIDDEN, detail="VoidWallet active"
) )
await update_wallet_balance(wallet_id=id, amount=int(amount)) await update_wallet_balance(wallet_id=data.id, amount=int(data.amount))
return {"status": "Success"} return {"status": "Success"}

View file

@ -29,10 +29,10 @@ from lnbits.core.helpers import (
stop_extension_background_work, stop_extension_background_work,
) )
from lnbits.core.models import ( from lnbits.core.models import (
Callback,
ConversionData, ConversionData,
CreateInvoice, CreateInvoice,
CreateLnurl, CreateLnurl,
CreateLnurlAuth,
DecodePayment, DecodePayment,
Payment, Payment,
PaymentFilters, PaymentFilters,
@ -649,7 +649,7 @@ async def api_payments_decode(data: DecodePayment, response: Response):
@core_app.post("/api/v1/lnurlauth") @core_app.post("/api/v1/lnurlauth")
async def api_perform_lnurlauth( async def api_perform_lnurlauth(
data: Callback, wallet: WalletTypeInfo = Depends(require_admin_key) data: CreateLnurlAuth, wallet: WalletTypeInfo = Depends(require_admin_key)
): ):
err = await perform_lnurlauth(data.callback, wallet=wallet) err = await perform_lnurlauth(data.callback, wallet=wallet)
if err: if err:
@ -906,7 +906,11 @@ async def delete_extension_db(ext_id: str):
# TINYURL # TINYURL
@core_app.post("/api/v1/tinyurl") @core_app.post(
"/api/v1/tinyurl",
name="Tinyurl",
description="creates a tinyurl",
)
async def api_create_tinyurl( async def api_create_tinyurl(
url: str, endless: bool = False, wallet: WalletTypeInfo = Depends(get_key_type) url: str, endless: bool = False, wallet: WalletTypeInfo = Depends(get_key_type)
): ):
@ -923,7 +927,11 @@ async def api_create_tinyurl(
) )
@core_app.get("/api/v1/tinyurl/{tinyurl_id}") @core_app.get(
"/api/v1/tinyurl/{tinyurl_id}",
name="Tinyurl",
description="get a tinyurl by id",
)
async def api_get_tinyurl( async def api_get_tinyurl(
tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type) tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
): ):
@ -941,7 +949,11 @@ async def api_get_tinyurl(
) )
@core_app.delete("/api/v1/tinyurl/{tinyurl_id}") @core_app.delete(
"/api/v1/tinyurl/{tinyurl_id}",
name="Tinyurl",
description="delete a tinyurl by id",
)
async def api_delete_tinyurl( async def api_delete_tinyurl(
tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type) tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
): ):
@ -960,16 +972,17 @@ async def api_delete_tinyurl(
) )
@core_app.get("/t/{tinyurl_id}") @core_app.get(
"/t/{tinyurl_id}",
name="Tinyurl",
description="redirects a tinyurl by id",
)
async def api_tinyurl(tinyurl_id: str): async def api_tinyurl(tinyurl_id: str):
try: tinyurl = await get_tinyurl(tinyurl_id)
tinyurl = await get_tinyurl(tinyurl_id) if tinyurl:
if tinyurl: response = RedirectResponse(url=tinyurl.url)
response = RedirectResponse(url=tinyurl.url) return response
return response else:
else:
return
except Exception:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="unable to find tinyurl" status_code=HTTPStatus.NOT_FOUND, detail="unable to find tinyurl"
) )

View file

@ -35,7 +35,9 @@ from ..crud import (
) )
from ..services import pay_invoice, redeem_lnurl_withdraw from ..services import pay_invoice, redeem_lnurl_withdraw
core_html_routes: APIRouter = APIRouter(tags=["Core NON-API Website Routes"]) core_html_routes: APIRouter = APIRouter(
tags=["Core NON-API Website Routes"], include_in_schema=False
)
@core_html_routes.get("/favicon.ico", response_class=FileResponse) @core_html_routes.get("/favicon.ico", response_class=FileResponse)

View file

@ -268,6 +268,7 @@ class EnvSettings(LNbitsSettings):
host: str = Field(default="127.0.0.1") host: str = Field(default="127.0.0.1")
port: int = Field(default=5000) port: int = Field(default=5000)
forwarded_allow_ips: str = Field(default="*") forwarded_allow_ips: str = Field(default="*")
lnbits_title: str = Field(default="LNbits API")
lnbits_path: str = Field(default=".") lnbits_path: str = Field(default=".")
lnbits_commit: str = Field(default="unknown") lnbits_commit: str = Field(default="unknown")
super_user: str = Field(default="") super_user: str = Field(default="")