1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use super::*;

#[derive(Serialize)]
struct Request {
    email: String,
    password: String,
}

#[derive(Deserialize)]
struct Response {
    id: i32,
}

#[derive(Debug)]
pub enum Returns {
    InvalidEmail,
    InvalidPassword,
    Valid(i32),
}

impl Client {
    // Returns ID of user
    pub async fn validate_user(
        &self,
        email: String,
        password: String,
    ) -> Result<Returns, Box<dyn std::error::Error>> {
        let res = req()
            .get(self.endpoint("/v1/users/validate", vec![]))
            .json(&Request { email, password })
            .send()
            .await?;

        match res.status() {
            StatusCode::NOT_FOUND => Ok(Returns::InvalidEmail),
            StatusCode::UNAUTHORIZED => Ok(Returns::InvalidPassword),
            StatusCode::OK => Ok(Returns::Valid(res.json::<Response>().await?.id)),
            _ => Err("Unknown Response Status Code".into()),
        }
    }
}