kbzhishiku
This commit is contained in:
1 parent
7f542f54a5
commit
08a19ebe96
68 files changed
+16602
-229
No files matched your search
@@ -5,7 +5,7 @@ from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import requests
|
||||
from flask import current_app
|
||||
from flask import current_app, has_app_context, has_request_context, request
|
||||
|
||||
from app.config import Config
|
||||
|
||||
@@ -13,6 +13,32 @@ from app.config import Config
|
||||
class IntegrationApiService:
|
||||
_cache: Dict[Tuple[str, str], Dict[str, Any]] = {}
|
||||
|
||||
@staticmethod
|
||||
def get_api_key() -> str:
|
||||
if has_app_context():
|
||||
return str(current_app.config.get("INTEGRATION_API_KEY") or "").strip()
|
||||
return str(Config.INTEGRATION_API_KEY or "").strip()
|
||||
|
||||
@staticmethod
|
||||
def get_base_url() -> str:
|
||||
mode = "local"
|
||||
if has_app_context():
|
||||
mode = str(current_app.config.get("INTEGRATION_API_MODE") or "local").strip().lower()
|
||||
configured = str(current_app.config.get("INTEGRATION_API_BASE") or "").strip().rstrip("/")
|
||||
public_base = str(current_app.config.get("WECOM_PUBLIC_BASE_URL") or "").strip().rstrip("/")
|
||||
else:
|
||||
mode = str(getattr(Config, "INTEGRATION_API_MODE", "local") or "local").strip().lower()
|
||||
configured = str(Config.INTEGRATION_API_BASE or "").strip().rstrip("/")
|
||||
public_base = ""
|
||||
|
||||
if mode != "external":
|
||||
if public_base:
|
||||
return public_base
|
||||
if has_request_context():
|
||||
return request.host_url.rstrip("/")
|
||||
|
||||
return configured
|
||||
|
||||
@staticmethod
|
||||
def _cache_key(dataset_id: str, remote_document_id: str) -> Tuple[str, str]:
|
||||
return (str(dataset_id), str(remote_document_id))
|
||||
@@ -60,8 +86,8 @@ class IntegrationApiService:
|
||||
if cached and IntegrationApiService._is_cache_valid(cached):
|
||||
return cached
|
||||
|
||||
base_url = (Config.INTEGRATION_API_BASE or "").rstrip("/")
|
||||
api_key = Config.INTEGRATION_API_KEY
|
||||
base_url = IntegrationApiService.get_base_url()
|
||||
api_key = IntegrationApiService.get_api_key()
|
||||
if not base_url or not api_key:
|
||||
return None
|
||||
|
||||
@@ -82,7 +108,7 @@ class IntegrationApiService:
|
||||
IntegrationApiService._cache[cache_key] = payload
|
||||
return payload
|
||||
except Exception as e:
|
||||
if current_app:
|
||||
if has_app_context():
|
||||
current_app.logger.warning(f"Integration API lookup failed: {e}")
|
||||
return None
|
||||
|
||||
@@ -121,8 +147,8 @@ class IntegrationApiService:
|
||||
if not unresolved:
|
||||
return results
|
||||
|
||||
base_url = (Config.INTEGRATION_API_BASE or "").rstrip("/")
|
||||
api_key = Config.INTEGRATION_API_KEY
|
||||
base_url = IntegrationApiService.get_base_url()
|
||||
api_key = IntegrationApiService.get_api_key()
|
||||
if not base_url or not api_key:
|
||||
return results
|
||||
|
||||
@@ -136,7 +162,7 @@ class IntegrationApiService:
|
||||
try:
|
||||
resp = requests.post(url, headers=headers, json=body, timeout=(5, 12))
|
||||
if resp.status_code != 200:
|
||||
if current_app:
|
||||
if has_app_context():
|
||||
current_app.logger.warning(
|
||||
"Integration API batch lookup failed: status=%s", resp.status_code
|
||||
)
|
||||
@@ -187,7 +213,7 @@ class IntegrationApiService:
|
||||
|
||||
return results
|
||||
except Exception as e:
|
||||
if current_app:
|
||||
if has_app_context():
|
||||
current_app.logger.warning(f"Integration API batch lookup exception: {e}")
|
||||
# Fallback to single lookup when batch call errors.
|
||||
for item in unresolved:
|
||||
|
||||
Reference in new issue
Block a user