wp_mini/endpoints/user.rs
1use crate::client::WattpadRequestBuilder;
2use crate::field::UserField;
3use crate::types::UserResponse;
4use crate::WattpadError;
5use std::sync::atomic::AtomicBool;
6use std::sync::Arc;
7
8/// Provides access to user-related API endpoints.
9///
10/// This client allows you to fetch public information about Wattpad users.
11pub struct UserClient {
12 /// The shared `reqwest` client for making HTTP requests.
13 pub(crate) http: reqwest::Client,
14 /// A flag indicating whether the main client is authenticated.
15 pub(crate) is_authenticated: Arc<AtomicBool>,
16}
17
18impl UserClient {
19 /// Fetches detailed public information about a specific user.
20 ///
21 /// This function retrieves a user's profile data, such as their follower count,
22 /// stories they've written, and more.
23 ///
24 /// # Arguments
25 /// * `username` - The username of the user to fetch.
26 /// * `fields` - An optional slice of `UserField` specifying which fields to retrieve.
27 /// If `None`, a default set of fields will be requested.
28 ///
29 /// # Returns
30 /// A `Result` containing a `UserResponse` struct with the user's data on success.
31 ///
32 /// # Errors
33 /// Returns a `WattpadError` if the network request fails, the API returns an error
34 /// (e.g., user not found), or a requested field requires authentication when the
35 /// client is unauthenticated.
36 ///
37 /// # Examples
38 /// ```no_run
39 /// # use wattpad::{WattpadClient, field::UserField};
40 /// # #[tokio::main]
41 /// # async fn main() -> Result<(), wattpad::WattpadError> {
42 /// let client = WattpadClient::new();
43 /// let username = "test";
44 /// let fields = &[UserField::Username, UserField::FollowerCount];
45 ///
46 /// let user_info = client.user.get_user_info(username, Some(fields)).await?;
47 ///
48 /// println!("User: {}", user_info.username);
49 /// println!("Followers: {}", user_info.follower_count);
50 /// # Ok(())
51 /// # }
52 /// ```
53 pub async fn get_user_info(
54 &self,
55 username: &str,
56 fields: Option<&[UserField]>,
57 ) -> Result<UserResponse, WattpadError> {
58 WattpadRequestBuilder::new(
59 &self.http,
60 &self.is_authenticated,
61 reqwest::Method::GET,
62 &format!("/api/v3/users/{}", username),
63 )
64 .fields(fields)?
65 .execute()
66 .await
67 }
68}