import logging import requests from settings import APPLY_TIMEOUT, APPLY_URL, INTERNAL_API_KEY logger = logging.getLogger("web.apply_client") def _row_to_profile(profile_row) -> dict: """Convert a user_profiles row into the payload dict for /apply. Apply-side ProfileModel (Pydantic) validates + coerces types; we just hand over whatever the row has. `updated_at` and any other extra keys are ignored by the model.""" if profile_row is None: return {} return dict(profile_row) class ApplyClient: def __init__(self): self.base = APPLY_URL.rstrip("/") self.timeout = APPLY_TIMEOUT self.headers = {"X-Internal-Api-Key": INTERNAL_API_KEY} def health(self) -> bool: try: r = requests.get(f"{self.base}/health", timeout=5) return r.ok except requests.RequestException: return False def apply(self, url: str, profile: dict, submit_forms: bool, application_id: int | None = None) -> dict: body = { "url": url, "profile": profile, "submit_forms": bool(submit_forms), "application_id": application_id, } try: r = requests.post( f"{self.base}/apply", json=body, headers=self.headers, timeout=self.timeout, ) if r.status_code >= 400: return {"success": False, "message": f"apply HTTP {r.status_code}: {r.text[:300]}", "provider": "", "forensics": {}} return r.json() except requests.RequestException as e: return {"success": False, "message": f"apply unreachable: {e}", "provider": "", "forensics": {}}