make all methods from all wallets async.

This commit is contained in:
fiatjaf
2021-03-24 01:01:09 -03:00
parent 42bd5ea989
commit d4d069fc77
15 changed files with 219 additions and 187 deletions
+27 -23
View File
@@ -24,10 +24,11 @@ class LNPayWallet(Wallet):
self.wallet_key = getenv("LNPAY_WALLET_KEY") or getenv("LNPAY_ADMIN_KEY")
self.auth = {"X-Api-Key": getenv("LNPAY_API_KEY")}
def status(self) -> StatusResponse:
async def status(self) -> StatusResponse:
url = f"{self.endpoint}/wallet/{self.wallet_key}"
try:
r = httpx.get(url, headers=self.auth, timeout=60)
async with httpx.AsyncClient() as client:
r = await client.get(url, headers=self.auth, timeout=60)
except (httpx.ConnectError, httpx.RequestError):
return StatusResponse(f"Unable to connect to '{url}'", 0)
@@ -43,7 +44,7 @@ class LNPayWallet(Wallet):
return StatusResponse(None, data["balance"] * 1000)
def create_invoice(
async def create_invoice(
self,
amount: int,
memo: Optional[str] = None,
@@ -55,12 +56,13 @@ class LNPayWallet(Wallet):
else:
data["memo"] = memo or ""
r = httpx.post(
f"{self.endpoint}/wallet/{self.wallet_key}/invoice",
headers=self.auth,
json=data,
timeout=60,
)
async with httpx.AsyncClient() as client:
r = await client.post(
f"{self.endpoint}/wallet/{self.wallet_key}/invoice",
headers=self.auth,
json=data,
timeout=60,
)
ok, checking_id, payment_request, error_message = (
r.status_code == 201,
None,
@@ -74,13 +76,14 @@ class LNPayWallet(Wallet):
return InvoiceResponse(ok, checking_id, payment_request, error_message)
def pay_invoice(self, bolt11: str) -> PaymentResponse:
r = httpx.post(
f"{self.endpoint}/wallet/{self.wallet_key}/withdraw",
headers=self.auth,
json={"payment_request": bolt11},
timeout=180,
)
async def pay_invoice(self, bolt11: str) -> PaymentResponse:
async with httpx.AsyncClient() as client:
r = await client.post(
f"{self.endpoint}/wallet/{self.wallet_key}/withdraw",
headers=self.auth,
json={"payment_request": bolt11},
timeout=180,
)
try:
data = r.json()
@@ -97,14 +100,15 @@ class LNPayWallet(Wallet):
preimage = data["lnTx"]["payment_preimage"]
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return self.get_payment_status(checking_id)
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return await self.get_payment_status(checking_id)
def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = httpx.get(
url=f"{self.endpoint}/lntx/{checking_id}?fields=settled",
headers=self.auth,
)
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
async with httpx.AsyncClient() as client:
r = await client.get(
url=f"{self.endpoint}/lntx/{checking_id}?fields=settled",
headers=self.auth,
)
if r.is_error:
return PaymentStatus(None)