introduce Wallet.normalize_endpoint to remove code duplication

This commit is contained in:
Pavol Rusnak 2023-12-24 15:03:58 +01:00
parent 824a8fa7c8
commit 8eabf53642
12 changed files with 25 additions and 27 deletions

View File

@ -25,8 +25,7 @@ class AlbyWallet(Wallet):
if not settings.alby_access_token:
raise ValueError("cannot initialize AlbyWallet: missing alby_access_token")
endpoint = settings.alby_api_endpoint
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.endpoint = self.normalize_endpoint(settings.alby_api_endpoint)
self.auth = {
"Authorization": "Bearer " + settings.alby_access_token,
"User-Agent": settings.user_agent,

View File

@ -95,6 +95,14 @@ class Wallet(ABC):
def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
pass
def normalize_endpoint(self, endpoint: str, add_proto=True) -> str:
endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
if add_proto:
endpoint = (
f"https://{endpoint}" if not endpoint.startswith("http") else endpoint
)
return endpoint
class Unsupported(Exception):
pass

View File

@ -24,7 +24,7 @@ class ClicheWallet(Wallet):
if not settings.cliche_endpoint:
raise ValueError("cannot initialize ClicheWallet: missing cliche_endpoint")
self.endpoint = settings.cliche_endpoint
self.endpoint = self.normalize_endpoint(settings.cliche_endpoint)
async def status(self) -> StatusResponse:
try:

View File

@ -40,11 +40,7 @@ class CoreLightningRestWallet(Wallet):
"invalid corelightning_rest_macaroon provided"
)
url = settings.corelightning_rest_url
self.url = url[:-1] if url.endswith("/") else url
self.url = (
f"https://{self.url}" if not self.url.startswith("http") else self.url
)
self.url = self.normalize_endpoint(settings.corelightning_rest_url)
headers = {
"macaroon": macaroon,
"encodingtype": "hex",

View File

@ -35,8 +35,7 @@ class EclairWallet(Wallet):
if not settings.eclair_pass:
raise ValueError("cannot initialize EclairWallet: missing eclair_pass")
url = settings.eclair_url
self.url = url[:-1] if url.endswith("/") else url
self.url = self.normalize_endpoint(settings.eclair_url)
self.ws_url = f"ws://{urllib.parse.urlsplit(self.url).netloc}/ws"
password = settings.eclair_pass

View File

@ -32,8 +32,7 @@ class LNbitsWallet(Wallet):
"cannot initialize LNbitsWallet: "
"missing lnbits_key or lnbits_admin_key or lnbits_invoice_key"
)
endpoint = settings.lnbits_endpoint
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.endpoint = self.normalize_endpoint(settings.lnbits_endpoint)
self.headers = {"X-Api-Key": key, "User-Agent": settings.user_agent}
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.headers)

View File

@ -89,8 +89,9 @@ class LndWallet(Wallet):
"lnd_invoice_macaroon or lnd_grpc_macaroon_encrypted"
)
endpoint = settings.lnd_grpc_endpoint
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.endpoint = self.normalize_endpoint(
settings.lnd_grpc_endpoint, add_proto=False
)
self.port = int(settings.lnd_grpc_port)
self.macaroon = load_macaroon(macaroon)
cert = open(cert_path, "rb").read()

View File

@ -58,12 +58,7 @@ class LndRestWallet(Wallet):
"This only works if you have a publicly issued certificate."
)
endpoint = settings.lnd_rest_endpoint
endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
endpoint = (
f"https://{endpoint}" if not endpoint.startswith("http") else endpoint
)
self.endpoint = endpoint
self.endpoint = self.normalize_endpoint(settings.lnd_rest_endpoint)
# if no cert provided it should be public so we set verify to True
# and it will still check for validity of certificate and fail if its not valid

View File

@ -35,8 +35,8 @@ class LNPayWallet(Wallet):
)
self.wallet_key = wallet_key
endpoint = settings.lnpay_api_endpoint
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.endpoint = self.normalize_endpoint(settings.lnpay_api_endpoint)
headers = {
"X-Api-Key": settings.lnpay_api_key,
"User-Agent": settings.user_agent,

View File

@ -35,8 +35,8 @@ class LnTipsWallet(Wallet):
"missing lntips_api_key or lntips_admin_key or lntips_invoice_key"
)
endpoint = settings.lntips_api_endpoint
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.endpoint = self.normalize_endpoint(settings.lntips_api_endpoint)
headers = {
"Authorization": f"Basic {key}",
"User-Agent": settings.user_agent,

View File

@ -36,8 +36,8 @@ class OpenNodeWallet(Wallet):
)
self.key = key
endpoint = settings.opennode_api_endpoint
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.endpoint = self.normalize_endpoint(settings.opennode_api_endpoint)
headers = {
"Authorization": self.key,
"User-Agent": settings.user_agent,

View File

@ -33,7 +33,8 @@ class SparkWallet(Wallet):
if not settings.spark_token:
raise ValueError("cannot initialize SparkWallet: missing spark_token")
url = settings.spark_url.replace("/rpc", "")
url = self.normalize_endpoint(settings.spark_url)
url = url.replace("/rpc", "")
self.token = settings.spark_token
headers = {"X-Access": self.token, "User-Agent": settings.user_agent}