feat: add CORS support, public JS SDK, and live Render vitals forwarding
This commit is contained in:
@@ -8,31 +8,56 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
async def forward_vitals_to_api(vitals: NormalizedVitals) -> bool:
|
||||
"""
|
||||
Attempts to POST the normalized vitals to the configured target API.
|
||||
Returns True if successful, False otherwise.
|
||||
Attempts to POST the normalized vitals to the configured target API
|
||||
and to the Render cloud database.
|
||||
Returns True if at least one forward succeeds.
|
||||
"""
|
||||
url = settings.target_api_url
|
||||
import os
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
if settings.api_token:
|
||||
headers["Authorization"] = f"Bearer {settings.api_token}"
|
||||
# 1. Forward to primary target API (e.g. Injomo Dev Workflow)
|
||||
primary_url = settings.target_api_url
|
||||
primary_success = False
|
||||
if primary_url:
|
||||
try:
|
||||
primary_headers = headers.copy()
|
||||
if settings.api_token:
|
||||
primary_headers["Authorization"] = f"Bearer {settings.api_token}"
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(
|
||||
primary_url,
|
||||
json=vitals.model_dump(mode="json"),
|
||||
headers=primary_headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
logger.info(f"Successfully forwarded vitals to primary API for device {vitals.device_id}")
|
||||
primary_success = True
|
||||
except Exception as e:
|
||||
logger.error(f"Error forwarding to primary API: {e}")
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(
|
||||
url,
|
||||
json=vitals.model_dump(mode="json"),
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
logger.info(f"Successfully forwarded vitals for device {vitals.device_id}")
|
||||
return True
|
||||
# 2. Forward to Render Cloud Server
|
||||
render_url = "https://contectfiveparamonitor.onrender.com/api/push-vitals"
|
||||
render_success = False
|
||||
|
||||
# Detect if we are already running on Render to prevent an infinite loop
|
||||
is_on_render = os.getenv("RENDER") is not None or os.getenv("PORT") == "10000" or "render.local" in os.getenv("HOSTNAME", "")
|
||||
|
||||
if not is_on_render:
|
||||
try:
|
||||
url_with_key = render_url
|
||||
if settings.api_token:
|
||||
url_with_key = f"{render_url}?api_key={settings.api_token}"
|
||||
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(
|
||||
url_with_key,
|
||||
json=vitals.model_dump(mode="json"),
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
logger.info(f"Successfully forwarded vitals to Render Cloud for device {vitals.device_id}")
|
||||
render_success = True
|
||||
except Exception as e:
|
||||
logger.error(f"Error forwarding to Render Cloud: {e}")
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
logger.error(f"API returned HTTP {e.response.status_code}: {e.response.text}")
|
||||
except httpx.RequestError as e:
|
||||
logger.error(f"Request to API failed: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error forwarding to API: {e}")
|
||||
|
||||
return False
|
||||
return primary_success or render_success
|
||||
|
||||
Reference in New Issue
Block a user