Spaces:
Paused
Paused
Update api_executor.py
Browse files- api_executor.py +16 -16
api_executor.py
CHANGED
|
@@ -5,7 +5,7 @@ Flare β API Executor (v2.0 Β· session-aware token management)
|
|
| 5 |
from __future__ import annotations
|
| 6 |
import json, re, time, requests
|
| 7 |
from typing import Any, Dict, Optional, Union
|
| 8 |
-
from
|
| 9 |
from config_provider import ConfigProvider, APIConfig
|
| 10 |
from session import Session
|
| 11 |
|
|
@@ -107,7 +107,7 @@ def _fetch_token(api: APIConfig, session: Session) -> None:
|
|
| 107 |
if not api.auth or not api.auth.enabled:
|
| 108 |
return
|
| 109 |
|
| 110 |
-
|
| 111 |
|
| 112 |
try:
|
| 113 |
# Use _render_json for body to preserve types
|
|
@@ -138,10 +138,10 @@ def _fetch_token(api: APIConfig, session: Session) -> None:
|
|
| 138 |
"refresh_token": json_data.get("refresh_token")
|
| 139 |
}
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
-
|
| 145 |
raise
|
| 146 |
|
| 147 |
def _refresh_token(api: APIConfig, session: Session) -> bool:
|
|
@@ -153,7 +153,7 @@ def _refresh_token(api: APIConfig, session: Session) -> bool:
|
|
| 153 |
if not token_info.get("refresh_token"):
|
| 154 |
return False
|
| 155 |
|
| 156 |
-
|
| 157 |
|
| 158 |
try:
|
| 159 |
body = _render_json(api.auth.token_refresh_body or {}, session, api.name)
|
|
@@ -182,11 +182,11 @@ def _refresh_token(api: APIConfig, session: Session) -> bool:
|
|
| 182 |
"refresh_token": json_data.get("refresh_token", token_info["refresh_token"])
|
| 183 |
}
|
| 184 |
|
| 185 |
-
|
| 186 |
return True
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
-
|
| 190 |
return False
|
| 191 |
|
| 192 |
def _ensure_token(api: APIConfig, session: Session) -> None:
|
|
@@ -254,21 +254,21 @@ def call_api(api: APIConfig, session: Session) -> requests.Response:
|
|
| 254 |
|
| 255 |
for attempt in range(retry_count + 1):
|
| 256 |
try:
|
| 257 |
-
|
| 258 |
-
|
| 259 |
|
| 260 |
response = requests.request(**request_params)
|
| 261 |
|
| 262 |
# Handle 401 Unauthorized
|
| 263 |
if response.status_code == 401 and api.auth and api.auth.enabled and attempt < retry_count:
|
| 264 |
-
|
| 265 |
_fetch_token(api, session) # Force new token
|
| 266 |
headers = _render(api.headers, session, api.name) # Re-render headers
|
| 267 |
request_params["headers"] = headers
|
| 268 |
continue
|
| 269 |
|
| 270 |
response.raise_for_status()
|
| 271 |
-
|
| 272 |
|
| 273 |
# Response mapping iΕlemi
|
| 274 |
if response.status_code in (200, 201, 202, 204) and hasattr(api, 'response_mappings') and api.response_mappings:
|
|
@@ -308,27 +308,27 @@ def call_api(api: APIConfig, session: Session) -> requests.Response:
|
|
| 308 |
|
| 309 |
# Session'a kaydet
|
| 310 |
session.variables[var_name] = value
|
| 311 |
-
|
| 312 |
|
| 313 |
except Exception as e:
|
| 314 |
-
|
| 315 |
|
| 316 |
return response
|
| 317 |
|
| 318 |
except requests.exceptions.Timeout as e:
|
| 319 |
last_error = e
|
| 320 |
-
|
| 321 |
|
| 322 |
except requests.exceptions.RequestException as e:
|
| 323 |
last_error = e
|
| 324 |
-
|
| 325 |
|
| 326 |
# Retry backoff
|
| 327 |
if attempt < retry_count:
|
| 328 |
backoff = api.retry.backoff_seconds if api.retry else 2
|
| 329 |
if api.retry and api.retry.strategy == "exponential":
|
| 330 |
backoff = backoff * (2 ** attempt)
|
| 331 |
-
|
| 332 |
time.sleep(backoff)
|
| 333 |
|
| 334 |
# All retries failed
|
|
|
|
| 5 |
from __future__ import annotations
|
| 6 |
import json, re, time, requests
|
| 7 |
from typing import Any, Dict, Optional, Union
|
| 8 |
+
from logger import log_info, log_error, log_warning, log_debug
|
| 9 |
from config_provider import ConfigProvider, APIConfig
|
| 10 |
from session import Session
|
| 11 |
|
|
|
|
| 107 |
if not api.auth or not api.auth.enabled:
|
| 108 |
return
|
| 109 |
|
| 110 |
+
log_info(f"π Fetching token for {api.name}")
|
| 111 |
|
| 112 |
try:
|
| 113 |
# Use _render_json for body to preserve types
|
|
|
|
| 138 |
"refresh_token": json_data.get("refresh_token")
|
| 139 |
}
|
| 140 |
|
| 141 |
+
log_info(f"β
Token obtained for {api.name}")
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
+
log_error(f"β Token fetch failed for {api.name}", e)
|
| 145 |
raise
|
| 146 |
|
| 147 |
def _refresh_token(api: APIConfig, session: Session) -> bool:
|
|
|
|
| 153 |
if not token_info.get("refresh_token"):
|
| 154 |
return False
|
| 155 |
|
| 156 |
+
log_info(f"π Refreshing token for {api.name}")
|
| 157 |
|
| 158 |
try:
|
| 159 |
body = _render_json(api.auth.token_refresh_body or {}, session, api.name)
|
|
|
|
| 182 |
"refresh_token": json_data.get("refresh_token", token_info["refresh_token"])
|
| 183 |
}
|
| 184 |
|
| 185 |
+
log_info(f"β
Token refreshed for {api.name}")
|
| 186 |
return True
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
+
log_error(f"β Token refresh failed for {api.name}", e)
|
| 190 |
return False
|
| 191 |
|
| 192 |
def _ensure_token(api: APIConfig, session: Session) -> None:
|
|
|
|
| 254 |
|
| 255 |
for attempt in range(retry_count + 1):
|
| 256 |
try:
|
| 257 |
+
log_info(f"π API call: {api.name} {api.method} {api.url} (attempt {attempt + 1}/{retry_count + 1})")
|
| 258 |
+
log_info(f"π Request body: {json.dumps(body, ensure_ascii=False)}")
|
| 259 |
|
| 260 |
response = requests.request(**request_params)
|
| 261 |
|
| 262 |
# Handle 401 Unauthorized
|
| 263 |
if response.status_code == 401 and api.auth and api.auth.enabled and attempt < retry_count:
|
| 264 |
+
log_info(f"π Got 401, refreshing token for {api.name}")
|
| 265 |
_fetch_token(api, session) # Force new token
|
| 266 |
headers = _render(api.headers, session, api.name) # Re-render headers
|
| 267 |
request_params["headers"] = headers
|
| 268 |
continue
|
| 269 |
|
| 270 |
response.raise_for_status()
|
| 271 |
+
log_info(f"β
API call successful: {api.name} ({response.status_code})")
|
| 272 |
|
| 273 |
# Response mapping iΕlemi
|
| 274 |
if response.status_code in (200, 201, 202, 204) and hasattr(api, 'response_mappings') and api.response_mappings:
|
|
|
|
| 308 |
|
| 309 |
# Session'a kaydet
|
| 310 |
session.variables[var_name] = value
|
| 311 |
+
log_info(f"π Mapped response value: {var_name} = {value}")
|
| 312 |
|
| 313 |
except Exception as e:
|
| 314 |
+
log_error("β οΈ Response mapping error", e)
|
| 315 |
|
| 316 |
return response
|
| 317 |
|
| 318 |
except requests.exceptions.Timeout as e:
|
| 319 |
last_error = e
|
| 320 |
+
log_warning(f"β±οΈ API timeout for {api.name} (attempt {attempt + 1})")
|
| 321 |
|
| 322 |
except requests.exceptions.RequestException as e:
|
| 323 |
last_error = e
|
| 324 |
+
log_error(f"β API error for {api.name}", e)
|
| 325 |
|
| 326 |
# Retry backoff
|
| 327 |
if attempt < retry_count:
|
| 328 |
backoff = api.retry.backoff_seconds if api.retry else 2
|
| 329 |
if api.retry and api.retry.strategy == "exponential":
|
| 330 |
backoff = backoff * (2 ** attempt)
|
| 331 |
+
log_info(f"β³ Waiting {backoff}s before retry...")
|
| 332 |
time.sleep(backoff)
|
| 333 |
|
| 334 |
# All retries failed
|