mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-11 03:26:58 +02:00
Radix, request methods, and theme stuff
This commit is contained in:
@@ -1,14 +1,24 @@
|
||||
use http::header::{HeaderName, CONTENT_TYPE, USER_AGENT};
|
||||
use http::{HeaderMap, HeaderValue, Method};
|
||||
use reqwest::redirect::Policy;
|
||||
use tauri::{AppHandle, Wry};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct CustomResponse {
|
||||
status: String,
|
||||
body: String,
|
||||
url: String,
|
||||
method: String,
|
||||
elapsed: u128,
|
||||
elapsed2: u128,
|
||||
url: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_request(url: &str) -> Result<CustomResponse, String> {
|
||||
pub async fn send_request(
|
||||
app_handle: AppHandle<Wry>,
|
||||
url: &str,
|
||||
method: &str,
|
||||
) -> Result<CustomResponse, String> {
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
let mut abs_url = url.to_string();
|
||||
@@ -16,10 +26,36 @@ pub async fn send_request(url: &str) -> Result<CustomResponse, String> {
|
||||
abs_url = format!("http://{}", url);
|
||||
}
|
||||
|
||||
let resp = reqwest::get(abs_url.to_string()).await;
|
||||
let client = reqwest::Client::builder()
|
||||
.redirect(Policy::none())
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
// headers.insert(CONTENT_TYPE, HeaderValue::from_static("image/png"));
|
||||
headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
|
||||
headers.insert("x-foo-bar", HeaderValue::from_static("hi mom"));
|
||||
headers.insert(
|
||||
HeaderName::from_static("x-api-key"),
|
||||
HeaderValue::from_static("123-123-123"),
|
||||
);
|
||||
|
||||
let m = Method::from_bytes(method.to_uppercase().as_bytes()).unwrap();
|
||||
let req = client
|
||||
.request(m, abs_url.to_string())
|
||||
.headers(headers)
|
||||
.build()
|
||||
.unwrap();
|
||||
let resp = client.execute(req).await;
|
||||
|
||||
let elapsed = start.elapsed().as_millis();
|
||||
|
||||
crate::runtime::run_plugin_sync("../plugins/plugin.ts").unwrap();
|
||||
let p = app_handle
|
||||
.path_resolver()
|
||||
.resolve_resource("plugins/plugin.ts")
|
||||
.expect("failed to resolve resource");
|
||||
|
||||
crate::runtime::run_plugin_sync(p.to_str().unwrap()).unwrap();
|
||||
|
||||
match resp {
|
||||
Ok(v) => {
|
||||
@@ -32,10 +68,14 @@ pub async fn send_request(url: &str) -> Result<CustomResponse, String> {
|
||||
body,
|
||||
elapsed,
|
||||
elapsed2,
|
||||
method: method.to_string(),
|
||||
url: url2,
|
||||
})
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
Err(e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user