📄 rtsport_api_proxy.py 1,545 bytes Yesterday 01:59 📋 Raw

"""Proxy RTSport API requests from main server to local API.

Note: Coach Dashboard API endpoints are NOT included here to avoid
routing conflicts with the catch-all /{path:path} proxy route.
They are mounted separately in main_v2.py from rtsport_coach_api.
"""

import httpx
from fastapi import APIRouter, Request, Response
from fastapi.responses import JSONResponse

router = APIRouter()

RTS_API_BASE = "http://127.0.0.1:8001"

async def proxy_request(request: Request, path: str):
"""Forward request to RTSport API."""
async with httpx.AsyncClient() as client:
url = f"{RTS_API_BASE}/api/v1/{path}"

    # Forward headers
    headers = dict(request.headers)
    headers.pop('host', None)

    # Get body if present
    body = await request.body()

    # Forward request
    method = request.method
    resp = await client.request(
        method=method,
        url=url,
        headers=headers,
        content=body,
        params=request.query_params,
        timeout=30.0
    )

    return Response(
        content=resp.content,
        status_code=resp.status_code,
        headers=dict(resp.headers)
    )

Paths handled locally by coach_api_router and parent_api_router — do NOT proxy

LOCAL_PATHS: set[str] = set()

@router.api_route("/{path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
async def rtsport_proxy(request: Request, path: str):
return await proxy_request(request, path)