refactor(sap): inject http client into deployment_url to drop internal patch

Replace the test's patch of litellm.module_level_client with dependency
injection via a new optional _http_client attribute, clearing the TQ008
test-quality violation. Production still falls back to the module-level
client when none is injected.
This commit is contained in:
ZOU Yi (BD/SWD-WDE1) 2026-09-07 13:58:16 +08:00
parent 70e540ddc8
commit 77890478a1
2 changed files with 7 additions and 7 deletions

View file

@ -18,6 +18,7 @@ if TYPE_CHECKING:
import tiktoken
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
from litellm.llms.custom_httpx.http_handler import HTTPHandler
LiteLLMLoggingObj = _LiteLLMLoggingObj
else:
@ -136,6 +137,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig):
self.token_creator = None
self._base_url = None
self._resource_group = None
self._http_client: HTTPHandler | None = None
def run_env_setup(self, service_key: str | None = None) -> None:
try:
@ -169,9 +171,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig):
@cached_property
def deployment_url(self) -> str:
# Keep a short, tight client lifecycle here to avoid fd leaks
client: Final = litellm.module_level_client
# with httpx.Client(timeout=30) as client:
client: Final = self._http_client if self._http_client is not None else litellm.module_level_client
deployments: Final = client.get(f"{self.base_url}/lm/deployments", headers=self.headers).json()
valid: Final[list[tuple[str, str]]] = []
for dep in deployments.get("resources", []):

View file

@ -10,7 +10,7 @@ Regression tests for:
configured resource group contains no orchestration deployment.
"""
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock
import pytest
@ -128,10 +128,10 @@ def test_deployment_url_raises_404_when_no_orchestration_deployment():
mock_client = MagicMock()
mock_client.get.return_value.json.return_value = {"resources": []}
config._http_client = mock_client
with patch("litellm.module_level_client", mock_client):
with pytest.raises(GenAIHubOrchestrationError) as exc_info:
_ = config.deployment_url
with pytest.raises(GenAIHubOrchestrationError) as exc_info:
_ = config.deployment_url
assert exc_info.value.status_code == 404
assert "fake-group" in exc_info.value.message