@@ -123,4 +123,83 @@ impl HttpClient {
123
123
// 返回状态码是否为200
124
124
Ok ( response. status ( ) . as_u16 ( ) == 200 )
125
125
}
126
+
127
+ pub async fn proxy_request_with_headers (
128
+ & self ,
129
+ target_url : & str ,
130
+ method : & str ,
131
+ headers : std:: collections:: HashMap < String , String > ,
132
+ body : Vec < u8 > ,
133
+ ) -> Result < Response > {
134
+ log_message (
135
+ format ! ( "Proxying {} request to {} with headers" , method, target_url) ,
136
+ "DEBUG" . to_string ( ) ,
137
+ MODEL_NAME . to_string ( ) ,
138
+ ) ;
139
+
140
+ // 根据HTTP方法构建请求
141
+ let mut request_builder = match method {
142
+ "GET" => self . client . get ( target_url) ,
143
+ "POST" => self . client . post ( target_url) ,
144
+ _ => {
145
+ log_message (
146
+ format ! ( "Unsupported HTTP method: {}" , method) ,
147
+ "ERROR" . to_string ( ) ,
148
+ MODEL_NAME . to_string ( ) ,
149
+ ) ;
150
+ return Err ( anyhow:: anyhow!( "Unsupported HTTP method" ) ) ;
151
+ }
152
+ } ;
153
+
154
+ // 添加headers
155
+ for ( key, value) in headers {
156
+ // 在日志记录前先克隆值
157
+ let key_clone = key. clone ( ) ;
158
+ let value_clone = value. clone ( ) ;
159
+
160
+ log_message (
161
+ format ! ( "Adding header: {} = {}" , key_clone, value_clone) ,
162
+ "DEBUG" . to_string ( ) ,
163
+ MODEL_NAME . to_string ( ) ,
164
+ ) ;
165
+
166
+ request_builder = request_builder. header ( key, value) ;
167
+ }
168
+
169
+ // 添加body并发送请求
170
+ let response = request_builder. body ( body) . send ( ) . await ?;
171
+
172
+ log_message (
173
+ format ! (
174
+ "Received response: Status={}, Content-Length={:?}" ,
175
+ response. status( ) ,
176
+ response. headers( ) . get( "content-length" )
177
+ ) ,
178
+ "INFO" . to_string ( ) ,
179
+ MODEL_NAME . to_string ( ) ,
180
+ ) ;
181
+
182
+ Ok ( response)
183
+ }
184
+
185
+ pub async fn send_request_with_headers (
186
+ & self ,
187
+ target_url : & str ,
188
+ method : & str ,
189
+ headers : std:: collections:: HashMap < String , String > ,
190
+ body : Vec < u8 >
191
+ ) -> Result < String , String > {
192
+ let function_name = "send_request_with_headers" ;
193
+ log_message (
194
+ format ! ( "[{}] Sending {} request to {}" , function_name, method, target_url) ,
195
+ "DEBUG" . to_string ( ) ,
196
+ MODEL_NAME . to_string ( ) ,
197
+ ) ;
198
+
199
+ let response = self . proxy_request_with_headers ( target_url, method, headers, body) . await
200
+ . map_err ( |e| format ! ( "Request failed: {}" , e) ) ?;
201
+
202
+ response. text ( ) . await
203
+ . map_err ( |e| format ! ( "Failed to parse response: {}" , e) )
204
+ }
126
205
}
0 commit comments