Files
bakery-ia/gateway/app/routes/forecasting.py
Urtzi Alfaro caf7dea73a Fix docker
2025-07-17 19:46:41 +02:00

67 lines
2.0 KiB
Python

"""
Forecasting routes for gateway
"""
from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import JSONResponse
import httpx
import logging
from app.core.config import settings
logger = logging.getLogger(__name__)
router = APIRouter()
@router.post("/predict")
async def create_forecast(request: Request):
"""Proxy forecast request to forecasting service"""
try:
body = await request.body()
auth_header = request.headers.get("Authorization")
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{settings.FORECASTING_SERVICE_URL}/predict",
content=body,
headers={
"Content-Type": "application/json",
"Authorization": auth_header
}
)
return JSONResponse(
status_code=response.status_code,
content=response.json()
)
except httpx.RequestError as e:
logger.error(f"Forecasting service unavailable: {e}")
raise HTTPException(
status_code=503,
detail="Forecasting service unavailable"
)
@router.get("/forecasts")
async def get_forecasts(request: Request):
"""Get forecasts"""
try:
auth_header = request.headers.get("Authorization")
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(
f"{settings.FORECASTING_SERVICE_URL}/forecasts",
headers={"Authorization": auth_header}
)
return JSONResponse(
status_code=response.status_code,
content=response.json()
)
except httpx.RequestError as e:
logger.error(f"Forecasting service unavailable: {e}")
raise HTTPException(
status_code=503,
detail="Forecasting service unavailable"
)