|
@@ -1,5 +1,6 @@
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
+import os
|
|
|
import requests
|
|
import requests
|
|
|
|
|
|
|
|
from .bitstamp_rate_limit import throttle_bitstamp_request
|
|
from .bitstamp_rate_limit import throttle_bitstamp_request
|
|
@@ -7,20 +8,41 @@ from .storage import get_connection
|
|
|
|
|
|
|
|
BITSTAMP_BASE_URL = "https://www.bitstamp.net"
|
|
BITSTAMP_BASE_URL = "https://www.bitstamp.net"
|
|
|
METADATA_REFRESH_SECONDS = 24 * 60 * 60
|
|
METADATA_REFRESH_SECONDS = 24 * 60 * 60
|
|
|
|
|
+METADATA_CACHE_TTL_SECONDS = 60
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_currencies() -> list[dict]:
|
|
def fetch_currencies() -> list[dict]:
|
|
|
|
|
+ cache_key = "bitstamp:metadata:currencies"
|
|
|
|
|
+ cached = __import__("exec_mcp.repo", fromlist=["cache_get"]).cache_get(cache_key)
|
|
|
|
|
+ if cached is not None:
|
|
|
|
|
+ return cached
|
|
|
throttle_bitstamp_request()
|
|
throttle_bitstamp_request()
|
|
|
response = requests.get(f"{BITSTAMP_BASE_URL}/api/v2/currencies/", timeout=30)
|
|
response = requests.get(f"{BITSTAMP_BASE_URL}/api/v2/currencies/", timeout=30)
|
|
|
response.raise_for_status()
|
|
response.raise_for_status()
|
|
|
- return response.json()
|
|
|
|
|
|
|
+ payload = response.json()
|
|
|
|
|
+ __import__("exec_mcp.repo", fromlist=["cache_put"]).cache_put(
|
|
|
|
|
+ cache_key,
|
|
|
|
|
+ payload,
|
|
|
|
|
+ max(int(os.getenv("BITSTAMP_METADATA_CACHE_TTL_SECONDS", str(METADATA_CACHE_TTL_SECONDS))), 0),
|
|
|
|
|
+ )
|
|
|
|
|
+ return payload
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_markets() -> list[dict]:
|
|
def fetch_markets() -> list[dict]:
|
|
|
|
|
+ cache_key = "bitstamp:metadata:markets"
|
|
|
|
|
+ cached = __import__("exec_mcp.repo", fromlist=["cache_get"]).cache_get(cache_key)
|
|
|
|
|
+ if cached is not None:
|
|
|
|
|
+ return cached
|
|
|
throttle_bitstamp_request()
|
|
throttle_bitstamp_request()
|
|
|
response = requests.get(f"{BITSTAMP_BASE_URL}/api/v2/markets/", timeout=30)
|
|
response = requests.get(f"{BITSTAMP_BASE_URL}/api/v2/markets/", timeout=30)
|
|
|
response.raise_for_status()
|
|
response.raise_for_status()
|
|
|
- return response.json()
|
|
|
|
|
|
|
+ payload = response.json()
|
|
|
|
|
+ __import__("exec_mcp.repo", fromlist=["cache_put"]).cache_put(
|
|
|
|
|
+ cache_key,
|
|
|
|
|
+ payload,
|
|
|
|
|
+ max(int(os.getenv("BITSTAMP_METADATA_CACHE_TTL_SECONDS", str(METADATA_CACHE_TTL_SECONDS))), 0),
|
|
|
|
|
+ )
|
|
|
|
|
+ return payload
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_metadata(kind: str, payload: list[dict]) -> None:
|
|
def save_metadata(kind: str, payload: list[dict]) -> None:
|