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
92
93
94
95
96
use crate::prelude::*;
use rive_models::{
    payload::{ChangeUsernamePayload, EditUserPayload},
    user::{User, UserProfile},
};

impl Client {
    /// Retrieve your user information.
    pub async fn fetch_self(&self) -> Result<User> {
        Ok(self
            .client
            .get(ep!(self, "/users/@me"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Edit currently authenticated user.
    pub async fn edit_user(&self, payload: EditUserPayload) -> Result<User> {
        Ok(self
            .client
            .patch(ep!(self, "/users/@me"))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Fetch a user's information.
    pub async fn fetch_user(&self, id: impl Into<String>) -> Result<User> {
        Ok(self
            .client
            .get(ep!(self, "/users/{}", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Change your username.
    pub async fn change_username(&self, payload: ChangeUsernamePayload) -> Result<User> {
        Ok(self
            .client
            .patch(ep!(self, "/users/@me/username"))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// This returns a default avatar based on the given id.
    pub async fn fetch_default_avatar(&self, id: impl Into<String>) -> Result<Vec<u8>> {
        Ok(self
            .client
            .get(ep!(self, "/users/{}/default_avatar", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .bytes()
            .await?
            .into())
    }

    /// Retrieve a user's profile data.
    ///
    ///Will fail if you do not have permission to access the other user's profile.
    pub async fn fetch_user_profile(&self, id: impl Into<String>) -> Result<UserProfile> {
        Ok(self
            .client
            .get(ep!(self, "/users/{}/profile", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }
}