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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use {
serde::de::DeserializeOwned,
serde_json::{Value, from_value},
};
use crate::{
api::service::constants,
errors::QobuzApiError::{self, ApiErrorResponse, ApiResponseParseError, HttpError},
models::QobuzApiStatusResponse,
utils::{deserialize_response, get_current_timestamp, get_md5_hash, to_query_string},
};
impl crate::api::service::QobuzApiService {
/// Sends a GET request to the Qobuz API.
///
/// This method handles the complete request lifecycle including parameter formatting,
/// authentication token injection, response parsing, and error handling. It automatically
/// includes common parameters and checks for API error responses.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call (e.g., "/album/get")
/// * `params` - A slice of key-value parameter pairs to include in the query string
///
/// # Type Parameters
///
/// * `T` - The expected response type that must implement `DeserializeOwned`
///
/// # Returns
///
/// Returns `Ok(T)` with the deserialized response data if the request is successful,
/// or `Err(QobuzApiError)` if the request fails due to network issues, API errors,
/// or response parsing problems.
///
/// # Examples
///
/// ```rust
/// # use qobuz_api_rust::api::service::QobuzApiService;
/// # use qobuz_api_rust::models::Album;
/// # async fn example(service: &QobuzApiService) -> Result<(), Box<dyn std::error::Error>> {
/// let album_id = "12345";
/// let params = vec![("album_id".to_string(), album_id.to_string())];
/// let album: Album = service.get("/album/get", ¶ms).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// This function will return an error if:
/// - The HTTP request fails (network issues)
/// - The API returns an error response (status: "error")
/// - The response cannot be parsed as the expected type `T`
/// - The response cannot be deserialized from JSON
pub async fn get<T>(
&self,
endpoint: &str,
params: &[(String, String)],
) -> Result<T, QobuzApiError>
where
T: DeserializeOwned,
{
// Add common parameters
let all_params = params.to_vec();
let query_string = to_query_string(&all_params);
let url = format!("{}{}?{}", constants::API_BASE_URL, endpoint, query_string);
let mut request = self.client.get(&url);
if let Some(ref token) = self.user_auth_token {
request = request.header("X-User-Auth-Token", token);
}
let response = request.send().await.map_err(HttpError)?;
let value: Value = deserialize_response(response).await?;
if let Some(status) = value.get("status")
&& status == "error"
{
let error_response: QobuzApiStatusResponse =
from_value(value.clone()).map_err(|e| ApiResponseParseError {
content: e.to_string(),
source: e,
})?;
return Err(ApiErrorResponse {
code: error_response.code.unwrap_or_default(),
message: error_response.message.unwrap_or_default(),
status: error_response.status.unwrap_or_default(),
});
}
from_value(value).map_err(|e| ApiResponseParseError {
content: e.to_string(),
source: e,
})
}
/// Sends a POST request to the Qobuz API.
///
/// This method handles POST requests to the Qobuz API, automatically including
/// required parameters like the application ID and user authentication token if available.
/// It manages the complete request lifecycle including parameter formatting,
/// response parsing, and error handling.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call (e.g., "/user/login")
/// * `params` - A slice of key-value parameter pairs to include in the form body
///
/// # Type Parameters
///
/// * `T` - The expected response type that must implement `DeserializeOwned`
///
/// # Returns
///
/// Returns `Ok(T)` with the deserialized response data if the request is successful,
/// or `Err(QobuzApiError)` if the request fails due to network issues, API errors,
/// or response parsing problems.
///
/// # Examples
///
/// ```rust
/// # use qobuz_api_rust::api::service::QobuzApiService;
/// # use qobuz_api_rust::models::Login;
/// # async fn example(service: &QobuzApiService) -> Result<(), Box<dyn std::error::Error>> {
/// let params = vec![
/// ("email".to_string(), "user@example.com".to_string()),
/// ("password".to_string(), "hashed_password".to_string()),
/// ];
/// let login_response: Login = service.post("/user/login", ¶ms).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// This function will return an error if:
/// - The HTTP request fails (network issues)
/// - The API returns an error response (status: "error")
/// - The response cannot be parsed as the expected type `T`
/// - The response cannot be deserialized from JSON
pub async fn post<T>(
&self,
endpoint: &str,
params: &[(String, String)],
) -> Result<T, QobuzApiError>
where
T: DeserializeOwned,
{
// Add common parameters
let mut all_params = params.to_vec();
all_params.push(("app_id".to_string(), self.app_id.clone()));
if let Some(ref token) = self.user_auth_token {
all_params.push(("user_auth_token".to_string(), token.clone()));
}
let url = format!("{}{}", constants::API_BASE_URL, endpoint);
let response = self
.client
.post(&url)
.form(&all_params)
.send()
.await
.map_err(HttpError)?;
let value: Value = deserialize_response(response).await?;
if let Some(status) = value.get("status")
&& status == "error"
{
let error_response: QobuzApiStatusResponse =
from_value(value).map_err(|e| ApiResponseParseError {
content: e.to_string(),
source: e,
})?;
return Err(ApiErrorResponse {
code: error_response.code.unwrap_or_default(),
message: error_response.message.unwrap_or_default(),
status: error_response.status.unwrap_or_default(),
});
}
from_value(value).map_err(|e| ApiResponseParseError {
content: e.to_string(),
source: e,
})
}
/// Generates a signature for protected Qobuz API endpoints.
///
/// This method creates a signature string using the Qobuz API's authentication scheme.
/// The signature is computed by concatenating the HTTP method, endpoint, sorted parameters,
/// and application secret, then applying an MD5 hash. This ensures the request is
/// authenticated and authorized.
///
/// # Arguments
///
/// * `method` - The HTTP method (e.g., "GET", "POST")
/// * `endpoint` - The API endpoint to call (e.g., "/album/get")
/// * `params` - A slice of key-value parameter pairs to include in the signature calculation
///
/// # Returns
///
/// Returns a string containing the MD5 hash of the signature string.
///
/// # Algorithm
///
/// The signature is generated by:
/// 1. Adding common parameters (app_id, method, timestamp, user_auth_token if present)
/// 2. Sorting parameters alphabetically by key
/// 3. Creating a signature string by concatenating method, endpoint, sorted parameters, and app secret
/// 4. Computing the MD5 hash of the signature string
fn generate_signature(
&self,
method: &str,
endpoint: &str,
params: &[(String, String)],
) -> String {
let timestamp = get_current_timestamp();
let mut all_params = params.to_vec();
all_params.push(("app_id".to_string(), self.app_id.clone()));
all_params.push(("method".to_string(), method.to_string()));
all_params.push(("timestamp".to_string(), timestamp.clone()));
if let Some(ref token) = self.user_auth_token {
all_params.push(("user_auth_token".to_string(), token.clone()));
}
// Sort parameters alphabetically by key
all_params.sort_by(|a, b| a.0.cmp(&b.0));
// Create the signature string
let mut signature_string = format!("{}{}", method, endpoint);
for (key, value) in &all_params {
signature_string.push_str(&format!("{}{}", key, value));
}
signature_string.push_str(&self.app_secret);
get_md5_hash(&signature_string)
}
/// Sends a GET request to the Qobuz API with signature authentication.
///
/// This method is used for protected endpoints that require signature-based authentication.
/// It automatically generates the required signature using the Qobuz API's authentication
/// scheme, includes the necessary parameters (app_id, user_auth_token if available),
/// and handles the complete request lifecycle including response parsing and error handling.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint to call (e.g., "/album/get")
/// * `params` - A slice of key-value parameter pairs to include in the query string
///
/// # Type Parameters
///
/// * `T` - The expected response type that must implement `DeserializeOwned`
///
/// # Returns
///
/// Returns `Ok(T)` with the deserialized response data if the request is successful,
/// or `Err(QobuzApiError)` if the request fails due to network issues, API errors,
/// or response parsing problems.
///
/// # Examples
///
/// ```rust
/// # use qobuz_api_rust::api::service::QobuzApiService;
/// # use qobuz_api_rust::models::Album;
/// # async fn example(service: &QobuzApiService) -> Result<(), Box<dyn std::error::Error>> {
/// let album_id = "12345";
/// let params = vec![("album_id".to_string(), album_id.to_string())];
/// let album: Album = service.signed_get("/album/get", ¶ms).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// This function will return an error if:
/// - The HTTP request fails (network issues)
/// - The API returns an error response (status: "error")
/// - The response cannot be parsed as the expected type `T`
/// - The response cannot be deserialized from JSON
pub async fn signed_get<T>(
&self,
endpoint: &str,
params: &[(String, String)],
) -> Result<T, QobuzApiError>
where
T: DeserializeOwned,
{
// Add common parameters
let mut all_params = params.to_vec();
all_params.push(("app_id".to_string(), self.app_id.clone()));
if let Some(ref token) = self.user_auth_token {
all_params.push(("user_auth_token".to_string(), token.clone()));
}
// Generate signature
let signature = self.generate_signature("GET", endpoint, params);
all_params.push(("request_ts".to_string(), get_current_timestamp()));
all_params.push(("request_sig".to_string(), signature));
let query_string = to_query_string(&all_params);
let url = format!("{}{}?{}", constants::API_BASE_URL, endpoint, query_string);
let response = self.client.get(&url).send().await.map_err(HttpError)?;
let value: Value = deserialize_response(response).await?;
if let Some(status) = value.get("status")
&& status == "error"
{
let error_response: QobuzApiStatusResponse =
from_value(value).map_err(|e| ApiResponseParseError {
content: e.to_string(),
source: e,
})?;
return Err(ApiErrorResponse {
code: error_response.code.unwrap_or_default(),
message: error_response.message.unwrap_or_default(),
status: error_response.status.unwrap_or_default(),
});
}
from_value(value).map_err(|e| ApiResponseParseError {
content: e.to_string(),
source: e,
})
}
}