mirror of
https://github.com/QuantEngines/fx_quant_engine.git
synced 2026-07-27 18:37:47 +00:00
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
import requests
|
|
|
|
|
|
@dataclass
|
|
class RetryConfig:
|
|
timeout_sec: float = 5.0
|
|
max_attempts: int = 3
|
|
backoff_sec: float = 0.5
|
|
|
|
|
|
class HttpClient:
|
|
def __init__(self, config: RetryConfig) -> None:
|
|
self.config = config
|
|
|
|
def get_json(
|
|
self,
|
|
url: str,
|
|
params: dict[str, Any] | None = None,
|
|
headers: dict[str, str] | None = None,
|
|
) -> dict[str, Any]:
|
|
last_err = "unknown"
|
|
for attempt in range(1, self.config.max_attempts + 1):
|
|
try:
|
|
response = requests.get(url, params=params, headers=headers, timeout=self.config.timeout_sec)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
if not isinstance(data, dict):
|
|
raise RuntimeError("Expected JSON object response")
|
|
return data
|
|
except Exception as exc: # noqa: BLE001
|
|
last_err = str(exc)
|
|
if attempt < self.config.max_attempts:
|
|
time.sleep(self.config.backoff_sec * attempt)
|
|
|
|
raise RuntimeError(
|
|
f"HTTP GET failed after {self.config.max_attempts} attempts for {url}: {last_err}"
|
|
)
|