import os
import httpx
from fastapi import FastAPI, Request, Response, Depends, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
import jwt
app = FastAPI()
security = HTTPBearer()
SARVAM_API_KEY = os.environ["SARVAM_API_KEY"]
JWT_SECRET = os.environ["JWT_SECRET"]
SARVAM_BASE = "https://apps.sarvam.ai/api/app-runtime/"
def verify_jwt(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = jwt.decode(credentials.credentials, JWT_SECRET, algorithms=["HS256"])
return payload
except jwt.InvalidTokenError:
raise HTTPException(status_code=401, detail="Invalid token")
@app.api_route("/api/sarvam/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy(path: str, request: Request, user: dict = Depends(verify_jwt)):
async with httpx.AsyncClient() as client:
resp = await client.request(
method=request.method,
url=f"{SARVAM_BASE}{path}",
headers={"Authorization": f"Bearer {SARVAM_API_KEY}"},
content=await request.body(),
)
return Response(content=resp.content, status_code=resp.status_code, headers=dict(resp.headers))