1use serde::{Deserialize, Serialize};
2use reqwest::Client;
3use std::error::Error;
4
5#[derive(Serialize, Deserialize)]
6pub struct LoginRequest {
7 pub username: String,
8 pub password: String,
9 pub hwid: String,
10}
11
12pub struct LicenseAPI {
13 url: String,
14 client: Client,
15}
16
17impl LicenseAPI {
18 pub fn new(url: impl Into<String>) -> Self {
19 LicenseAPI {
20 url: url.into(),
21 client: Client::new(),
22 }
23 }
24
25 pub async fn login(&self, creds: &LoginRequest) -> Result<bool, Box<dyn Error + Send + Sync>> {
26 let login_resp = self
27 .client
28 .post(&format!("{}/auth/login", self.url.trim_end_matches('/')))
29 .json(&serde_json::json!({
30 "username": creds.username,
31 "password": creds.password,
32 }))
33 .send()
34 .await?
35 .error_for_status()?;
36
37 let access_token: String = login_resp
38 .json::<serde_json::Value>()
39 .await?
40 .get("access_token")
41 .and_then(|v| v.as_str().map(String::from))
42 .ok_or("missing access_token in response")?;
43
44 let _ = self
45 .client
46 .patch(&format!("{}/users/hwid", self.url.trim_end_matches('/')))
47 .bearer_auth(&access_token)
48 .json(&serde_json::json!({ "hwid": creds.hwid }))
49 .send()
50 .await?
51 .error_for_status()?;
52
53 Ok(true)
54 }
55}