Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/client/http_client.py: 0%

18 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 12:01 +0000

1"""HTTP client for making requests to the LiteLLM proxy server.""" 

2 

3from collections.abc import Mapping 

4from typing import Any, Final 

5 

6import requests 

7 

8 

9class HTTPClient: 

10 """HTTP client for making requests to the LiteLLM proxy server.""" 

11 

12 def __init__(self, base_url: str, api_key: str | None = None, timeout: int = 30): 

13 """Initialize the HTTP client. 

14 

15 Args: 

16 base_url: Base URL of the LiteLLM proxy server 

17 api_key: Optional API key for authentication 

18 timeout: Request timeout in seconds (default: 30) 

19 """ 

20 self._base_url = base_url.rstrip("/") 

21 self._api_key = api_key 

22 self._timeout = timeout 

23 

24 def request( 

25 self, 

26 method: str, 

27 uri: str, 

28 *, 

29 data: Mapping[str, object] | list | bytes | None = None, 

30 json: Mapping[str, object] | list | None = None, 

31 headers: dict[str, str] | None = None, 

32 **kwargs: Any, 

33 ) -> Any: 

34 """Make an HTTP request to the LiteLLM proxy server. 

35 

36 This method is used to make generic requests to the LiteLLM proxy 

37 server, when there is not a specific client or method for the request. 

38 

39 Args: 

40 method: HTTP method (GET, POST, PUT, DELETE, etc.) 

41 uri: URI path (will be appended to base_url) (e.g., "/credentials") 

42 data: (optional) Dictionary, list of tuples, bytes, or file-like 

43 object to send in the body of the request. 

44 json: (optional) A JSON serializable Python object to send in the body 

45 of the request. 

46 headers: (optional) Dictionary of HTTP headers to send with the request. 

47 **kwargs: Additional keyword arguments to pass to the request. 

48 

49 Returns: 

50 Parsed JSON response from the server 

51 

52 Raises: 

53 requests.exceptions.RequestException: If the request fails 

54 ValueError: If the response is not valid JSON 

55 

56 Example: 

57 >>> client.http.request("POST", "/health/test_connection", json={ 

58 "litellm_params": { 

59 "model": "gpt-4", 

60 "custom_llm_provider": "azure_ai", 

61 "litellm_credential_name": None, 

62 "api_key": "6xxxxxxx", 

63 "api_base": "https://litellm8397336933.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-10-21", 

64 }, 

65 "mode": "chat", 

66 }) 

67 {'status': 'error', 

68 'result': {'model': 'gpt-4', 

69 'custom_llm_provider': 'azure_ai', 

70 'litellm_credential_name': None, 

71 ... 

72 """ 

73 # Build complete URL 

74 url: Final = f"{self._base_url}/{uri.lstrip('/')}" 

75 

76 # Prepare headers 

77 request_headers: Final = {} 

78 if headers: 

79 request_headers.update(headers) 

80 if self._api_key: 

81 request_headers["Authorization"] = f"Bearer {self._api_key}" 

82 

83 response: Final = requests.request( 

84 method=method, 

85 url=url, 

86 data=data, 

87 json=json, 

88 headers=request_headers, 

89 timeout=self._timeout, 

90 **kwargs, 

91 ) 

92 

93 # Raise for HTTP errors 

94 response.raise_for_status() 

95 

96 # Parse and return JSON response 

97 return response.json()