Files
craftlabs-authorization-sdk/native/craftlabs-auth-cli/src/platform_api.rs
T

53 lines
1.3 KiB
Rust

use serde::{Serialize, Deserialize};
#[derive(Serialize)]
struct ActivationReport {
sn: String,
device_id: String,
timestamp: String,
}
#[derive(Deserialize)]
struct ApiResponse {
status: String,
message: Option<String>,
}
pub struct PlatformClient {
base_url: String,
client: reqwest::Client,
}
impl PlatformClient {
pub fn new(base_url: &str) -> Self {
PlatformClient {
base_url: base_url.trim_end_matches('/').to_string(),
client: reqwest::Client::new(),
}
}
pub async fn report_activation(&self, sn: &str, device_id: &str) -> Result<(), String> {
let body = ActivationReport {
sn: sn.to_string(),
device_id: device_id.to_string(),
timestamp: chrono::Utc::now().to_rfc3339(),
};
let url = format!("{}/api/v1/licenses/activate", self.base_url);
let resp = self.client
.post(&url)
.json(&body)
.send()
.await
.map_err(|e| format!("请求失败: {}", e))?;
if resp.status().is_success() {
Ok(())
} else {
let status = resp.status();
let text = resp.text().await.unwrap_or_default();
Err(format!("HTTP {}: {}", status, text))
}
}
}