Spaces:
Sleeping
Sleeping
File size: 1,394 Bytes
d8e8718 4952bf4 d8e8718 4952bf4 d8e8718 4952bf4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | from fastapi import FastAPI, Request, Response
import httpx
app = FastAPI()
GOOGLE_API_URL = "https://generativelanguage.googleapis.com"
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy(path: str, request: Request):
async with httpx.AsyncClient() as client:
url = f"{GOOGLE_API_URL}/{path}"
if request.query_params:
url += f"?{request.query_params}"
body = await request.body()
# Фильтруем заголовки, оставляем только нужные для API
allowed_headers = ['content-type', 'x-goog-api-key', 'authorization', 'x-goog-api-client']
headers = {k: v for k, v in request.headers.items() if k.lower() in allowed_headers}
try:
resp = await client.request(
method=request.method,
url=url,
content=body,
headers=headers,
timeout=60.0
)
return Response(
content=resp.content,
status_code=resp.status_code,
headers={"Content-Type": resp.headers.get("Content-Type", "application/json")}
)
except Exception as e:
return Response(content=str(e), status_code=500)
@app.get("/")
async def health():
return {"status": "proxy is running"} |