|
22 | 22 | import argparse |
23 | 23 | import json |
24 | 24 | import os |
25 | | -import ssl |
26 | 25 | import sys |
27 | | -import urllib.error |
28 | | -import urllib.request |
29 | 26 | from datetime import datetime, timezone |
30 | 27 | from pathlib import Path |
31 | 28 |
|
| 29 | +import requests |
32 | 30 | from dotenv import load_dotenv |
33 | 31 |
|
34 | 32 | # --------------------------------------------------------------------------- |
@@ -195,31 +193,21 @@ def fetch_traces_by_session_id(session_id): |
195 | 193 | # --------------------------------------------------------------------------- |
196 | 194 | # Validation API call |
197 | 195 | # --------------------------------------------------------------------------- |
| 196 | + |
| 197 | +# Session reuses TCP connection and SSL handshake across all traces. |
| 198 | +_SESSION = requests.Session() |
| 199 | + |
| 200 | + |
198 | 201 | def validate_trace(trace_id): |
199 | 202 | """Call the validation API for a single trace. Returns (response_dict, http_status).""" |
200 | | - payload = json.dumps({"trace_id": trace_id}).encode("utf-8") |
201 | | - req = urllib.request.Request( |
202 | | - API_URL, |
203 | | - data=payload, |
204 | | - headers={ |
205 | | - "Authorization": f"Bearer {API_TOKEN}", |
206 | | - "Content-Type": "application/json", |
207 | | - }, |
208 | | - method="POST", |
209 | | - ) |
210 | | - ctx = ssl.create_default_context() |
211 | | - |
212 | 203 | try: |
213 | | - with urllib.request.urlopen(req, timeout=300, context=ctx) as resp: |
214 | | - body = json.loads(resp.read().decode("utf-8")) |
215 | | - return body, resp.status |
216 | | - except urllib.error.HTTPError as e: |
217 | | - error_body = e.read().decode("utf-8", errors="replace") |
218 | | - try: |
219 | | - error_json = json.loads(error_body) |
220 | | - except json.JSONDecodeError: |
221 | | - error_json = {"detail": error_body} |
222 | | - return error_json, e.code |
| 204 | + resp = _SESSION.post( |
| 205 | + API_URL, |
| 206 | + json={"trace_id": trace_id}, |
| 207 | + headers={"Authorization": f"Bearer {API_TOKEN}"}, |
| 208 | + timeout=300, |
| 209 | + ) |
| 210 | + return resp.json(), resp.status_code |
223 | 211 | except Exception as e: |
224 | 212 | return {"detail": str(e)}, 0 |
225 | 213 |
|
|
0 commit comments