1+ import json
2+ import logging
3+ from typing import Dict , Any , Optional
4+
5+ logger = logging .getLogger (__name__ )
6+
7+ def extract_connection_pool (thrift_http_client ):
8+ """
9+ Extract the connection pool from a Thrift HTTP client.
10+
11+ Args:
12+ thrift_http_client: A THttpClient instance
13+
14+ Returns:
15+ The underlying connection pool
16+ """
17+ return thrift_http_client ._THttpClient__pool
18+
19+ def make_request (
20+ thrift_http_client ,
21+ method : str ,
22+ path : str ,
23+ data : Optional [Dict [str , Any ]] = None ,
24+ params : Optional [Dict [str , Any ]] = None ,
25+ ):
26+ """
27+ Make an HTTP request using a Thrift HTTP client's connection pool.
28+
29+ Args:
30+ thrift_http_client: A THttpClient instance
31+ method: HTTP method (GET, POST, DELETE, etc.)
32+ path: Path to append to the base URL
33+ data: Request body data as a dictionary
34+ params: URL parameters as a dictionary
35+
36+ Returns:
37+ Parsed JSON response
38+ """
39+ # Access the underlying connection pool
40+ pool = extract_connection_pool (thrift_http_client )
41+
42+ # Get the base URI from the Thrift client
43+ scheme = thrift_http_client .scheme
44+ host = thrift_http_client .host
45+ port = thrift_http_client .port
46+
47+ # Build the full URL
48+ base_url = f"{ scheme } ://{ host } "
49+ if port :
50+ base_url += f":{ port } "
51+ url = f"{ path .lstrip ('/' )} "
52+
53+ # Prepare headers
54+ headers = {}
55+ if hasattr (thrift_http_client , '_headers' ) and thrift_http_client ._headers :
56+ headers .update (thrift_http_client ._headers )
57+ headers ["Content-Type" ] = "application/json"
58+
59+ # Make the request
60+ logger .debug ("Making %s request to %s/%s" , method , base_url , url )
61+
62+ response = pool .request (
63+ method = method ,
64+ url = url ,
65+ body = json .dumps (data ).encode ('utf-8' ) if data else None ,
66+ headers = headers ,
67+ fields = params
68+ )
69+
70+ # Check for errors
71+ if response .status >= 400 :
72+ error_message = response .data .decode ('utf-8' )
73+ logger .error ("HTTP error %s: %s" , response .status , error_message )
74+ raise Exception (f"HTTP error { response .status } : { error_message } " )
75+
76+ # Parse and return the response
77+ if response .data :
78+ return json .loads (response .data .decode ('utf-8' ))
79+ return None
0 commit comments