Skip to content

Commit 72ac70d

Browse files
authored
Merge pull request #190 from slaveofcode/feat/api-client
feat(api-client): REST API client with Postman/Insomnia/Swagger import
2 parents 7f2e283 + 900ed40 commit 72ac70d

18 files changed

Lines changed: 2027 additions & 4 deletions

src-tauri/Cargo.lock

Lines changed: 162 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tauri-plugin-updater = "2"
1919
serde = { version = "1", features = ["derive"] }
2020
serde_json = "1"
2121
tokio = { version = "1", features = ["full"] }
22+
reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false }
2223
chrono = "0.4"
2324
lazy_static = "1.5"
2425
image = "0.25"

src-tauri/src/commands.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,64 @@ mod tests {
10291029
assert_eq!(out.get_pixel(3, 3), &image::Rgba([5, 5, 0, 255]));
10301030
}
10311031
}
1032+
1033+
// ---- HTTP request proxy (used by the API Client tool) ----
1034+
1035+
#[derive(serde::Serialize)]
1036+
pub struct HttpResponse {
1037+
pub status: u16,
1038+
pub status_text: String,
1039+
pub headers: std::collections::HashMap<String, String>,
1040+
pub body: String,
1041+
pub duration_ms: u64,
1042+
}
1043+
1044+
#[tauri::command]
1045+
pub async fn http_request(
1046+
method: String,
1047+
url: String,
1048+
headers: std::collections::HashMap<String, String>,
1049+
body: Option<String>,
1050+
) -> Result<HttpResponse, String> {
1051+
let start = std::time::Instant::now();
1052+
let client = reqwest::Client::builder()
1053+
.danger_accept_invalid_certs(false)
1054+
.redirect(reqwest::redirect::Policy::limited(10))
1055+
.build()
1056+
.map_err(|e| e.to_string())?;
1057+
1058+
let method_parsed = reqwest::Method::from_bytes(method.to_uppercase().as_bytes())
1059+
.map_err(|e| e.to_string())?;
1060+
1061+
let mut builder = client.request(method_parsed, &url);
1062+
for (k, v) in &headers {
1063+
if let (Ok(name), Ok(val)) = (
1064+
reqwest::header::HeaderName::from_bytes(k.as_bytes()),
1065+
reqwest::header::HeaderValue::from_str(v),
1066+
) {
1067+
builder = builder.header(name, val);
1068+
}
1069+
}
1070+
if let Some(b) = body {
1071+
builder = builder.body(b);
1072+
}
1073+
1074+
let res = builder.send().await.map_err(|e| e.to_string())?;
1075+
let duration_ms = start.elapsed().as_millis() as u64;
1076+
let status = res.status().as_u16();
1077+
let status_text = res.status().canonical_reason().unwrap_or("").to_string();
1078+
let mut resp_headers = std::collections::HashMap::new();
1079+
for (k, v) in res.headers() {
1080+
if let Ok(val) = v.to_str() {
1081+
resp_headers.insert(k.as_str().to_string(), val.to_string());
1082+
}
1083+
}
1084+
let body_str = res.text().await.map_err(|e| e.to_string())?;
1085+
Ok(HttpResponse {
1086+
status,
1087+
status_text,
1088+
headers: resp_headers,
1089+
body: body_str,
1090+
duration_ms,
1091+
})
1092+
}

src-tauri/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fn main() {
5050
commands::check_permissions,
5151
commands::mark_first_run_complete,
5252
commands::open_system_preferences,
53+
commands::http_request,
5354
])
5455
.run(tauri::generate_context!())
5556
.expect("error while running tauri application");

0 commit comments

Comments
 (0)