This commit is contained in:
2026-04-26 01:15:01 +03:30
parent 72588fe12c
commit 2cd96ceec6
11 changed files with 338 additions and 380 deletions
+42 -15
View File
@@ -4,6 +4,7 @@ import requests
from django.conf import settings
from .exceptions import ExternalAPIRequestError
from .exceptions import MockDirectoryNotFound, MockFileNotFound
from .mock_loader import MockLoader
from .services import ServiceRegistry
@@ -25,15 +26,19 @@ class ExternalAPIAdapter:
request_method = method.upper()
self._validate_method(request_method)
service = self.service_registry.get(service_name)
if getattr(settings, "USE_EXTERNAL_API_MOCK", False):
mock_response = self.mock_loader.load(service_name=service_name, path=path, method=request_method)
return AdapterResponse(
status_code=mock_response.status_code,
data=mock_response.data,
headers={"X-Mock-File": mock_response.file_path},
is_mock=True,
)
use_mock = getattr(settings, "USE_EXTERNAL_API_MOCK", False) and service_name != "ai"
if use_mock:
try:
mock_response = self.mock_loader.load(service_name=service_name, path=path, method=request_method)
return AdapterResponse(
status_code=mock_response.status_code,
data=mock_response.data,
headers={"X-Mock-File": mock_response.file_path},
is_mock=True,
)
except (MockDirectoryNotFound, MockFileNotFound):
pass
return self._call_real_api(
service=service,
@@ -47,25 +52,47 @@ class ExternalAPIAdapter:
def _call_real_api(self, service, path, method, payload=None, query=None, headers=None):
base_url = service.get("base_url", "").rstrip("/")
api_key = service.get("api_key", "")
host_header = service.get("host_header", "").strip()
if not base_url:
raise ExternalAPIRequestError("External service base_url is not configured.")
url = f"{base_url}/{str(path).lstrip('/')}"
files = None
request_payload = payload
request_headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
if host_header:
request_headers["Host"] = host_header
if headers:
request_headers.update(headers)
if isinstance(payload, dict) and payload.get("__files__"):
files = payload["__files__"]
request_payload = {
key: value
for key, value in payload.items()
if key != "__files__"
}
request_headers.pop("Content-Type", None)
try:
request_kwargs = {
"method": method,
"url": url,
"params": query,
"headers": request_headers,
"timeout": getattr(settings, "EXTERNAL_API_TIMEOUT", 30),
}
if files:
request_kwargs["data"] = request_payload
request_kwargs["files"] = files
else:
request_kwargs["json"] = request_payload
response = requests.request(
method=method,
url=url,
json=payload,
params=query,
headers=request_headers,
timeout=getattr(settings, "EXTERNAL_API_TIMEOUT", 30),
**request_kwargs,
)
except requests.RequestException as exc:
raise ExternalAPIRequestError(f"External API request failed for '{url}': {exc}") from exc