qobuz_api_rust/api/auth.rs
1use crate::{
2 api::service::QobuzApiService,
3 errors::QobuzApiError,
4 models::{Login, QobuzApiStatusResponse},
5};
6
7impl QobuzApiService {
8 /// Internal helper to update the user authentication token in the service instance.
9 ///
10 /// This method extracts the authentication token from the login response and stores it
11 /// in the service for use in subsequent API requests that require authentication.
12 ///
13 /// # Arguments
14 ///
15 /// * `result` - A reference to the login response containing the authentication token
16 ///
17 /// # Returns
18 ///
19 /// A clone of the original login response
20 fn update_auth_token(&mut self, result: &Login) -> Login {
21 // Update the user auth token in the service
22 if let Some(auth_token) = &result.auth_token {
23 self.user_auth_token = Some(auth_token.clone());
24 }
25 result.clone()
26 }
27
28 /// Authenticates a user with the Qobuz API using their identifier and password.
29 ///
30 /// This method performs a login request to the Qobuz API using either an email address
31 /// or username as the identifier. The password must be provided as an MD5 hash.
32 /// On successful login, the user authentication token is automatically stored in the
33 /// service instance for use in subsequent authenticated API requests.
34 ///
35 /// # Arguments
36 ///
37 /// * `identifier` - The user's identifier, which can be either an email address or username
38 /// * `password` - The MD5 hash of the user's password
39 ///
40 /// # Returns
41 ///
42 /// * `Ok(Login)` - A login response containing user information and authentication token
43 /// * `Err(QobuzApiError)` - If the API request fails or authentication is unsuccessful
44 ///
45 /// # Example
46 ///
47 /// ```no_run
48 /// # use qobuz_api_rust::{QobuzApiService, QobuzApiError};
49 /// # #[tokio::main]
50 /// # async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
51 /// let mut api = QobuzApiService::new().await?;
52 /// // Note: Password should be MD5 hashed
53 /// let login_result = api.login("user@example.com", "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8").await?;
54 /// # Ok(())
55 /// # }
56 /// ```
57 pub async fn login(
58 &mut self,
59 identifier: &str,
60 password: &str,
61 ) -> Result<Login, QobuzApiError> {
62 let params = vec![
63 ("username".to_string(), identifier.to_string()), // Qobuz API uses "username" field for both email and username
64 ("password".to_string(), password.to_string()),
65 ];
66
67 let result: Login = self.post("/user/login", ¶ms).await?;
68 let result = self.update_auth_token(&result);
69
70 Ok(result)
71 }
72
73 /// Authenticates a user with the Qobuz API using their user ID and authentication token.
74 ///
75 /// This method allows authentication using an existing user ID and authentication token,
76 /// which can be useful for maintaining sessions across application restarts.
77 /// On successful login, the authentication token is stored in the service instance
78 /// for use in subsequent API requests that require authentication.
79 ///
80 /// # Arguments
81 ///
82 /// * `user_id` - The user's unique identifier in the Qobuz system
83 /// * `user_auth_token` - The user's authentication token
84 ///
85 /// # Returns
86 ///
87 /// * `Ok(Login)` - A login response containing user information and authentication token
88 /// * `Err(QobuzApiError)` - If the API request fails or authentication is unsuccessful
89 ///
90 /// # Example
91 ///
92 /// ```no_run
93 /// # use qobuz_api_rust::{QobuzApiService, QobuzApiError};
94 /// # #[tokio::main]
95 /// # async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
96 /// let mut api = QobuzApiService::new().await?;
97 /// let login_result = api.login_with_token("123456789", "auth_token_here").await?;
98 /// # Ok(())
99 /// # }
100 /// ```
101 pub async fn login_with_token(
102 &mut self,
103 user_id: &str,
104 user_auth_token: &str,
105 ) -> Result<Login, QobuzApiError> {
106 let params = vec![
107 ("user_id".to_string(), user_id.to_string()),
108 ("user_auth_token".to_string(), user_auth_token.to_string()),
109 ];
110
111 let result: Login = self.post("/user/login", ¶ms).await?;
112 let result = self.update_auth_token(&result);
113
114 Ok(result)
115 }
116
117 /// Requests a password reset link for the specified user identifier.
118 ///
119 /// This method sends a password reset request to the Qobuz API for the given identifier,
120 /// which can be either an email address or username. If the identifier exists in the system,
121 /// the user will receive instructions to reset their password.
122 ///
123 /// # Arguments
124 ///
125 /// * `identifier` - The user's identifier (email address or username) for which to request a password reset
126 ///
127 /// # Returns
128 ///
129 /// * `Ok(QobuzApiStatusResponse)` - A response indicating whether the password reset request was successful
130 /// * `Err(QobuzApiError)` - If the API request fails
131 ///
132 /// # Example
133 ///
134 /// ```no_run
135 /// # use qobuz_api_rust::{QobuzApiService, QobuzApiError};
136 /// # #[tokio::main]
137 /// # async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
138 /// let api = QobuzApiService::new().await?;
139 /// let result = api.reset_password("user@example.com").await?;
140 /// if result.status == Some("success".to_string()) {
141 /// println!("Password reset email sent successfully");
142 /// }
143 /// # Ok(())
144 /// # }
145 /// ```
146 pub async fn reset_password(
147 &self,
148 identifier: &str,
149 ) -> Result<QobuzApiStatusResponse, QobuzApiError> {
150 let params = vec![("username".to_string(), identifier.to_string())];
151
152 let result: QobuzApiStatusResponse = self.get("/user/resetPassword", ¶ms).await?;
153 Ok(result)
154 }
155}