import logging import httpx from app.config.settings import NOTE_SERVICE_URL class NoteServiceClient: async def create_audio_note(self, payload: dict): """Call the Note Service to create an audio note. This method catches HTTP errors and logs them instead of raising, to avoid making transcription endpoints return 500 when the Note Service is unavailable or returns 4xx/5xx. Returns parsed JSON on success or None on failure. """ try: async with httpx.AsyncClient(timeout=30) as client: r = await client.post( f"{NOTE_SERVICE_URL}/internal/notes/audio", json=payload, ) r.raise_for_status() return r.json() except httpx.HTTPStatusError as exc: status = getattr(exc.response, "status_code", "?") logging.warning( "NoteService returned HTTP %s for %s: %s", status, f"{NOTE_SERVICE_URL}/internal/notes/audio", exc, ) return None except Exception as exc: # network errors, timeouts, etc. logging.exception("Failed to call NoteService: %s", exc) return None async def save_transcript(self, payload: dict): # alias used elsewhere in the codebase return await self.create_audio_note(payload) async def update_note(self, note_id: str, payload: dict): async with httpx.AsyncClient(timeout=30) as client: r = await client.patch( f"{NOTE_SERVICE_URL}/notes/{note_id}", json=payload, ) r.raise_for_status() return r.json() async def update_note_internal(self, note_id: str, payload: dict): async with httpx.AsyncClient(timeout=30) as client: r = await client.patch( f"{NOTE_SERVICE_URL}/internal/notes/{note_id}", json=payload, ) r.raise_for_status() return r.json()