-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathservices_wrapper.py
More file actions
45 lines (37 loc) · 1.53 KB
/
Copy pathservices_wrapper.py
File metadata and controls
45 lines (37 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import time
import httpx
from lnbits.settings import settings
from loguru import logger
from .crud import get_wrapper_assetlinks_cache, set_wrapper_assetlinks_cache
WRAPPER_ASSETLINKS_URL = (
"https://github.com/lnbits/TPoS-Stripe-Tap-to-Pay-Wrapper-Stripev5"
"/releases/latest/download/assetlinks.json"
)
WRAPPER_ASSETLINKS_CACHE_SECONDS = 60 * 60
async def fetch_wrapper_assetlinks() -> dict | list:
now = int(time.time())
cached = await get_wrapper_assetlinks_cache()
if cached:
cached_assetlinks, cached_at = cached
cache_fresh = now - cached_at < WRAPPER_ASSETLINKS_CACHE_SECONDS
if cache_fresh:
return cached_assetlinks
try:
async with httpx.AsyncClient(
follow_redirects=True, headers={"User-Agent": settings.user_agent}
) as client:
resp = await client.get(WRAPPER_ASSETLINKS_URL, timeout=10)
resp.raise_for_status()
assetlinks = resp.json()
except Exception as exc:
if cached:
logger.warning(f"Using cached TPoS wrapper assetlinks.json: {exc!s}")
return cached[0]
raise RuntimeError("Unable to fetch TPoS wrapper assetlinks.json.") from exc
if not isinstance(assetlinks, (dict, list)):
if cached:
logger.warning("Using cached TPoS wrapper assetlinks.json: invalid JSON")
return cached[0]
raise RuntimeError("TPoS wrapper assetlinks.json is not valid JSON.")
await set_wrapper_assetlinks_cache(assetlinks, now)
return assetlinks