Skip to content

Commit

Permalink
Merge pull request #2 from motorina0/main
Browse files Browse the repository at this point in the history
refactor: remove direct dependencies to `lnbits.extensions.satspay`
  • Loading branch information
dni authored Feb 21, 2023
2 parents 0857065 + 4402c42 commit b727456
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
6 changes: 5 additions & 1 deletion crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ async def post_donation(donation_id: str) -> dict:
else:
return {"message": "Unsopported servicename"}
await db.execute(
"UPDATE streamalerts.Donations SET posted = 1 WHERE id = ?", (donation_id,)
"UPDATE streamalerts.Donations SET posted = ? WHERE id = ?",
(
True,
donation_id,
),
)
return response.json()

Expand Down
28 changes: 28 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import httpx

from lnbits.app import settings

from .models import ChargeStatus


async def create_charge(data: dict, api_key: str) -> str:
async with httpx.AsyncClient() as client:
headers = {"X-API-KEY": api_key}
r = await client.post(
url=f"http://{settings.host}:{settings.port}/satspay/api/v1/charge",
headers=headers,
json=data,
)
r.raise_for_status()
return r.json()["id"]


async def get_charge_status(charge_id: str, api_key: str) -> ChargeStatus:
async with httpx.AsyncClient() as client:
headers = {"X-API-KEY": api_key}
r = await client.get(
url=f"http://{settings.host}:{settings.port}/satspay/api/v1/charge/{charge_id}",
headers=headers,
)
r.raise_for_status()
return ChargeStatus.parse_obj(r.json())
5 changes: 5 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ class Service(BaseModel):
@classmethod
def from_row(cls, row: Row) -> "Service":
return cls(**dict(row))


class ChargeStatus(BaseModel):
id: str
paid: bool
41 changes: 24 additions & 17 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
from starlette.requests import Request
from starlette.responses import RedirectResponse

from lnbits.core.crud import get_user
from lnbits.core.crud import get_user, get_wallet
from lnbits.decorators import WalletTypeInfo, get_key_type

# todo: use the API, not direct import
from lnbits.extensions.satspay.models import CreateCharge # type: ignore
from lnbits.utils.exchange_rates import btc_price

# todo: use the API, not direct import
from ..satspay.crud import create_charge, get_charge # type: ignore
from . import streamalerts_ext
from .crud import (
authenticate_service,
Expand All @@ -31,6 +26,7 @@
update_donation,
update_service,
)
from .helpers import create_charge, get_charge_status
from .models import CreateDonation, CreateService, ValidateDonation


Expand Down Expand Up @@ -119,17 +115,19 @@ async def api_create_donation(data: CreateDonation, request: Request):
name = data.name if data.name else "Anonymous"

description = f"{sats} sats donation from {name} to {service.twitchuser}"
create_charge_data = CreateCharge(
amount=sats,
completelink=f"https://twitch.tv/{service.twitchuser}",
completelinktext="Back to Stream!",
webhook=webhook_base + "/streamalerts/api/v1/postdonation",
description=description,
create_charge_data = {
"amount": sats,
"completelink": f"https://twitch.tv/{service.twitchuser}",
"completelinktext": "Back to Stream!",
"webhook": webhook_base + "/streamalerts/api/v1/postdonation",
"description": description,
**charge_details,
)
charge = await create_charge(user=charge_details["user"], data=create_charge_data)
}
wallet = await get_wallet(service.wallet)
assert wallet, f"Could not fetch wallet: {service.wallet}"
charge_id = await create_charge(data=create_charge_data, api_key=wallet.inkey)
await create_donation(
id=charge.id,
id=charge_id,
wallet=service.wallet,
message=message,
name=name,
Expand All @@ -138,7 +136,7 @@ async def api_create_donation(data: CreateDonation, request: Request):
amount=amount,
service=data.service,
)
return {"redirect_url": f"/satspay/{charge.id}"}
return {"redirect_url": f"/satspay/{charge_id}"}


@streamalerts_ext.post("/api/v1/postdonation")
Expand All @@ -147,7 +145,16 @@ async def api_post_donation(request: Request, data: ValidateDonation):
This endpoint acts as a webhook for the SatsPayServer extension."""

donation_id = data.id
charge = await get_charge(donation_id)
donation = await get_donation(donation_id)
if not donation:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"Donation '{donation_id}' not found!",
)
wallet = await get_wallet(donation.wallet)
assert wallet, f"Could not fetch wallet: {donation.wallet}"

charge = await get_charge_status(donation_id, wallet.inkey)
if charge and charge.paid:
return await post_donation(donation_id)
else:
Expand Down

0 comments on commit b727456

Please sign in to comment.