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
+28 -22
View File
@@ -25,8 +25,10 @@ class LNbitsWallet(Wallet):
)
self.key = {"X-Api-Key": key}
def status(self) -> StatusResponse:
r = httpx.get(url=f"{self.endpoint}/api/v1/wallet", headers=self.key)
async def status(self) -> StatusResponse:
async with httpx.AsyncClient() as client:
r = await client.get(url=f"{self.endpoint}/api/v1/wallet", headers=self.key)
try:
data = r.json()
except:
@@ -39,7 +41,7 @@ class LNbitsWallet(Wallet):
return StatusResponse(None, data["balance"])
def create_invoice(
async def create_invoice(
self,
amount: int,
memo: Optional[str] = None,
@@ -51,11 +53,12 @@ class LNbitsWallet(Wallet):
else:
data["memo"] = memo or ""
r = httpx.post(
url=f"{self.endpoint}/api/v1/payments",
headers=self.key,
json=data,
)
async with httpx.AsyncClient() as client:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments",
headers=self.key,
json=data,
)
ok, checking_id, payment_request, error_message = (
not r.is_error,
None,
@@ -71,12 +74,13 @@ class LNbitsWallet(Wallet):
return InvoiceResponse(ok, checking_id, payment_request, error_message)
def pay_invoice(self, bolt11: str) -> PaymentResponse:
r = httpx.post(
url=f"{self.endpoint}/api/v1/payments",
headers=self.key,
json={"out": True, "bolt11": bolt11},
)
async def pay_invoice(self, bolt11: str) -> PaymentResponse:
async with httpx.AsyncClient() as client:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments",
headers=self.key,
json={"out": True, "bolt11": bolt11},
)
ok, checking_id, fee_msat, error_message = not r.is_error, None, 0, None
if r.is_error:
@@ -87,20 +91,22 @@ class LNbitsWallet(Wallet):
return PaymentResponse(ok, checking_id, fee_msat, error_message)
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
r = httpx.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.key
)
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
async with httpx.AsyncClient() as client:
r = await client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.key
)
if r.is_error:
return PaymentStatus(None)
return PaymentStatus(r.json()["paid"])
def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = httpx.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.key
)
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
async with httpx.AsyncClient() as client:
r = await client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.key
)
if r.is_error:
return PaymentStatus(None)