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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::{
api::service::QobuzApiService,
errors::QobuzApiError,
models::{Login, QobuzApiStatusResponse},
};
impl QobuzApiService {
/// Internal helper to update the user authentication token in the service instance.
///
/// This method extracts the authentication token from the login response and stores it
/// in the service for use in subsequent API requests that require authentication.
///
/// # Arguments
///
/// * `result` - A reference to the login response containing the authentication token
///
/// # Returns
///
/// A clone of the original login response
fn update_auth_token(&mut self, result: &Login) -> Login {
// Update the user auth token in the service
if let Some(auth_token) = &result.auth_token {
self.user_auth_token = Some(auth_token.clone());
}
result.clone()
}
/// Authenticates a user with the Qobuz API using their identifier and password.
///
/// This method performs a login request to the Qobuz API using either an email address
/// or username as the identifier. The password must be provided as an MD5 hash.
/// On successful login, the user authentication token is automatically stored in the
/// service instance for use in subsequent authenticated API requests.
///
/// # Arguments
///
/// * `identifier` - The user's identifier, which can be either an email address or username
/// * `password` - The MD5 hash of the user's password
///
/// # Returns
///
/// * `Ok(Login)` - A login response containing user information and authentication token
/// * `Err(QobuzApiError)` - If the API request fails or authentication is unsuccessful
///
/// # Example
///
/// ```no_run
/// # use qobuz_api_rust::{QobuzApiService, QobuzApiError};
/// # #[tokio::main]
/// # async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
/// let mut api = QobuzApiService::new().await?;
/// // Note: Password should be MD5 hashed
/// let login_result = api.login("user@example.com", "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8").await?;
/// # Ok(())
/// # }
/// ```
pub async fn login(
&mut self,
identifier: &str,
password: &str,
) -> Result<Login, QobuzApiError> {
let params = vec![
("username".to_string(), identifier.to_string()), // Qobuz API uses "username" field for both email and username
("password".to_string(), password.to_string()),
];
let result: Login = self.post("/user/login", ¶ms).await?;
let result = self.update_auth_token(&result);
Ok(result)
}
/// Authenticates a user with the Qobuz API using their user ID and authentication token.
///
/// This method allows authentication using an existing user ID and authentication token,
/// which can be useful for maintaining sessions across application restarts.
/// On successful login, the authentication token is stored in the service instance
/// for use in subsequent API requests that require authentication.
///
/// # Arguments
///
/// * `user_id` - The user's unique identifier in the Qobuz system
/// * `user_auth_token` - The user's authentication token
///
/// # Returns
///
/// * `Ok(Login)` - A login response containing user information and authentication token
/// * `Err(QobuzApiError)` - If the API request fails or authentication is unsuccessful
///
/// # Example
///
/// ```no_run
/// # use qobuz_api_rust::{QobuzApiService, QobuzApiError};
/// # #[tokio::main]
/// # async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
/// let mut api = QobuzApiService::new().await?;
/// let login_result = api.login_with_token("123456789", "auth_token_here").await?;
/// # Ok(())
/// # }
/// ```
pub async fn login_with_token(
&mut self,
user_id: &str,
user_auth_token: &str,
) -> Result<Login, QobuzApiError> {
let params = vec![
("user_id".to_string(), user_id.to_string()),
("user_auth_token".to_string(), user_auth_token.to_string()),
];
let result: Login = self.post("/user/login", ¶ms).await?;
let result = self.update_auth_token(&result);
Ok(result)
}
/// Requests a password reset link for the specified user identifier.
///
/// This method sends a password reset request to the Qobuz API for the given identifier,
/// which can be either an email address or username. If the identifier exists in the system,
/// the user will receive instructions to reset their password.
///
/// # Arguments
///
/// * `identifier` - The user's identifier (email address or username) for which to request a password reset
///
/// # Returns
///
/// * `Ok(QobuzApiStatusResponse)` - A response indicating whether the password reset request was successful
/// * `Err(QobuzApiError)` - If the API request fails
///
/// # Example
///
/// ```no_run
/// # use qobuz_api_rust::{QobuzApiService, QobuzApiError};
/// # #[tokio::main]
/// # async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
/// let api = QobuzApiService::new().await?;
/// let result = api.reset_password("user@example.com").await?;
/// if result.status == Some("success".to_string()) {
/// println!("Password reset email sent successfully");
/// }
/// # Ok(())
/// # }
/// ```
pub async fn reset_password(
&self,
identifier: &str,
) -> Result<QobuzApiStatusResponse, QobuzApiError> {
let params = vec![("username".to_string(), identifier.to_string())];
let result: QobuzApiStatusResponse = self.get("/user/resetPassword", ¶ms).await?;
Ok(result)
}
}