refactor: move business logic logic to helpers.py

This commit is contained in:
Vlad Stan
2022-11-25 11:25:40 +02:00
parent 6f5c9a7798
commit 1aecedac84
3 changed files with 34 additions and 33 deletions
+29
View File
@@ -1,3 +1,6 @@
import httpx
from loguru import logger
from .models import Charges
@@ -22,3 +25,29 @@ def public_charge(charge: Charges):
c["completelink"] = charge.completelink
return c
async def call_webhook(charge: Charges):
async with httpx.AsyncClient() as client:
try:
r = await client.post(
charge.webhook,
json=public_charge(charge),
timeout=40,
)
except AssertionError:
charge.webhook = None
except Exception as e:
logger.warning(f"Failed to call webhook for charge {charge.id}")
logger.warning(e)
async def fetch_onchain_balance(charge: Charges):
endpoint = (
f"{charge.config['mempool_endpoint']}/testnet"
if charge.config["network"] == "Testnet"
else charge.config["mempool_endpoint"]
)
async with httpx.AsyncClient() as client:
r = await client.get(endpoint + "/api/address/" + charge.onchainaddress)
return r.json()["chain_stats"]["funded_txo_sum"]