moosicbox_music_api_api 0.3.0

MoosicBox "Music API" API package
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! HTTP endpoint handlers for music API operations.
//!
//! This module provides Actix-Web route handlers for managing music service providers,
//! including listing APIs, authentication, library scanning, and search functionality.
//! All endpoints are profile-aware and operate within the context of a `MoosicBox` profile.

use std::collections::BTreeMap;

use actix_web::{
    Result, Scope,
    dev::{ServiceFactory, ServiceRequest},
    error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound},
    route,
    web::{self, Json},
};
use moosicbox_music_api::{
    MusicApis, SourceToMusicApi as _, models::search::api::ApiSearchResultsResponse,
};
use moosicbox_music_models::ApiSource;
use moosicbox_paging::Page;
use moosicbox_profiles::api::ProfileName;
use serde::Deserialize;

use crate::models::{ApiMusicApi, AuthValues, convert_to_api_music_api};

/// Binds music API HTTP endpoints to an Actix-Web scope.
///
/// Registers all music API-related endpoints including listing APIs,
/// authentication, scanning, and search functionality.
///
/// # Examples
///
/// ```rust,ignore
/// use actix_web::web;
/// use moosicbox_music_api_api::api::bind_services;
///
/// let scope = web::scope("/music-apis");
/// let scope = bind_services(scope);
/// # let _ = scope;
/// ```
#[must_use]
pub fn bind_services<
    T: ServiceFactory<ServiceRequest, Config = (), Error = actix_web::Error, InitError = ()>,
>(
    scope: Scope<T>,
) -> Scope<T> {
    scope
        .service(music_apis_endpoint)
        .service(auth_music_api_endpoint)
        .service(scan_music_api_endpoint)
        .service(enable_scan_origin_music_api_endpoint)
        .service(search_music_apis_endpoint)
}

/// `OpenAPI` specification for music API endpoints.
///
/// Provides `OpenAPI`/Swagger documentation for all music API-related endpoints.
#[cfg(feature = "openapi")]
#[derive(utoipa::OpenApi)]
#[openapi(
    tags((name = "MusicApi")),
    paths(
        music_apis_endpoint,
        auth_music_api_endpoint,
        scan_music_api_endpoint,
        enable_scan_origin_music_api_endpoint,
        search_music_apis_endpoint,
    ),
    components(schemas(
        ApiMusicApi,
    ))
)]
pub struct Api;

/// Query parameters for retrieving music APIs.
///
/// Used to paginate the list of enabled music APIs for a profile.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetMusicApis {
    /// Starting offset for pagination (default: 0)
    offset: Option<u32>,
    /// Maximum number of items to return (default: 30)
    limit: Option<u32>,
}

#[cfg_attr(
    feature = "openapi", utoipa::path(
        tags = ["MusicApi"],
        get,
        path = "",
        description = "Get the list enabled music APIs",
        params(
            ("moosicbox-profile" = String, Header, description = "MoosicBox profile"),
            ("offset" = Option<u32>, Query, description = "Page offset"),
            ("limit" = Option<u32>, Query, description = "Page limit"),
        ),
        responses(
            (
                status = 200,
                description = "A paginated response of tracks for the music APIs",
                body = Value,
            )
        )
    )
)]
#[route("", method = "GET")]
/// Retrieves a paginated list of enabled music APIs for the current profile.
///
/// # Errors
///
/// * Returns a 404 error if the specified profile is not found
/// * Returns a 500 error if querying music API state fails
///
/// # Panics
///
/// * Panics if the number of music APIs exceeds `u32::MAX` (practically impossible)
///
/// # Examples
///
/// ```text
/// GET /music-apis?offset=0&limit=30
/// moosicbox-profile: default
/// ```
pub async fn music_apis_endpoint(
    query: web::Query<GetMusicApis>,
    profile_name: ProfileName,
) -> Result<Json<Page<ApiMusicApi>>> {
    let profile_name: String = profile_name.into();
    let offset = query.offset.unwrap_or(0);
    let limit = query.limit.unwrap_or(30);
    let music_apis = moosicbox_music_api::profiles::PROFILES
        .get(&profile_name)
        .ok_or_else(|| ErrorNotFound(format!("Missing profile '{profile_name}'")))?;
    let music_apis = music_apis.iter().collect::<Vec<_>>();
    let total = u32::try_from(music_apis.len()).unwrap();
    let music_apis = futures::future::join_all(
        music_apis
            .into_iter()
            .skip(offset as usize)
            .take(limit as usize)
            .map(convert_to_api_music_api),
    )
    .await;
    let music_apis = music_apis
        .into_iter()
        .collect::<Result<Vec<_>, _>>()
        .map_err(ErrorInternalServerError)?;

    Ok(Json(Page::WithTotal {
        items: music_apis,
        offset,
        limit,
        total,
    }))
}

/// Query parameters for authenticating a music API.
///
/// Specifies which music API source to authenticate with.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthMusicApi {
    /// The music API source to authenticate
    api_source: ApiSource,
}

#[cfg_attr(
    feature = "openapi", utoipa::path(
        tags = ["MusicApi"],
        post,
        path = "/auth",
        description = "Authenticate a specific MusicApi",
        params(
            ("moosicbox-profile" = String, Header, description = "MoosicBox profile"),
            ("apiSource" = ApiSource, Query, description = "ApiSource to authenticate"),
        ),
        responses(
            (
                status = 200,
                description = "The updated state for the MusicApi that was authenticated",
                body = ApiMusicApi,
            )
        )
    )
)]
#[route("/auth", method = "POST")]
#[cfg_attr(not(feature = "_auth"), allow(unreachable_code, unused))]
/// Authenticates with a specific music API using the provided credentials.
///
/// Supports different authentication methods (username/password or OAuth polling)
/// depending on the API's capabilities.
///
/// # Errors
///
/// * Returns a 404 error if the specified music API is not found
/// * Returns a 400 error if the authentication method is not supported
/// * Returns a 500 error if the authentication process fails
///
/// # Examples
///
/// ```text
/// POST /music-apis/auth?apiSource=QOBUZ
/// moosicbox-profile: default
/// Content-Type: application/x-www-form-urlencoded
///
/// type=username-password&username=alice&password=secret
/// ```
pub async fn auth_music_api_endpoint(
    query: web::Query<AuthMusicApi>,
    form: web::Form<AuthValues>,
    music_apis: MusicApis,
) -> Result<Json<ApiMusicApi>> {
    let music_api = music_apis
        .get(&query.api_source)
        .ok_or_else(|| ErrorNotFound(format!("MusicApi '{}' not found", query.api_source)))?;

    match form.0 {
        #[cfg_attr(not(feature = "auth-username-password"), allow(unused_variables))]
        AuthValues::UsernamePassword { username, password } => {
            #[cfg(not(feature = "auth-username-password"))]
            return Err(ErrorBadRequest("Auth not supported"));

            #[cfg(feature = "auth-username-password")]
            if let Some(auth) = music_api.auth() {
                let user_pass_auth = auth
                    .clone()
                    .into_username_password()
                    .ok_or_else(|| ErrorBadRequest("Auth not supported"))?;

                auth.attempt_login(move |_| {
                    let user_pass_auth = user_pass_auth.clone();
                    let username = username.clone();
                    let password = password.clone();
                    async move { user_pass_auth.login(username, password).await }
                })
                .await
                .map_err(ErrorInternalServerError)?;
            }
        }
        AuthValues::Poll => {
            #[cfg(not(feature = "auth-poll"))]
            return Err(ErrorBadRequest("Auth not supported"));

            #[cfg(feature = "auth-poll")]
            if let Some(auth) = music_api.auth() {
                let poll_auth = auth
                    .clone()
                    .into_poll()
                    .ok_or_else(|| ErrorBadRequest("Auth not supported"))?;

                auth.attempt_login(move |_| {
                    let poll_auth = poll_auth.clone();
                    async move { poll_auth.login().await }
                })
                .await
                .map_err(ErrorInternalServerError)?;
            }
        }
    }

    Ok(Json(
        convert_to_api_music_api(&**music_api)
            .await
            .map_err(ErrorInternalServerError)?,
    ))
}

/// Query parameters for scanning a music API.
///
/// Specifies which music API source to scan.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScanMusicApi {
    /// The music API source to scan
    api_source: ApiSource,
}

#[cfg_attr(
    feature = "openapi", utoipa::path(
        tags = ["MusicApi"],
        post,
        path = "/scan",
        description = "Scan a specific MusicApi",
        params(
            ("moosicbox-profile" = String, Header, description = "MoosicBox profile"),
            ("apiSource" = ApiSource, Query, description = "ApiSource to scan"),
        ),
        responses(
            (
                status = 200,
                description = "The updated state for the MusicApi that was scanned",
                body = ApiMusicApi,
            )
        )
    )
)]
#[route("/scan", method = "POST")]
/// Initiates a library scan for a specific music API.
///
/// This triggers the music API to scan and index its available content.
///
/// # Errors
///
/// * Returns a 404 error if the specified music API is not found
/// * Returns a 500 error if the scan operation fails
///
/// # Examples
///
/// ```text
/// POST /music-apis/scan?apiSource=TIDAL
/// moosicbox-profile: default
/// ```
pub async fn scan_music_api_endpoint(
    query: web::Query<ScanMusicApi>,
    music_apis: MusicApis,
) -> Result<Json<ApiMusicApi>> {
    let music_api = music_apis
        .get(&query.api_source)
        .ok_or_else(|| ErrorNotFound(format!("MusicApi '{}' not found", query.api_source)))?;

    music_api.scan().await.map_err(ErrorInternalServerError)?;

    Ok(Json(
        convert_to_api_music_api(&**music_api)
            .await
            .map_err(ErrorInternalServerError)?,
    ))
}

/// Query parameters for enabling scan for a music API.
///
/// Specifies which music API source to enable for library scanning.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EnableScanMusicApi {
    /// The music API source to enable for scanning
    api_source: ApiSource,
}

#[cfg_attr(
    feature = "openapi", utoipa::path(
        tags = ["MusicApi"],
        post,
        path = "/scan-origins",
        description = "Enable a specific MusicApi scan origin",
        params(
            ("moosicbox-profile" = String, Header, description = "MoosicBox profile"),
            ("apiSource" = ApiSource, Query, description = "ApiSource to scan"),
        ),
        responses(
            (
                status = 200,
                description = "The updated state for the MusicApi that was enabled",
                body = ApiMusicApi,
            )
        )
    )
)]
#[route("/scan-origins", method = "POST")]
/// Enables library scanning for a specific music API.
///
/// This configures the music API to be included in library scan operations.
///
/// # Errors
///
/// * Returns a 404 error if the specified music API is not found
/// * Returns a 500 error if enabling the scan fails
///
/// # Examples
///
/// ```text
/// POST /music-apis/scan-origins?apiSource=QOBUZ
/// moosicbox-profile: default
/// ```
pub async fn enable_scan_origin_music_api_endpoint(
    query: web::Query<EnableScanMusicApi>,
    music_apis: MusicApis,
) -> Result<Json<ApiMusicApi>> {
    let music_api = music_apis
        .get(&query.api_source)
        .ok_or_else(|| ErrorNotFound(format!("MusicApi '{}' not found", query.api_source)))?;

    music_api
        .enable_scan()
        .await
        .map_err(ErrorInternalServerError)?;

    Ok(Json(
        convert_to_api_music_api(&**music_api)
            .await
            .map_err(ErrorInternalServerError)?,
    ))
}

/// Query parameters for searching music APIs.
///
/// Specifies the search query, optional API source filters, and pagination options.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchMusicApis {
    /// The search query string
    query: String,
    /// Comma-separated list of API sources to search (searches all if not specified)
    api_source: Option<String>,
    /// Starting offset for pagination
    offset: Option<u32>,
    /// Maximum number of results to return
    limit: Option<u32>,
}

#[cfg_attr(
    feature = "openapi", utoipa::path(
        tags = ["MusicApi"],
        get,
        path = "/search",
        description = "Search the music APIs",
        params(
            ("moosicbox-profile" = String, Header, description = "MoosicBox profile"),
            ("query" = String, Query, description = "The search query"),
            ("apiSource" = Option<String>, Query, description = "The ApiSource(s) to search"),
            ("offset" = Option<u32>, Query, description = "Page offset"),
            ("limit" = Option<u32>, Query, description = "Page limit"),
        ),
        responses(
            (
                status = 200,
                description = "A paginated response of search results for the music APIs",
                body = Value,
            )
        )
    )
)]
#[route("/search", method = "GET")]
/// Searches across one or more music APIs for content matching the query.
///
/// Results are returned grouped by API source. If no API sources are specified,
/// all enabled APIs that support search will be queried.
///
/// # Errors
///
/// * Returns a 404 error if the specified profile is not found
/// * Returns a 400 error if invalid API sources are provided
/// * Returns a 500 error if any search operation fails
///
/// # Examples
///
/// ```text
/// GET /music-apis/search?query=radiohead&apiSource=QOBUZ,TIDAL&offset=0&limit=20
/// moosicbox-profile: default
/// ```
pub async fn search_music_apis_endpoint(
    query: web::Query<SearchMusicApis>,
    profile_name: ProfileName,
) -> Result<Json<BTreeMap<ApiSource, ApiSearchResultsResponse>>> {
    let api_sources = query
        .api_source
        .as_ref()
        .map(|x| {
            x.split(',')
                .map(std::convert::TryInto::try_into)
                .collect::<Result<Vec<ApiSource>, _>>()
                .map_err(|e| ErrorBadRequest(format!("Invalid apiSource: {e:?}")))
        })
        .transpose()?;
    let profile_name: String = profile_name.into();
    let music_apis = moosicbox_music_api::profiles::PROFILES
        .get(&profile_name)
        .ok_or_else(|| ErrorNotFound(format!("Missing profile '{profile_name}'")))?;
    let music_apis = music_apis
        .iter()
        .filter(|x| x.supports_search())
        .filter(|x| {
            api_sources
                .as_ref()
                .is_none_or(|sources| sources.contains(x.source()))
        })
        .collect::<Vec<_>>();

    let search_results = music_apis
        .iter()
        .map(|x| x.search(&query.query, query.offset, query.limit));
    let search_results = futures::future::join_all(search_results).await;
    let search_results = search_results
        .into_iter()
        .collect::<Result<Vec<_>, _>>()
        .map_err(ErrorInternalServerError)?;

    let search_results = music_apis
        .into_iter()
        .map(|x| x.source().clone())
        .zip(search_results.into_iter())
        .collect::<BTreeMap<_, _>>();

    Ok(Json(search_results))
}