Update main.py
Browse files
main.py
CHANGED
|
@@ -1,99 +1,142 @@
|
|
| 1 |
-
import
|
| 2 |
import requests
|
|
|
|
| 3 |
import sys
|
| 4 |
-
from
|
| 5 |
-
from fastapi.responses import JSONResponse
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 9 |
BASE = "https://odoo.binrushd.care"
|
| 10 |
AUTH_URL = f"{BASE}/api/auth/token"
|
| 11 |
SESSION_AUTH_URL = f"{BASE}/web/session/authenticate"
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"CF-Access-Client-Id": CF_ID,
|
| 35 |
"CF-Access-Client-Secret": CF_SECRET,
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# 2) محاولة التقاط session_id من Set-Cookie، وإن لم يوجد نعمل authenticate
|
| 49 |
-
session_id = s.cookies.get("session_id") or r.cookies.get("session_id")
|
| 50 |
-
if not session_id:
|
| 51 |
-
payload = {
|
| 52 |
-
"jsonrpc": "2.0",
|
| 53 |
-
"params": {"db": DB_NAME, "login": LOGIN, "password": PASSWORD}
|
| 54 |
}
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if not session_id:
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
return JSONResponse(
|
| 92 |
-
status_code=500,
|
| 93 |
-
content={"ok": False, "error": "UnhandledError", "detail": str(e)},
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
@app.get("/ping")
|
| 97 |
-
def ping():
|
| 98 |
-
"""بسيطة لاختبار الاتصال"""
|
| 99 |
-
return {"pong": True}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Response
|
| 2 |
import requests
|
| 3 |
+
import json
|
| 4 |
import sys
|
| 5 |
+
from http.cookiejar import Cookie
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# ========= Config ثابتة للاختبار =========
|
| 10 |
BASE = "https://odoo.binrushd.care"
|
| 11 |
AUTH_URL = f"{BASE}/api/auth/token"
|
| 12 |
SESSION_AUTH_URL = f"{BASE}/web/session/authenticate"
|
| 13 |
|
| 14 |
+
CF_ID = "0491b36d7dcabce5b04f1a53f347bb4e.access"
|
| 15 |
+
CF_SECRET = "22152cb41b62393e159daaff7dce433006c3744c5850e6adc15fa3544bb5eb09"
|
| 16 |
+
LOGIN = "binrushd.automation@gmail.com"
|
| 17 |
+
PASSWORD = "BR2025"
|
| 18 |
+
DB_NAME = "BR_EMR_16.0_202401"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def cookies_to_log_list(cookiejar):
|
| 22 |
+
"""حوّل الكوكيز لقائمة سهلة القراءة في اللوج."""
|
| 23 |
+
out = []
|
| 24 |
+
for c in cookiejar: # type: Cookie
|
| 25 |
+
out.append({
|
| 26 |
+
"name": c.name,
|
| 27 |
+
"value": c.value,
|
| 28 |
+
"domain": c.domain,
|
| 29 |
+
"path": c.path,
|
| 30 |
+
"secure": c.secure,
|
| 31 |
+
"expires": c.expires,
|
| 32 |
+
})
|
| 33 |
+
return out
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.get("/get-token-and-session")
|
| 37 |
+
def get_token_and_session():
|
| 38 |
+
s = requests.Session()
|
| 39 |
+
# مهم: نمنع brotli عشان ما يجيلك نص HTML مضغوط br
|
| 40 |
+
s.headers.update({
|
| 41 |
+
"Accept": "*/*",
|
| 42 |
+
"Content-Type": "application/json",
|
| 43 |
+
"Accept-Encoding": "gzip, deflate", # بدون br
|
| 44 |
+
"Connection": "keep-alive",
|
| 45 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 46 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 47 |
+
"Chrome/120.0.0.0 Safari/537.36",
|
| 48 |
"CF-Access-Client-Id": CF_ID,
|
| 49 |
"CF-Access-Client-Secret": CF_SECRET,
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
# ========== (1) طلب التوكن ==========
|
| 54 |
+
auth_headers = {
|
| 55 |
+
"CF-Access-Client-Id": CF_ID,
|
| 56 |
+
"CF-Access-Client-Secret": CF_SECRET,
|
| 57 |
+
# لو السيرفر بيقرأ دول من الهيدر:
|
| 58 |
+
"login": LOGIN,
|
| 59 |
+
"password": PASSWORD,
|
| 60 |
+
"db": DB_NAME,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
+
|
| 63 |
+
r = s.get(AUTH_URL, headers=auth_headers, timeout=(5, 30), allow_redirects=True)
|
| 64 |
+
|
| 65 |
+
# لوج مفصل للرد
|
| 66 |
+
r_log = {
|
| 67 |
+
"status_code": r.status_code,
|
| 68 |
+
"reason": r.reason,
|
| 69 |
+
"headers": dict(r.headers),
|
| 70 |
+
"set_cookie": r.headers.get("Set-Cookie", ""),
|
| 71 |
+
"cookies_now": cookies_to_log_list(s.cookies),
|
| 72 |
+
"body_preview": r.text[:1000],
|
| 73 |
+
}
|
| 74 |
+
print("[AUTH RESPONSE]", json.dumps(r_log, ensure_ascii=False), file=sys.stderr)
|
| 75 |
+
|
| 76 |
+
if r.status_code >= 400:
|
| 77 |
+
# رجّع الرد كما هو بنفس الحالة والمحتوى
|
| 78 |
+
return Response(content=r.text,
|
| 79 |
+
status_code=r.status_code,
|
| 80 |
+
media_type=r.headers.get("Content-Type", "text/plain"))
|
| 81 |
+
|
| 82 |
+
# استخرج التوكن
|
| 83 |
+
try:
|
| 84 |
+
data = r.json()
|
| 85 |
+
except ValueError:
|
| 86 |
+
data = {}
|
| 87 |
+
token = data.get("access_token") or (data.get("data") or {}).get("access_token")
|
| 88 |
+
if not token:
|
| 89 |
+
msg = {"error": "No access_token in response", "raw": data}
|
| 90 |
+
print("[NO TOKEN]", json.dumps(msg, ensure_ascii=False), file=sys.stderr)
|
| 91 |
+
return Response(content=json.dumps(msg, ensure_ascii=False),
|
| 92 |
+
status_code=502, media_type="application/json")
|
| 93 |
+
|
| 94 |
+
# ========== (2) التقاط/إنشاء session_id ==========
|
| 95 |
+
session_id = s.cookies.get("session_id") or r.cookies.get("session_id")
|
| 96 |
+
|
| 97 |
if not session_id:
|
| 98 |
+
payload = {
|
| 99 |
+
"jsonrpc": "2.0",
|
| 100 |
+
"params": {"db": DB_NAME, "login": LOGIN, "password": PASSWORD}
|
| 101 |
+
}
|
| 102 |
+
r2 = s.post(SESSION_AUTH_URL, json=payload, timeout=(5, 30), allow_redirects=True)
|
| 103 |
|
| 104 |
+
r2_log = {
|
| 105 |
+
"status_code": r2.status_code,
|
| 106 |
+
"reason": r2.reason,
|
| 107 |
+
"headers": dict(r2.headers),
|
| 108 |
+
"set_cookie": r2.headers.get("Set-Cookie", ""),
|
| 109 |
+
"cookies_now": cookies_to_log_list(s.cookies),
|
| 110 |
+
"body_preview": r2.text[:1000],
|
| 111 |
+
}
|
| 112 |
+
print("[SESSION AUTH RESPONSE]", json.dumps(r2_log, ensure_ascii=False), file=sys.stderr)
|
| 113 |
|
| 114 |
+
if r2.status_code >= 400:
|
| 115 |
+
return Response(content=r2.text,
|
| 116 |
+
status_code=r2.status_code,
|
| 117 |
+
media_type=r2.headers.get("Content-Type", "text/plain"))
|
| 118 |
|
| 119 |
+
session_id = s.cookies.get("session_id") or r2.cookies.get("session_id")
|
| 120 |
|
| 121 |
+
if not session_id:
|
| 122 |
+
msg = {"error": "Could not obtain session_id cookie."}
|
| 123 |
+
print("[SESSION MISSING]", json.dumps(msg, ensure_ascii=False), file=sys.stderr)
|
| 124 |
+
return Response(content=json.dumps(msg, ensure_ascii=False),
|
| 125 |
+
status_code=502, media_type="application/json")
|
| 126 |
+
|
| 127 |
+
# ========== (3) النتيجة ==========
|
| 128 |
+
result = {
|
| 129 |
+
"access_token": token,
|
| 130 |
+
"session_id": session_id,
|
| 131 |
+
"cookies": cookies_to_log_list(s.cookies), # للمراجعة/الديبج
|
| 132 |
+
}
|
| 133 |
+
# اطبع الكوكيز بوضوح في اللوج
|
| 134 |
+
print("[FINAL COOKIES]", json.dumps(result["cookies"], ensure_ascii=False), file=sys.stderr)
|
| 135 |
+
|
| 136 |
+
return Response(content=json.dumps(result, ensure_ascii=False),
|
| 137 |
+
status_code=200, media_type="application/json")
|
| 138 |
+
|
| 139 |
+
except requests.RequestException as e:
|
| 140 |
+
err_txt = f"Network error: {type(e).__name__} - {str(e)}"
|
| 141 |
+
print("[NETWORK ERROR]", err_txt, file=sys.stderr)
|
| 142 |
+
return Response(content=err_txt, status_code=502, media_type="text/plain")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|