wp_mini/endpoints/
user.rs

1use crate::client::WattpadRequestBuilder;
2use crate::field::{StoryField, UserField};
3use crate::types::{StoryResponse, UserResponse, UserStoriesResponse};
4use crate::WattpadError;
5use std::sync::atomic::AtomicBool;
6use std::sync::Arc;
7use crate::pagination::Paginator;
8
9/// Provides access to user-related API endpoints.
10///
11/// This client allows you to fetch public information about Wattpad users.
12pub struct UserClient {
13    /// The shared `reqwest` client for making HTTP requests.
14    pub(crate) http: reqwest::Client,
15    /// A flag indicating whether the main client is authenticated.
16    pub(crate) is_authenticated: Arc<AtomicBool>,
17}
18
19impl UserClient {
20    /// Fetches detailed public information about a specific user.
21    ///
22    /// This function retrieves a user's profile data, such as their follower count,
23    /// stories they've written, and more.
24    ///
25    /// # Arguments
26    /// * `username` - The username of the user to fetch.
27    /// * `fields` - An optional slice of `UserField` specifying which fields to retrieve.
28    ///   If `None`, a default set of fields will be requested.
29    ///
30    /// # Returns
31    /// A `Result` containing a `UserResponse` struct with the user's data on success.
32    ///
33    /// # Errors
34    /// Returns a `WattpadError` if the network request fails, the API returns an error
35    /// (e.g., user not found), or a requested field requires authentication when the
36    /// client is unauthenticated.
37    ///
38    /// # Examples
39    /// ```no_run
40    /// # use wattpad::{WattpadClient, field::UserField};
41    /// # #[tokio::main]
42    /// # async fn main() -> Result<(), wattpad::WattpadError> {
43    /// let client = WattpadClient::new();
44    /// let username = "test";
45    /// let fields = &[UserField::Username, UserField::FollowerCount];
46    ///
47    /// let user_info = client.user.get_user_info(username, Some(fields)).await?;
48    ///
49    /// println!("User: {}", user_info.username);
50    /// println!("Followers: {}", user_info.follower_count);
51    /// # Ok(())
52    /// # }
53    /// ```
54    pub async fn get_user_info(
55        &self,
56        username: &str,
57        fields: Option<&[UserField]>,
58    ) -> Result<UserResponse, WattpadError> {
59        WattpadRequestBuilder::new(
60            &self.http,
61            &self.is_authenticated,
62            reqwest::Method::GET,
63            &format!("/api/v3/users/{}", username),
64        )
65            .fields(fields, None)?
66            .execute()
67            .await
68    }
69
70    /// Creates a request builder to fetch user stories.
71    ///
72    /// You can configure the limit/offset or call `.execute_all().await`
73    /// to get everything.
74    ///
75    /// # Arguments
76    /// * `username` - The username of the user to fetch.
77    /// * `fields` - An optional slice of `StoryField` specifying which fields to retrieve.
78    ///   If `None`, a default set of fields will be requested.
79    ///
80    /// # Returns
81    /// A `Result` containing a `UserStoriesResponse` struct with the user's data on success.
82    ///
83    /// # Errors
84    /// Returns a `WattpadError` if the network request fails, the API returns an error
85    /// (e.g., user not found), or a requested field requires authentication when the
86    /// client is unauthenticated.
87    ///
88    /// # Examples
89    /// ```no_run
90    /// # use wattpad::{WattpadClient, field::StoryField};
91    /// # #[tokio::main]
92    /// # async fn main() -> Result<(), wattpad::WattpadError> {
93    /// let client = WattpadClient::new();
94    /// let username = "test";
95    /// let fields = &[StoryField::Id, StoryField::Title];
96    ///
97    /// let result_of_all_stories = client.user.get_user_stories(username, Some(fields)).execute_all().await?;
98    ///
99    /// // Use result_of_all_stories.
100    ///
101    /// # Ok(())
102    /// # }
103    /// ```
104    pub fn get_user_stories<'a>(
105        &'a self,
106        username: &'a str,
107        fields: Option<&'a [StoryField]>,
108    ) -> Result<Paginator<'a, StoryResponse, UserStoriesResponse>, WattpadError> {
109
110        let req = WattpadRequestBuilder::new(
111            &self.http,
112            &self.is_authenticated,
113            reqwest::Method::GET,
114            &format!("/api/v3/users/{}/stories", username),
115        )
116            .fields(fields, Some("stories"))?;
117
118        Ok(Paginator::new(req))
119    }
120}