librelink_client/
login.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug)]
4pub struct ResponseLoginRequest {
5    pub status: i32,
6    pub data: Data,
7}
8
9#[derive(Serialize, Deserialize, Debug)]
10pub struct Data {
11    #[serde(rename = "authTicket")]
12    pub auth_ticket: AuthTicket,
13}
14
15#[derive(Serialize, Deserialize, Debug)]
16pub struct AuthTicket {
17    pub token: String,
18    pub expires: u64,
19    pub duration: u64,
20}
21
22#[derive(Serialize, Deserialize, Debug)]
23pub struct ErrorResponse {
24    pub status: i32,
25    pub error: Error,
26}
27
28#[derive(Serialize, Deserialize, Debug)]
29pub struct Error {
30    pub message: String,
31}
32
33pub async fn try_get_access_token(
34    username: &str,
35    password: &str,
36) -> Result<String, Box<dyn std::error::Error>> {
37    let url = "https://api.libreview.io/llu/auth/login";
38
39    let login_request = serde_json::json!({
40        "email": username,
41        "password": password,
42    });
43
44    let client = reqwest::Client::new();
45
46    let response = client
47        .post(url)
48        .header("version", "4.2.1")
49        .header("product", "llu.android")
50        .header("User-Agent", "Apidog/1.0.0 (https://apidog.com)")
51        .json(&login_request)
52        .send()
53        .await?;
54
55    let text = response.text().await?;
56    let api_response: Result<ResponseLoginRequest, serde_json::Error> = serde_json::from_str(&text);
57
58    match api_response {
59        Ok(response_data) => Ok(response_data.data.auth_ticket.token),
60        Err(_) => {
61            let error_response: ErrorResponse = serde_json::from_str(&text).unwrap();
62            if error_response.status == 2 {
63                Err(Box::new(std::io::Error::new(
64                    std::io::ErrorKind::Other,
65                    error_response.error.message,
66                )))
67            } else {
68                Err(Box::new(std::io::Error::new(
69                    std::io::ErrorKind::Other,
70                    "Unknown error",
71                )))
72            }
73        }
74    }
75}