mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-07-12 13:29:04 +00:00
Route the Google Solar client through the shared call_with_retry with full jitter (de-synchronises the 32 concurrent containers per Google's "avoid synchronised requests" guidance), honouring Retry-After, a 60s max backoff (rides out the 600 QPM per-minute window), and 6 bounded retries. 429/5xx/transport errors are transient; other 4xx propagate immediately; 404-entity-not-found stays BuildingInsightsNotFoundError. On exhaustion a TransientHttpError surfaces so the subtask fails and is re-triggered (no silent degrade). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
85 lines
3.3 KiB
Python
85 lines
3.3 KiB
Python
from typing import Any, Literal, Optional
|
|
|
|
import requests
|
|
|
|
from infrastructure.http_retry import TransientHttpError, call_with_retry
|
|
|
|
|
|
class BuildingInsightsNotFoundError(Exception):
|
|
pass
|
|
|
|
|
|
class GoogleSolarApiClient:
|
|
base_url: str = "https://solar.googleapis.com/v1"
|
|
# Bounded retries (NOT Google's infinite-loop example). With the 60s max
|
|
# backoff below, 6 retries span ~2 of the Solar API's per-minute (600 QPM)
|
|
# windows — enough to ride out a transient 429 burst, then give up so a
|
|
# stuck request can't blow the container timeout; the subtask fails and is
|
|
# re-triggered.
|
|
MAX_RETRIES: int = 6
|
|
MAX_BACKOFF_SECONDS: float = 60.0
|
|
ENTITY_NOT_FOUND_ERROR: str = "Requested entity was not found."
|
|
|
|
def __init__(self, api_key: str) -> None:
|
|
self._api_key = api_key
|
|
|
|
@staticmethod
|
|
def _parse_retry_after(response: requests.Response) -> Optional[float]:
|
|
header = response.headers.get("Retry-After")
|
|
if header is None:
|
|
return None
|
|
try:
|
|
return float(header)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
def get_building_insights(
|
|
self,
|
|
longitude: float,
|
|
latitude: float,
|
|
required_quality: Literal["HIGH", "MEDIUM", "LOW"] = "MEDIUM",
|
|
) -> dict[str, Any]:
|
|
insights_url = f"{self.base_url}/buildingInsights:findClosest"
|
|
params: dict[str, str] = {
|
|
"location.latitude": f"{latitude:.5f}",
|
|
"location.longitude": f"{longitude:.5f}",
|
|
"requiredQuality": required_quality,
|
|
"key": self._api_key,
|
|
}
|
|
|
|
def _fetch() -> dict[str, Any]:
|
|
try:
|
|
response = requests.get(insights_url, params=params)
|
|
response.raise_for_status()
|
|
result: dict[str, Any] = response.json()
|
|
return result
|
|
except requests.exceptions.RequestException as e:
|
|
response = e.response
|
|
if response is None:
|
|
# No response = a transport-level failure (connection reset /
|
|
# read or connect timeout) — transient, retry.
|
|
raise TransientHttpError(f"Solar API transport error: {e}") from e
|
|
status = response.status_code
|
|
if (
|
|
status == 404
|
|
and response.json().get("error", {}).get("message")
|
|
== self.ENTITY_NOT_FOUND_ERROR
|
|
):
|
|
raise BuildingInsightsNotFoundError() from e
|
|
if status == 429 or status >= 500:
|
|
# Rate-limit (429) and server errors (5xx) are transient.
|
|
# Honour the server's Retry-After when present (jittered
|
|
# exponential backoff otherwise — see `call_with_retry`).
|
|
raise TransientHttpError(
|
|
f"Solar API {status}",
|
|
retry_after=self._parse_retry_after(response),
|
|
) from e
|
|
# Other 4xx are the caller's fault — non-transient, propagate.
|
|
raise
|
|
|
|
return call_with_retry(
|
|
_fetch,
|
|
max_retries=self.MAX_RETRIES,
|
|
max_backoff=self.MAX_BACKOFF_SECONDS,
|
|
jitter=True,
|
|
)
|