11//
2- // 普通 HTTP 请求处理模块
2+ // HTTP request handler module
33//
44
55use std:: time:: Duration ;
@@ -20,14 +20,6 @@ pub struct FetchResponse {
2020 body : Vec < u8 > ,
2121}
2222
23- #[ derive( Debug , Clone , serde:: Serialize ) ]
24- pub struct ErrorResponse {
25- request_id : u32 ,
26- status : u16 ,
27- status_text : String ,
28- error : String ,
29- }
30-
3123#[ tauri:: command]
3224pub async fn http_fetch (
3325 method : String ,
@@ -57,11 +49,11 @@ pub async fn http_fetch(
5749 }
5850 }
5951
60- // 解析 HTTP 方法
52+ // Parse HTTP method
6153 let method = method. parse :: < reqwest:: Method > ( )
6254 . map_err ( |err| format ! ( "failed to parse method: {}" , err) ) ?;
6355
64- // 创建客户端
56+ // Create client
6557 let client = Client :: builder ( )
6658 . default_headers ( _headers)
6759 . redirect ( reqwest:: redirect:: Policy :: limited ( 3 ) )
@@ -70,14 +62,14 @@ pub async fn http_fetch(
7062 . build ( )
7163 . map_err ( |err| format ! ( "failed to create client: {}" , err) ) ?;
7264
73- // 构建请求
65+ // Build request
7466 let mut request = client. request (
7567 method. clone ( ) ,
7668 url. parse :: < reqwest:: Url > ( )
7769 . map_err ( |err| format ! ( "failed to parse url: {}" , err) ) ?
7870 ) ;
7971
80- // 对于需要 body 的请求方法,添加请求体
72+ // For request methods that need a body, add the request body
8173 if method == reqwest:: Method :: POST
8274 || method == reqwest:: Method :: PUT
8375 || method == reqwest:: Method :: PATCH
@@ -88,7 +80,7 @@ pub async fn http_fetch(
8880 }
8981 }
9082
91- // 发送请求
83+ // Send request
9284 let response = request. send ( ) . await
9385 . map_err ( |err| {
9486 let error_msg = err. source ( )
@@ -97,7 +89,7 @@ pub async fn http_fetch(
9789 format ! ( "request failed: {}" , error_msg)
9890 } ) ?;
9991
100- // 获取响应状态和头部
92+ // Get response status and headers
10193 let status = response. status ( ) . as_u16 ( ) ;
10294 let status_text = response. status ( ) . canonical_reason ( )
10395 . unwrap_or ( "Unknown" )
@@ -113,7 +105,7 @@ pub async fn http_fetch(
113105 ) ;
114106 }
115107
116- // 读取响应体
108+ // Read response body
117109 let response_body = response. bytes ( ) . await
118110 . map_err ( |err| format ! ( "failed to read response body: {}" , err) ) ?;
119111
@@ -134,13 +126,13 @@ pub async fn http_fetch_text(
134126 body : String ,
135127) -> Result < String , String > {
136128
137- // 将字符串 body 转换为字节
129+ // Convert string body to bytes
138130 let body_bytes = body. into_bytes ( ) ;
139131
140- // 调用主要的 fetch 方法
132+ // Call the main fetch method
141133 let response = http_fetch ( method, url, headers, body_bytes) . await ?;
142134
143- // 将响应体转换为字符串
135+ // Convert response body to string
144136 let response_text = String :: from_utf8 ( response. body )
145137 . map_err ( |err| format ! ( "failed to convert response to text: {}" , err) ) ?;
146138
@@ -155,21 +147,21 @@ pub async fn http_fetch_json(
155147 body : serde_json:: Value ,
156148) -> Result < serde_json:: Value , String > {
157149
158- // 将 JSON 转换为字符串再转换为字节
150+ // Convert JSON to string and then to bytes
159151 let body_string = serde_json:: to_string ( & body)
160152 . map_err ( |err| format ! ( "failed to serialize JSON body: {}" , err) ) ?;
161153 let body_bytes = body_string. into_bytes ( ) ;
162154
163- // 确保设置了正确的 Content-Type
155+ // Ensure the correct Content-Type is set
164156 let mut json_headers = headers;
165157 if !json_headers. contains_key ( "content-type" ) && !json_headers. contains_key ( "Content-Type" ) {
166158 json_headers. insert ( "Content-Type" . to_string ( ) , "application/json" . to_string ( ) ) ;
167159 }
168160
169- // 调用主要的 fetch 方法
161+ // Call the main fetch method
170162 let response = http_fetch ( method, url, json_headers, body_bytes) . await ?;
171163
172- // 将响应体解析为 JSON
164+ // Parse response body as JSON
173165 let response_json: serde_json:: Value = serde_json:: from_slice ( & response. body )
174166 . map_err ( |err| format ! ( "failed to parse response as JSON: {}" , err) ) ?;
175167
0 commit comments