Updated requests
This commit is contained in:
parent
f598fe29be
commit
f765163486
1 changed files with 27 additions and 43 deletions
|
|
@ -1,9 +1,9 @@
|
||||||
from requests import Response, get, post
|
from requests import Response, get, post
|
||||||
|
from flask import jsonify
|
||||||
from .base import InvoiceResponse, TxStatus, Wallet
|
from .base import InvoiceResponse, TxStatus, Wallet
|
||||||
|
import json
|
||||||
|
|
||||||
|
class OpenNodeWallet(Wallet):
|
||||||
class OpennodeWallet(Wallet):
|
|
||||||
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""
|
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""
|
||||||
|
|
||||||
def __init__(self, *, endpoint: str, admin_key: str, invoice_key: str):
|
def __init__(self, *, endpoint: str, admin_key: str, invoice_key: str):
|
||||||
|
|
@ -11,8 +11,6 @@ class OpennodeWallet(Wallet):
|
||||||
self.auth_admin = {"Authorization": admin_key}
|
self.auth_admin = {"Authorization": admin_key}
|
||||||
self.auth_invoice = {"Authorization": invoice_key}
|
self.auth_invoice = {"Authorization": invoice_key}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create_invoice(self, amount: int, memo: str = "") -> InvoiceResponse:
|
def create_invoice(self, amount: int, memo: str = "") -> InvoiceResponse:
|
||||||
payment_hash, payment_request = None, None
|
payment_hash, payment_request = None, None
|
||||||
r = post(
|
r = post(
|
||||||
|
|
@ -22,54 +20,40 @@ class OpennodeWallet(Wallet):
|
||||||
)
|
)
|
||||||
if r.ok:
|
if r.ok:
|
||||||
data = r.json()
|
data = r.json()
|
||||||
payment_hash, payment_request = data["data"]["id"], data["data"]["lightning_invoice"]["payreq"]
|
payment_hash, payment_request = data['data']['id'], data["data"]["lightning_invoice"]["payreq"]
|
||||||
|
|
||||||
return InvoiceResponse(r, payment_hash, payment_request)
|
return InvoiceResponse(r, payment_hash, payment_request)
|
||||||
|
|
||||||
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
|
||||||
print(f"{self.endpoint}/v1/charge/{payment_hash}")
|
|
||||||
print(self.auth_invoice)
|
|
||||||
r = get(url=f"{self.endpoint}/v1/charge/{payment_hash}", headers=self.auth_invoice)
|
|
||||||
data = r.json()
|
|
||||||
|
|
||||||
if r.ok:
|
|
||||||
data = r.json()
|
|
||||||
if data["data"]["status"] != "paid":
|
|
||||||
return TxStatus(r, None)
|
|
||||||
else:
|
|
||||||
return TxStatus(r, True)
|
|
||||||
|
|
||||||
else:
|
|
||||||
return TxStatus(r, False)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def pay_invoice(self, bolt11: str) -> Response:
|
def pay_invoice(self, bolt11: str) -> Response:
|
||||||
payment_hash = None
|
return post(
|
||||||
r = post(
|
url=f"{self.endpoint}/v2/withdrawals", headers=self.auth_admin, json={"type": "ln", "address": bolt11}
|
||||||
url=f"{self.endpoint}/v2/withdrawals",
|
|
||||||
headers=self.auth_admin,
|
|
||||||
json={"type": "ln", "address": bolt11},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if r.ok:
|
|
||||||
data = r.json()
|
|
||||||
payment_hash = data["data"]["id"]
|
|
||||||
|
|
||||||
return (r, payment_hash)
|
|
||||||
|
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
||||||
|
|
||||||
|
r = get(url=f"{self.endpoint}/v1/charge/{payment_hash}", headers=self.auth_invoice)
|
||||||
|
|
||||||
|
data = r.json()
|
||||||
|
print(data)
|
||||||
|
print(f"{self.endpoint}/v1/charge/{payment_hash} {self.auth_invoice}")
|
||||||
|
if not r.ok:
|
||||||
|
return TxStatus(r, None)
|
||||||
|
|
||||||
|
|
||||||
|
statuses = {"processing": None, "paid": True, "unpaid": False}
|
||||||
|
return TxStatus(r, statuses[r.json()["data"]["status"]])
|
||||||
|
|
||||||
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
||||||
|
|
||||||
r = get(url=f"{self.endpoint}/v1/withdrawal/{payment_hash}", headers=self.auth_admin)
|
r = get(url=f"{self.endpoint}/v1/withdrawal/{payment_hash}", headers=self.auth_admin)
|
||||||
|
|
||||||
print(f"{self.endpoint}/v1/withdrawal/{payment_hash}")
|
if not r.ok:
|
||||||
|
return TxStatus(r, None)
|
||||||
|
|
||||||
if r.ok:
|
statuses = {"pending": None, "confirmed": True, "error": False, "failed": False}
|
||||||
data = r.json()
|
return TxStatus(r, statuses[r.json()["data"]["status"]])
|
||||||
if ["data"]["status"] != "confirmed":
|
|
||||||
return TxStatus(r, None)
|
|
||||||
else:
|
|
||||||
return TxStatus(r, True)
|
|
||||||
else:
|
|
||||||
return TxStatus(r, False)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue