moosicbox_music_api 0.2.0

MoosicBox music API package
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
//! Profile-based music API registry.
//!
//! This module provides [`MusicApisProfiles`], a global registry for managing collections
//! of music APIs associated with different user profiles. Each profile can have its own
//! set of configured music API sources.

#![allow(clippy::type_complexity)]

use std::{
    collections::BTreeMap,
    sync::{Arc, LazyLock, RwLock},
};

use moosicbox_music_models::ApiSource;

use crate::{MusicApi, MusicApis};

/// Global registry of music API collections by profile.
pub static PROFILES: LazyLock<MusicApisProfiles> = LazyLock::new(MusicApisProfiles::default);

/// Registry for managing music API collections associated with profiles.
#[allow(clippy::module_name_repetitions)]
#[derive(Default)]
pub struct MusicApisProfiles {
    profiles: Arc<RwLock<BTreeMap<String, MusicApis>>>,
}

impl MusicApisProfiles {
    /// Adds a music API collection for the specified profile.
    ///
    /// # Panics
    ///
    /// * If the `RwLock` is poisoned
    pub fn add(
        &self,
        profile: String,
        music_apis: Arc<BTreeMap<ApiSource, Arc<Box<dyn MusicApi>>>>,
    ) {
        moosicbox_profiles::PROFILES.add(profile.clone());
        self.profiles
            .write()
            .unwrap()
            .insert(profile, MusicApis(music_apis));
    }

    /// Inserts or updates a music API collection for the specified profile.
    ///
    /// # Panics
    ///
    /// * If the `RwLock` is poisoned
    pub fn upsert(
        &self,
        profile: String,
        music_apis: Arc<BTreeMap<ApiSource, Arc<Box<dyn MusicApi>>>>,
    ) {
        let mut profiles = self.profiles.write().unwrap();

        if let Some(existing) = profiles.iter_mut().find(|(p, _)| *p == &profile) {
            *existing.1 = MusicApis(music_apis);
        } else {
            profiles.insert(profile, MusicApis(music_apis));
        }
    }

    /// Removes the music API collection for the specified profile.
    ///
    /// # Panics
    ///
    /// * If the `RwLock` is poisoned
    pub fn remove(&self, profile: &str) {
        self.profiles.write().unwrap().remove(profile);
    }

    /// Adds a music API collection and returns it.
    ///
    /// # Panics
    ///
    /// * If the `RwLock` is poisoned
    /// * If the profile was not added successfully
    #[must_use]
    pub fn add_fetch(
        &self,
        profile: &str,
        music_apis: Arc<BTreeMap<ApiSource, Arc<Box<dyn MusicApi>>>>,
    ) -> MusicApis {
        self.add(profile.to_owned(), music_apis);
        self.get(profile).unwrap()
    }

    /// Retrieves the music API collection for the specified profile.
    ///
    /// # Panics
    ///
    /// * If the `RwLock` is poisoned
    #[must_use]
    pub fn get(&self, profile: &str) -> Option<MusicApis> {
        self.profiles.read().unwrap().iter().find_map(|(p, api)| {
            if p == profile {
                Some(api.clone())
            } else {
                None
            }
        })
    }

    /// Returns the names of all registered profiles.
    ///
    /// # Panics
    ///
    /// * If the `RwLock` is poisoned
    #[must_use]
    pub fn names(&self) -> Vec<String> {
        self.profiles.read().unwrap().keys().cloned().collect()
    }
}

#[cfg(test)]
mod test {
    use std::{collections::BTreeMap, sync::Arc};

    use moosicbox_music_api_models::{
        AlbumOrder, AlbumOrderDirection, AlbumsRequest, ArtistOrder, ArtistOrderDirection,
        TrackAudioQuality, TrackOrder, TrackOrderDirection, TrackSource,
    };
    use moosicbox_music_models::{
        Album, AlbumType, ApiSource, Artist, PlaybackQuality, Track, id::Id,
    };
    use moosicbox_paging::{PagingResponse, PagingResult};

    use crate::{Error, MusicApi, SourceToMusicApi};

    use super::MusicApisProfiles;

    pub struct TestMusicApi {
        source: ApiSource,
    }

    impl TestMusicApi {
        fn new(source: ApiSource) -> Self {
            Self { source }
        }
    }

    #[async_trait::async_trait]
    impl MusicApi for TestMusicApi {
        fn source(&self) -> &ApiSource {
            &self.source
        }

        async fn artists(
            &self,
            _offset: Option<u32>,
            _limit: Option<u32>,
            _order: Option<ArtistOrder>,
            _order_direction: Option<ArtistOrderDirection>,
        ) -> PagingResult<Artist, Error> {
            Ok(PagingResponse::empty())
        }

        async fn artist(&self, _artist_id: &Id) -> Result<Option<Artist>, Error> {
            Ok(None)
        }

        async fn add_artist(&self, _artist_id: &Id) -> Result<(), Error> {
            Ok(())
        }

        async fn remove_artist(&self, _artist_id: &Id) -> Result<(), Error> {
            Ok(())
        }

        async fn albums(&self, _request: &AlbumsRequest) -> PagingResult<Album, Error> {
            Ok(PagingResponse::empty())
        }

        async fn album(&self, _album_id: &Id) -> Result<Option<Album>, Error> {
            Ok(None)
        }

        async fn album_versions(
            &self,
            _album_id: &Id,
            _offset: Option<u32>,
            _limit: Option<u32>,
        ) -> PagingResult<moosicbox_menu_models::AlbumVersion, Error> {
            Ok(PagingResponse::empty())
        }

        #[allow(clippy::too_many_arguments)]
        async fn artist_albums(
            &self,
            _artist_id: &Id,
            _album_type: Option<AlbumType>,
            _offset: Option<u32>,
            _limit: Option<u32>,
            _order: Option<AlbumOrder>,
            _order_direction: Option<AlbumOrderDirection>,
        ) -> PagingResult<Album, Error> {
            Ok(PagingResponse::empty())
        }

        async fn add_album(&self, _album_id: &Id) -> Result<(), Error> {
            Ok(())
        }

        async fn remove_album(&self, _album_id: &Id) -> Result<(), Error> {
            Ok(())
        }

        async fn tracks(
            &self,
            _track_ids: Option<&[Id]>,
            _offset: Option<u32>,
            _limit: Option<u32>,
            _order: Option<TrackOrder>,
            _order_direction: Option<TrackOrderDirection>,
        ) -> PagingResult<Track, Error> {
            Ok(PagingResponse::empty())
        }

        async fn track(&self, _track_id: &Id) -> Result<Option<Track>, Error> {
            Ok(None)
        }

        async fn album_tracks(
            &self,
            _album_id: &Id,
            _offset: Option<u32>,
            _limit: Option<u32>,
            _order: Option<TrackOrder>,
            _order_direction: Option<TrackOrderDirection>,
        ) -> PagingResult<Track, Error> {
            Ok(PagingResponse::empty())
        }

        async fn add_track(&self, _track_id: &Id) -> Result<(), Error> {
            Ok(())
        }

        async fn remove_track(&self, _track_id: &Id) -> Result<(), Error> {
            Ok(())
        }

        async fn track_source(
            &self,
            _track: crate::TrackOrId,
            _quality: TrackAudioQuality,
        ) -> Result<Option<TrackSource>, Error> {
            Ok(None)
        }

        async fn track_size(
            &self,
            _track: crate::TrackOrId,
            _source: &TrackSource,
            _quality: PlaybackQuality,
        ) -> Result<Option<u64>, Error> {
            Ok(None)
        }
    }

    #[test_log::test]
    fn profiles_add_stores_profile() {
        let profiles = MusicApisProfiles::default();
        let source = ApiSource::register("test_profiles_add", "test");
        let api: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source.clone())));
        let mut map = BTreeMap::new();
        map.insert(source, api);
        let music_apis = Arc::new(map);

        profiles.add("test_profile".to_owned(), music_apis);

        let retrieved = profiles.get("test_profile");
        assert!(retrieved.is_some());
    }

    #[test_log::test]
    fn profiles_get_returns_none_for_unknown_profile() {
        let profiles = MusicApisProfiles::default();

        let retrieved = profiles.get("unknown_profile");
        assert!(retrieved.is_none());
    }

    #[test_log::test]
    fn profiles_upsert_adds_new_profile() {
        let profiles = MusicApisProfiles::default();
        let source = ApiSource::register("test_profiles_upsert_add", "test");
        let api: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source.clone())));
        let mut map = BTreeMap::new();
        map.insert(source, api);
        let music_apis = Arc::new(map);

        profiles.upsert("new_profile".to_owned(), music_apis);

        let retrieved = profiles.get("new_profile");
        assert!(retrieved.is_some());
    }

    #[test_log::test]
    fn profiles_upsert_updates_existing_profile() {
        let profiles = MusicApisProfiles::default();
        let source1 = ApiSource::register("test_profiles_upsert_update_1", "test");
        let source2 = ApiSource::register("test_profiles_upsert_update_2", "test");

        let api1: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source1.clone())));
        let mut map1 = BTreeMap::new();
        map1.insert(source1, api1);
        let music_apis1 = Arc::new(map1);

        let api2: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source2.clone())));
        let mut map2 = BTreeMap::new();
        map2.insert(source2.clone(), api2);
        let music_apis2 = Arc::new(map2);

        profiles.add("update_profile".to_owned(), music_apis1);
        profiles.upsert("update_profile".to_owned(), music_apis2);

        let retrieved = profiles.get("update_profile");
        assert!(retrieved.is_some());
        let retrieved_apis = retrieved.unwrap();
        assert!(retrieved_apis.get(&source2).is_some());
    }

    #[test_log::test]
    fn profiles_remove_removes_profile() {
        let profiles = MusicApisProfiles::default();
        let source = ApiSource::register("test_profiles_remove", "test");
        let api: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source.clone())));
        let mut map = BTreeMap::new();
        map.insert(source, api);
        let music_apis = Arc::new(map);

        profiles.add("remove_profile".to_owned(), music_apis);
        assert!(profiles.get("remove_profile").is_some());

        profiles.remove("remove_profile");
        assert!(profiles.get("remove_profile").is_none());
    }

    #[test_log::test]
    fn profiles_add_fetch_returns_added_profile() {
        let profiles = MusicApisProfiles::default();
        let source = ApiSource::register("test_profiles_add_fetch", "test");
        let api: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source.clone())));
        let mut map = BTreeMap::new();
        map.insert(source.clone(), api);
        let music_apis = Arc::new(map);

        let retrieved = profiles.add_fetch("add_fetch_profile", music_apis);

        assert!(retrieved.get(&source).is_some());
    }

    #[test_log::test]
    fn profiles_names_returns_all_profile_names() {
        let profiles = MusicApisProfiles::default();
        let source1 = ApiSource::register("test_profiles_names_1", "test");
        let source2 = ApiSource::register("test_profiles_names_2", "test");

        let api1: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source1.clone())));
        let mut map1 = BTreeMap::new();
        map1.insert(source1, api1);

        let api2: Arc<Box<dyn MusicApi>> = Arc::new(Box::new(TestMusicApi::new(source2.clone())));
        let mut map2 = BTreeMap::new();
        map2.insert(source2, api2);

        profiles.add("profile1".to_owned(), Arc::new(map1));
        profiles.add("profile2".to_owned(), Arc::new(map2));

        let names = profiles.names();
        assert!(names.contains(&"profile1".to_owned()));
        assert!(names.contains(&"profile2".to_owned()));
    }
}

#[cfg(feature = "api")]
pub mod api {
    //! Actix-web request extraction for profile-scoped music APIs.

    use actix_web::{FromRequest, HttpRequest, dev::Payload, error::ErrorBadRequest};
    use futures::future::{Ready, err, ok};
    use moosicbox_profiles::api::ProfileName;

    use super::{MusicApis, PROFILES};

    impl FromRequest for MusicApis {
        type Error = actix_web::Error;
        type Future = Ready<Result<Self, actix_web::Error>>;

        fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
            let profile = ProfileName::from_request_inner(req);
            let profile = match profile {
                Ok(profile) => profile,
                Err(e) => {
                    return err(e);
                }
            };

            let Some(music_apis) = PROFILES.get(&profile.0) else {
                return err(ErrorBadRequest("Invalid profile"));
            };

            ok(music_apis)
        }
    }
}