From 3fd852a5e6967e7af53d2c38e35d6f4c99a873b6 Mon Sep 17 00:00:00 2001 From: EvanWu <850123119@qq.com> Date: Tue, 9 Sep 2025 15:45:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=20fetch.rs=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=AD=E7=9A=84=E6=B3=A8=E9=87=8A=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E4=B8=AD=E6=96=87=E6=B3=A8=E9=87=8A=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E4=B8=BA=E8=8B=B1=E6=96=87=EF=BC=8C=E4=BB=A5=E6=8F=90=E9=AB=98?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/fetch.rs | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src-tauri/src/fetch.rs b/src-tauri/src/fetch.rs index 36048eec2..21f1cb1a8 100644 --- a/src-tauri/src/fetch.rs +++ b/src-tauri/src/fetch.rs @@ -1,5 +1,5 @@ // -// 普通 HTTP 请求处理模块 +// HTTP request handler module // use std::time::Duration; @@ -20,14 +20,6 @@ pub struct FetchResponse { body: Vec, } -#[derive(Debug, Clone, serde::Serialize)] -pub struct ErrorResponse { - request_id: u32, - status: u16, - status_text: String, - error: String, -} - #[tauri::command] pub async fn http_fetch( method: String, @@ -57,11 +49,11 @@ pub async fn http_fetch( } } - // 解析 HTTP 方法 + // Parse HTTP method let method = method.parse::() .map_err(|err| format!("failed to parse method: {}", err))?; - // 创建客户端 + // Create client let client = Client::builder() .default_headers(_headers) .redirect(reqwest::redirect::Policy::limited(3)) @@ -70,14 +62,14 @@ pub async fn http_fetch( .build() .map_err(|err| format!("failed to create client: {}", err))?; - // 构建请求 + // Build request let mut request = client.request( method.clone(), url.parse::() .map_err(|err| format!("failed to parse url: {}", err))? ); - // 对于需要 body 的请求方法,添加请求体 + // For request methods that need a body, add the request body if method == reqwest::Method::POST || method == reqwest::Method::PUT || method == reqwest::Method::PATCH @@ -88,7 +80,7 @@ pub async fn http_fetch( } } - // 发送请求 + // Send request let response = request.send().await .map_err(|err| { let error_msg = err.source() @@ -97,7 +89,7 @@ pub async fn http_fetch( format!("request failed: {}", error_msg) })?; - // 获取响应状态和头部 + // Get response status and headers let status = response.status().as_u16(); let status_text = response.status().canonical_reason() .unwrap_or("Unknown") @@ -113,7 +105,7 @@ pub async fn http_fetch( ); } - // 读取响应体 + // Read response body let response_body = response.bytes().await .map_err(|err| format!("failed to read response body: {}", err))?; @@ -134,13 +126,13 @@ pub async fn http_fetch_text( body: String, ) -> Result { - // 将字符串 body 转换为字节 + // Convert string body to bytes let body_bytes = body.into_bytes(); - // 调用主要的 fetch 方法 + // Call the main fetch method let response = http_fetch(method, url, headers, body_bytes).await?; - // 将响应体转换为字符串 + // Convert response body to string let response_text = String::from_utf8(response.body) .map_err(|err| format!("failed to convert response to text: {}", err))?; @@ -155,21 +147,21 @@ pub async fn http_fetch_json( body: serde_json::Value, ) -> Result { - // 将 JSON 转换为字符串再转换为字节 + // Convert JSON to string and then to bytes let body_string = serde_json::to_string(&body) .map_err(|err| format!("failed to serialize JSON body: {}", err))?; let body_bytes = body_string.into_bytes(); - // 确保设置了正确的 Content-Type + // Ensure the correct Content-Type is set let mut json_headers = headers; if !json_headers.contains_key("content-type") && !json_headers.contains_key("Content-Type") { json_headers.insert("Content-Type".to_string(), "application/json".to_string()); } - // 调用主要的 fetch 方法 + // Call the main fetch method let response = http_fetch(method, url, json_headers, body_bytes).await?; - // 将响应体解析为 JSON + // Parse response body as JSON let response_json: serde_json::Value = serde_json::from_slice(&response.body) .map_err(|err| format!("failed to parse response as JSON: {}", err))?;