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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::prelude::*;
use rive_models::{
    payload::{DeleteAllSessionsPayload, EditSessionPayload, LoginPayload},
    session::{LoginResponse, SessionInfo},
};

impl Client {
    /// Login to an account.
    pub async fn login(&self, payload: LoginPayload) -> Result<LoginResponse> {
        Ok(self
            .client
            .post(ep!(self, "/auth/session/login"))
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Delete current session.
    pub async fn logout(&self) -> Result<()> {
        self.client
            .post(ep!(self, "/auth/session/logout"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Fetch all sessions associated with this account.
    pub async fn fetch_sessions(&self) -> Result<Vec<SessionInfo>> {
        Ok(self
            .client
            .get(ep!(self, "/auth/session/all"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Delete all active sessions, optionally including current one.
    pub async fn delete_all_sessions(&self, payload: DeleteAllSessionsPayload) -> Result<()> {
        self.client
            .delete(ep!(self, "/auth/session/all"))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Delete a specific active session.
    pub async fn revoke_session(&self, id: impl Into<String>) -> Result<()> {
        self.client
            .delete(ep!(self, "/auth/session/{}", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Edit specific session information.
    pub async fn edit_session(
        &self,
        id: impl Into<String>,
        payload: EditSessionPayload,
    ) -> Result<SessionInfo> {
        Ok(self
            .client
            .patch(ep!(self, "/auth/session/{}", id.into()))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }
}