first functionable
This commit is contained in:
85
src-tauri/src/lib.rs
Normal file
85
src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use std::collections::HashMap;
|
||||
use std::time::Instant;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct HttpResponse {
|
||||
status: u16,
|
||||
headers: HashMap<String, String>,
|
||||
body: String,
|
||||
time_elapsed: u128, // milliseconds
|
||||
}
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn execute_request(
|
||||
method: String,
|
||||
url: String,
|
||||
headers: HashMap<String, String>,
|
||||
body: Option<String>,
|
||||
query_params: Option<HashMap<String, String>>,
|
||||
) -> Result<HttpResponse, String> {
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(30))
|
||||
.build()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let req_method = method.parse::<reqwest::Method>().map_err(|e| e.to_string())?;
|
||||
|
||||
let mut request_builder = client.request(req_method, &url);
|
||||
|
||||
// Add Query Params
|
||||
if let Some(params) = query_params {
|
||||
request_builder = request_builder.query(¶ms);
|
||||
}
|
||||
|
||||
// Add Headers
|
||||
for (key, value) in headers {
|
||||
request_builder = request_builder.header(key, value);
|
||||
}
|
||||
|
||||
// Add Body
|
||||
if let Some(b) = body {
|
||||
if !b.is_empty() {
|
||||
request_builder = request_builder.body(b);
|
||||
}
|
||||
}
|
||||
|
||||
let start_time = Instant::now();
|
||||
// Execute request
|
||||
let response = request_builder.send().await.map_err(|e| e.to_string())?;
|
||||
let time_elapsed = start_time.elapsed().as_millis();
|
||||
|
||||
let status = response.status().as_u16();
|
||||
|
||||
let mut response_headers = HashMap::new();
|
||||
for (key, value) in response.headers() {
|
||||
// Handle header value to string conversion (skipping non-utf8 for simplicity or lossy conv)
|
||||
let val_str = value.to_str().unwrap_or("").to_string();
|
||||
// Capitalize or keep standard key format? keeping standard.
|
||||
response_headers.insert(key.to_string(), val_str);
|
||||
}
|
||||
|
||||
let body_text = response.text().await.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(HttpResponse {
|
||||
status,
|
||||
headers: response_headers,
|
||||
body: body_text,
|
||||
time_elapsed,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet, execute_request])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
6
src-tauri/src/main.rs
Normal file
6
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
rest_client_lib::run()
|
||||
}
|
||||
Reference in New Issue
Block a user