rive_http/users/
user_information.rs

1use crate::prelude::*;
2use rive_models::{
3    data::{ChangeUsernameData, EditUserData},
4    user::{User, UserProfile},
5};
6
7impl Client {
8    /// Retrieve your user information.
9    pub async fn fetch_self(&self) -> Result<User> {
10        Ok(self
11            .client
12            .get(ep!(self, "/users/@me"))
13            .auth(&self.authentication)
14            .send()
15            .await?
16            .process_error()
17            .await?
18            .json()
19            .await?)
20    }
21
22    /// Edit currently authenticated user.
23    pub async fn edit_user(&self, data: EditUserData) -> Result<User> {
24        Ok(self
25            .client
26            .patch(ep!(self, "/users/@me"))
27            .auth(&self.authentication)
28            .json(&data)
29            .send()
30            .await?
31            .process_error()
32            .await?
33            .json()
34            .await?)
35    }
36
37    /// Fetch a user's information.
38    pub async fn fetch_user(&self, id: impl Into<String>) -> Result<User> {
39        Ok(self
40            .client
41            .get(ep!(self, "/users/{}", id.into()))
42            .auth(&self.authentication)
43            .send()
44            .await?
45            .process_error()
46            .await?
47            .json()
48            .await?)
49    }
50
51    /// Change your username.
52    pub async fn change_username(&self, data: ChangeUsernameData) -> Result<User> {
53        Ok(self
54            .client
55            .patch(ep!(self, "/users/@me/username"))
56            .auth(&self.authentication)
57            .json(&data)
58            .send()
59            .await?
60            .process_error()
61            .await?
62            .json()
63            .await?)
64    }
65
66    /// This returns a default avatar based on the given id.
67    pub async fn fetch_default_avatar(&self, id: impl Into<String>) -> Result<Vec<u8>> {
68        Ok(self
69            .client
70            .get(ep!(self, "/users/{}/default_avatar", id.into()))
71            .auth(&self.authentication)
72            .send()
73            .await?
74            .process_error()
75            .await?
76            .bytes()
77            .await?
78            .into())
79    }
80
81    /// Retrieve a user's profile data.
82    ///
83    ///Will fail if you do not have permission to access the other user's profile.
84    pub async fn fetch_user_profile(&self, id: impl Into<String>) -> Result<UserProfile> {
85        Ok(self
86            .client
87            .get(ep!(self, "/users/{}/profile", id.into()))
88            .auth(&self.authentication)
89            .send()
90            .await?
91            .process_error()
92            .await?
93            .json()
94            .await?)
95    }
96}