Fix shutdown API, update connection state after shutdown
This commit is contained in:
18
src/api.rs
18
src/api.rs
@@ -253,30 +253,20 @@ impl ProxmoxClient {
|
||||
|
||||
pub async fn shutdown_node(&self, node: &str) -> Result<String, String> {
|
||||
let url = format!("{}/nodes/{}/status", self.base_url, node);
|
||||
println!("[API] 关机请求: POST {}", url);
|
||||
println!("[API] Headers: Authorization={}", self.auth_header());
|
||||
|
||||
let resp = self.client
|
||||
.post(&url)
|
||||
.header("Authorization", self.auth_header())
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&serde_json::json!({"command": "shutdown"}))
|
||||
.form(&[("command", "shutdown")])
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
let err = format!("网络错误: {}", e);
|
||||
println!("[API] {}", err);
|
||||
err
|
||||
})?;
|
||||
.map_err(|e| format!("网络错误: {}", e))?;
|
||||
|
||||
let status = resp.status();
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
println!("[API] 响应状态: {}", status);
|
||||
println!("[API] 响应内容: {}", body);
|
||||
|
||||
if !status.is_success() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(format!("HTTP {}: {}", status, body));
|
||||
}
|
||||
Ok(body)
|
||||
Ok("ok".to_string())
|
||||
}
|
||||
}
|
||||
@@ -530,8 +530,10 @@ impl eframe::App for App {
|
||||
if let Some(c) = client.as_ref() {
|
||||
match c.shutdown_node(&node).await {
|
||||
Ok(response) => {
|
||||
state.write().unwrap().add_log("✓ 关机命令发送成功");
|
||||
state.write().unwrap().add_log(&format!("响应: {}", response));
|
||||
let mut s = state.write().unwrap();
|
||||
s.add_log("✓ 关机命令发送成功");
|
||||
s.add_log(&format!("响应: {}", response));
|
||||
s.is_connected = false;
|
||||
}
|
||||
Err(e) => {
|
||||
state.write().unwrap().add_log(&format!("✗ 关机失败: {}", e));
|
||||
|
||||
Reference in New Issue
Block a user