Mr-Help commited on
Commit
e874921
ยท
verified ยท
1 Parent(s): b6f476e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -135
main.py CHANGED
@@ -1,142 +1,28 @@
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")
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
  import requests
 
 
 
3
 
4
  app = FastAPI()
5
 
6
+ # Endpoint ุฌุฏูŠุฏ ูŠู‚ูˆู… ุจุนู…ู„ request ู„ู„ู€ token endpoint ุจุชุงุนู†ุง
7
+ @app.get("/test-token")
8
+ def test_token():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  try:
10
+ resp = requests.get("https://crmleads.binrushd.care/odoo/token", timeout=15)
11
+ except requests.RequestException as e:
12
+ raise HTTPException(status_code=502, detail=f"Request failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ if resp.status_code != 200:
15
+ raise HTTPException(
16
+ status_code=resp.status_code,
17
+ detail={"error": "Bad response", "body": resp.text}
18
+ )
19
 
20
+ try:
21
+ data = resp.json()
22
+ except Exception:
23
+ raise HTTPException(
24
+ status_code=500,
25
+ detail={"error": "Invalid JSON Response", "body": resp.text}
26
+ )
27
+
28
+ return {"received_token": data.get("access_token")}