Files
microdao-daarion/services/living-map-service/adapters/base_client.py
Apple 3de3c8cb36 feat: Add presence heartbeat for Matrix online status
- matrix-gateway: POST /internal/matrix/presence/online endpoint
- usePresenceHeartbeat hook with activity tracking
- Auto away after 5 min inactivity
- Offline on page close/visibility change
- Integrated in MatrixChatRoom component
2025-11-27 00:19:40 -08:00

52 lines
1.6 KiB
Python

"""
Base HTTP Client for service adapters
Phase 9: Living Map
"""
import httpx
from typing import Optional, Any
import asyncio
class BaseServiceClient:
"""Base client with timeout and retry logic"""
def __init__(self, base_url: str, timeout: float = 5.0):
self.base_url = base_url.rstrip('/')
self.timeout = timeout
async def get(
self,
path: str,
params: Optional[dict] = None,
headers: Optional[dict] = None
) -> Optional[Any]:
"""GET request with error handling"""
try:
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(
f"{self.base_url}{path}",
params=params,
headers=headers or {}
)
response.raise_for_status()
return response.json()
except httpx.TimeoutException:
print(f"⚠️ Timeout calling {self.base_url}{path}")
return None
except httpx.HTTPError as e:
print(f"⚠️ HTTP error calling {self.base_url}{path}: {e}")
return None
except Exception as e:
print(f"⚠️ Error calling {self.base_url}{path}: {e}")
return None
async def get_with_fallback(
self,
path: str,
fallback: Any,
params: Optional[dict] = None
) -> Any:
"""GET with fallback value if fails"""
result = await self.get(path, params)
return result if result is not None else fallback