Return struct for response

This commit is contained in:
Gregory Schier
2023-02-15 20:26:33 -08:00
parent 2f1d3a5c60
commit 2418f737e7
5 changed files with 109 additions and 52 deletions

View File

@@ -9,16 +9,38 @@ fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
#[derive(serde::Serialize)]
struct CustomResponse {
status: String,
body: String,
elapsed: u128,
elapsed2: u128,
}
#[tauri::command]
async fn send_request(url: &str) -> Result<String, String> {
async fn send_request(url: &str) -> Result<CustomResponse, String> {
let start = std::time::Instant::now();
let mut url = url.to_string();
if !url.starts_with("http://") && !url.starts_with("https://") {
url = format!("http://{}", url);
}
let resp = reqwest::get(url).await;
let elapsed = start.elapsed().as_millis();
match resp {
Ok(v) => Ok(v.text().await.unwrap()),
Ok(v) => {
let status = v.status().to_string();
let body = v.text().await.unwrap();
let elapsed2 = start.elapsed().as_millis();
Ok(CustomResponse {
status,
body,
elapsed,
elapsed2,
})
}
Err(e) => Err(e.to_string()),
}
}