Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 4 |
|
| 5 |
GAS_WEBAPP_URL = "https://script.google.com/macros/s/AKfycbxRbCcC7ak0xd2z55HVM61LrsY-lH0qCKDAiX4h0EryEuBHrB2mVO6DpZFFMDe3VNRhyA/exec"
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# ✅ CORS مفتوح للديمو
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
@@ -17,27 +18,58 @@ app.add_middleware(
|
|
| 17 |
|
| 18 |
@app.post("/complaints")
|
| 19 |
async def complaints_proxy(req: Request):
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# === اطبع في الـ logs ===
|
| 23 |
print("========== INCOMING PAYLOAD ==========")
|
| 24 |
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
| 25 |
print("======================================")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
|
|
|
| 36 |
try:
|
| 37 |
-
|
|
|
|
| 38 |
except Exception:
|
| 39 |
-
return
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
|
| 7 |
GAS_WEBAPP_URL = "https://script.google.com/macros/s/AKfycbxRbCcC7ak0xd2z55HVM61LrsY-lH0qCKDAiX4h0EryEuBHrB2mVO6DpZFFMDe3VNRhyA/exec"
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
|
|
| 18 |
|
| 19 |
@app.post("/complaints")
|
| 20 |
async def complaints_proxy(req: Request):
|
| 21 |
+
# 1) اقرأ اللي جاي من الصفحة
|
| 22 |
+
try:
|
| 23 |
+
payload = await req.json()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print("❌ Failed to parse incoming JSON:", str(e))
|
| 26 |
+
return JSONResponse(
|
| 27 |
+
status_code=400,
|
| 28 |
+
content={"ok": False, "error": "invalid json body"}
|
| 29 |
+
)
|
| 30 |
|
|
|
|
| 31 |
print("========== INCOMING PAYLOAD ==========")
|
| 32 |
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
| 33 |
print("======================================")
|
| 34 |
|
| 35 |
+
# 2) ابعت لـ GAS (server-to-server)
|
| 36 |
+
try:
|
| 37 |
+
gas_resp = requests.post(
|
| 38 |
+
GAS_WEBAPP_URL,
|
| 39 |
+
json=payload,
|
| 40 |
+
headers={"Content-Type": "application/json"},
|
| 41 |
+
timeout=60
|
| 42 |
+
)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
# ✅ أهم جزء: ده كان غالبًا سبب الـ 500 عندك
|
| 45 |
+
print("❌ ERROR calling GAS:", repr(e))
|
| 46 |
+
return JSONResponse(
|
| 47 |
+
status_code=502,
|
| 48 |
+
content={
|
| 49 |
+
"ok": False,
|
| 50 |
+
"error": "failed to reach GAS",
|
| 51 |
+
"details": str(e)
|
| 52 |
+
}
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
print("========== GAS STATUS ==========")
|
| 56 |
+
print(gas_resp.status_code)
|
| 57 |
+
print("========== GAS RAW TEXT (first 1500 chars) ==========")
|
| 58 |
+
print((gas_resp.text or "")[:1500])
|
| 59 |
+
print("=====================================================")
|
| 60 |
|
| 61 |
+
# 3) رجّع نتيجة GAS للصفحة
|
| 62 |
+
# (حتى لو GAS رجّع HTML أو خطأ)
|
| 63 |
try:
|
| 64 |
+
gas_json = gas_resp.json()
|
| 65 |
+
return JSONResponse(status_code=200, content=gas_json)
|
| 66 |
except Exception:
|
| 67 |
+
return JSONResponse(
|
| 68 |
+
status_code=200,
|
| 69 |
+
content={
|
| 70 |
+
"ok": False,
|
| 71 |
+
"error": "GAS returned non-JSON",
|
| 72 |
+
"gas_status": gas_resp.status_code,
|
| 73 |
+
"raw": (gas_resp.text or "")[:1500]
|
| 74 |
+
}
|
| 75 |
+
)
|