-
Notifications
You must be signed in to change notification settings - Fork 59.8k
Expand file tree
/
Copy pathfetch.rs
More file actions
169 lines (143 loc) · 4.59 KB
/
fetch.rs
File metadata and controls
169 lines (143 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// HTTP request handler module
//
use std::time::Duration;
use std::error::Error;
use std::sync::atomic::{AtomicU32, Ordering};
use std::collections::HashMap;
use reqwest::Client;
use reqwest::header::{HeaderName, HeaderMap};
static REQUEST_COUNTER: AtomicU32 = AtomicU32::new(0);
#[derive(Debug, Clone, serde::Serialize)]
pub struct FetchResponse {
request_id: u32,
status: u16,
status_text: String,
headers: HashMap<String, String>,
body: Vec<u8>,
}
#[tauri::command]
pub async fn http_fetch(
method: String,
url: String,
headers: HashMap<String, String>,
body: Vec<u8>,
) -> Result<FetchResponse, String> {
let request_id = REQUEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let mut _headers = HeaderMap::new();
for (key, value) in &headers {
match key.parse::<HeaderName>() {
Ok(header_name) => {
match value.parse() {
Ok(header_value) => {
_headers.insert(header_name, header_value);
}
Err(err) => {
return Err(format!("failed to parse header value '{}': {}", value, err));
}
}
}
Err(err) => {
return Err(format!("failed to parse header name '{}': {}", key, err));
}
}
}
// Parse HTTP method
let method = method.parse::<reqwest::Method>()
.map_err(|err| format!("failed to parse method: {}", err))?;
// Create client
let client = Client::builder()
.default_headers(_headers)
.redirect(reqwest::redirect::Policy::limited(3))
.connect_timeout(Duration::new(10, 0))
.timeout(Duration::new(30, 0))
.build()
.map_err(|err| format!("failed to create client: {}", err))?;
// Build request
let mut request = client.request(
method.clone(),
url.parse::<reqwest::Url>()
.map_err(|err| format!("failed to parse url: {}", err))?
);
// For request methods that need a body, add the request body
if method == reqwest::Method::POST
|| method == reqwest::Method::PUT
|| method == reqwest::Method::PATCH
|| method == reqwest::Method::DELETE {
if !body.is_empty() {
let body_bytes = bytes::Bytes::from(body);
request = request.body(body_bytes);
}
}
// Send request
let response = request.send().await
.map_err(|err| {
let error_msg = err.source()
.map(|e| e.to_string())
.unwrap_or_else(|| err.to_string());
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")
.to_string();
let mut response_headers = HashMap::new();
for (name, value) in response.headers() {
response_headers.insert(
name.as_str().to_string(),
std::str::from_utf8(value.as_bytes())
.unwrap_or("<invalid utf8>")
.to_string()
);
}
// Read response body
let response_body = response.bytes().await
.map_err(|err| format!("failed to read response body: {}", err))?;
Ok(FetchResponse {
request_id,
status,
status_text,
headers: response_headers,
body: response_body.to_vec(),
})
}
#[tauri::command]
pub async fn http_fetch_text(
method: String,
url: String,
headers: HashMap<String, String>,
body: String,
) -> Result<String, String> {
// Convert string body to bytes
let body_bytes = body.into_bytes();
// 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))?;
Ok(response_text)
}
#[tauri::command]
pub async fn http_fetch_json(
method: String,
url: String,
headers: HashMap<String, String>,
body: serde_json::Value,
) -> Result<serde_json::Value, String> {
// 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();
// 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());
}
// Call the main fetch method
let response = http_fetch(method, url, json_headers, body_bytes).await?;
// 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))?;
Ok(response_json)
}