67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
|
|
"""
|
||
|
|
Data 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("/upload")
|
||
|
|
async def upload_sales_data(request: Request):
|
||
|
|
"""Proxy data upload to data 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.DATA_SERVICE_URL}/upload",
|
||
|
|
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"Data service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Data service unavailable"
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.get("/sales")
|
||
|
|
async def get_sales_data(request: Request):
|
||
|
|
"""Get sales data"""
|
||
|
|
try:
|
||
|
|
auth_header = request.headers.get("Authorization")
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||
|
|
response = await client.get(
|
||
|
|
f"{settings.DATA_SERVICE_URL}/sales",
|
||
|
|
headers={"Authorization": auth_header}
|
||
|
|
)
|
||
|
|
|
||
|
|
return JSONResponse(
|
||
|
|
status_code=response.status_code,
|
||
|
|
content=response.json()
|
||
|
|
)
|
||
|
|
|
||
|
|
except httpx.RequestError as e:
|
||
|
|
logger.error(f"Data service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Data service unavailable"
|
||
|
|
)
|